Fork me on GitHub

speaking of spec - what is the preferred way? using s/valid? in :pre :post of defn - or using the s/fdef? and when might one choose one versus the other?


@jakub696 It depends what you want to achieve.


We use Spec in lots of different ways -- see -- s/fdef works with instrumentation and generative testing so that's dev/test only (well, you can run instrumentation in production but it's often an overhead you won't want).


s/valid? and s/conform are great for production code working with data.


basically i'm static type a guy - and i want my code to crash if garbage goes in (i'll likely want this in production as well - overhead be damned)


:pre/`:post` conditions are not very commonly used in Clojure as far as I am aware.


i am more concerned with my code - not really seeing the need to enable the assertions for 3d party libraries etc.


If you're a "static type a guy", you may not find Clojure to your tastes -- Spec isn't anything like a type system, and all checking is runtime-based (it can do things a type system can't, but there are also things type systems provide that Spec does not).


If you try to write Clojure as if you're using a type system, you may find yourself with some very non-idiomatic (and slow) Clojure code. Spec is good for "system boundaries" and for data validation, as well as providing additional support during dev/test. It's worth observing that instrumentation only checks function arguments -- not return values (nor the :fn predicate relating return values and arguments). The latter are only for generative testing. Also, if you spec a higher-order function, instrumentation will do generative testing on arguments that are spec'd as functions in certain ways, which can be a real surprise to some folks.


I totally get the part about it being slow - because it's done in runtime - but i find even in static type systems - i still do some sort of validation at runtime - i mean compiler will ensure int is coming for say persons age - but i still need to validate somehow that age is between 0 and 150 at runtime - the argument about system boundries i also get - but i would argue - in larger projects it's harder to say where those boundaries are - and i tend to be defensive by default


Nothing is stopping anyone from using :pre and :post in whatever Clojure functions they write, of course, so if you are comfortable with the run time overhead, you can put whatever run time conditions you want there.


or invocations of assert , or using run time validation to cause an error return value from a function, versus throwing an exception, which is what :pre :post and assert all do.


If you go in with eyes wide open on the potential performance impact, and/or measure it to see that it is acceptable to your application, I'd say be as defensive as you want. I think the performance warnings are there so commonly, because without them, as engineers giving advice, we would feel negligent if we didn't try to warn people.


for sure totally fair - if i have a operation that's gonna get called 1 million times in one invocation - certainly will think twice about how much validation i put there - regardless of what type system (or language) i'm using so back to my original question knowing all of this are there advantages of one (:pre :post) or the other s/fdef ?


@jakub696 I didn't mean to duck out of the conversation -- it was just dinner time here. I agree with everything Andy said by way of follow-up, I just wanted to set expectations around Clojure and Spec, when you're coming from a static typing background.


no worries - in fact I read what you wrote and it's basically what i am looking for -


By default I believe s/fdef gives you only :pre checks, never :post , and you must call s/instrument to turn them on.


Also, depending upon the complexity of checks you want to do, non-spec implementations of validation checks can be an order of magnitude faster than using the logically equivalent spec validation checks.


The example I am about to link to is an extreme one, perhaps, because the non-spec validation is a single call to set? , which is very fast, but it should at least make you want to do performance measurements on any spec validation checks you perform in your application:


is it possible to turn on instrumentation only for some functions - and not others?


Yes. It is only turned on for the functions and/or namespace that you turn it on for. Never on by default.


(well, there may be dev tool environments that might turn it on in hopefully documented circumstances, but for this purpose I consider that "not on by default")

👍 4

I'm working with EDN format data on Node.js and would like to convert to JSON in order to get data. I used jsedn npm but not working well.


What is the best way to do it?


EDN is more expressive than JSON in two ways, it has more types and it is extensible. This means a generic EDN->JSON transformation cannot be done without losing information.


So to use EDN, I have to work with closurescript instead of javascript?


Actually I am new to clojure


I believe that simple examples of things for which there is an EDN syntax defined, but none for JSON, are sets of objects, and Clojure ratios (e.g. 22/7). I think a lot of people convert EDN to/from JSON while simply not using the things that JSON does not have.

👍 4

Sounds great. I am converting edn to json with cli but it's not converting some data which as # symbol as prefix. Can you let me know how to fix it?


for example echo {:a #inst 2020} | jet --to json is converting to {:a


#inst means a time literal, so #inst 2020 is January 1st, 2020, 00:00:00. I'm a bit surprised jet doesn't handle that. Maybe ask @borkdude when he's around tomorrow?


Oh, Looks like the EDN should be {:a #inst "2020"}


what you have there (without the quotes) isn't valid EDN @software.dev0218


$ echo '{:a #inst "2020"}' | jet --to json


@seancorfield Yes you are right. Actually it's {:data #inst "2020-03-03T00:00:00.000-00:00"}


OK, that would convert just fine

$ echo '{:data #inst "2020-03-03T00:00:00.000-00:00"}' | jet --to json


I am still getting like this


this can be related to something else?


You need ' around it

👍 4

otherwise echo is sending three separate strings and jet is just reading one


Compare what I posted above


It's working for now. I will try with real data. Thank you very much:+1:


:thinking_face: REPL evaluates \space into it seld but does not evaluate \cosmos 😲


Hi, I'm trying to do destructure in a let like so {{condition :main} :weather} x , problem is :weather gives back a vector (`[{:id 802, :main Clouds, :description Mäßig bewölkt, :icon 03d}]`), so I should do first on it before I can do :main, any way to do this inside the let?


I assume you mean something like {[{condition :main} & -rest] :weather} x ?


{[{condition :main} & _] :weather} x


I can't really say as to whether it should be done that way btw, as on one hand it looks like something you might have to unscramble a bit in your mind to figure out what's going on, due to the quite nested destructuring. On the other hand, I don't think it would take significant time to do so, and I don't know in the scheme of things if it would really matter either way. Its not something I've thought about enough to feel comfortable having a definite answer, so I'll just say that's at least how you would do that, if I'm understanding you, without commenting on whether or not you should do it


Hi. when I convert from EDN to JSON it also has to be working?

({:description "Massachusetts Presidential Primary Election", :date #inst "2020-03-03T00:00:00.000-00:00", :district-divisions #{{:ocd-id "ocd-division/country:us/state:ma", :voter-registration-authority-level :municipal, :election-authority-level :municipal, :voting-methods #{{:primary true, :instructions {:voting-id "You may be asked to show identification if: you are voting for the first-time in Massachusetts in a federal election, you are an inactive voter, you are casting a provisional or challenged ballot, or if the poll worker has a reasonable suspicion that leads them to request identification. Acceptable forms include (must show your name and address at which you are registered to vote): a MA driver's license or MA-issued ID card; recent utility bill; rent receipt; signed lease; a copy of a voter registration affidavit; or any other printed identification which contains the voter's name and address.\n\nVoters without ID: If you're a first-time voter who is unable to present ID when you check in, you may vote a provisional ballot and return with acceptable ID by close of polls. If you're asked for ID for any other reason, and are not able to present ID in such a situation, you must still be permitted to vote; however, your ballot must be challenged. Your ballot will be cast normally, and will only be re-examined in the case of a recount, court order, or audit."}, :type :in-person, :excuse-required false} {:primary false, :type :by-mail, :excuse-required true, :ballot-request-deadline-received #inst "2020-03-02T00:00:00.000-00:00", :acceptable-forms #{{:name :ma_absentee}}} {:primary false, :start #inst "2020-02-24T00:00:00.000-00:00", :type :early-voting, :excuse-required false, :end #inst "2020-02-28T00:00:00.000-00:00"}}, :voter-registration-methods #{{:instructions {:registration "You should know: you need an ID issued by the Massachusetts Registry of Motor Vehicles. If you don't have an ID issued by the Massachusetts Registry of Motor Vehicles, you can still register by mail to vote."}, :type :online, :supports-iframe true, :deadline-online #inst "2020-02-12T00:00:00.000-00:00", :url ""} {:deadline-postmarked #inst "2020-02-12T00:00:00.000-00:00", :instructions {:signature "To register in Massachusetts you must: \nbe a citizen of the United States \nbe a resident of Massachusetts \nbe at least 16 years old (must be 18 years old to vote on Election Day)\nnot have been convicted of corrupt practices in respect to elections \nnot be under guardianship with respect to voting \nnot be currently incarcerated for a felony conviction", :idnumber "Federal law requires that you provide your Massachusetts driver's license number to register to vote. If you do not have a current and valid Massachusetts driver's license, you must provide the last four digits of your Social Security number. If you have neither, you must write \"NONE\" in the box  and a unique identifying number will be assigned to you."}, :type :by-mail, :acceptable-forms #{{:name :nvrf}}}}, :primary-voting-method-source :state}}, :type :presidential-primary, :source {:type :state-list, :notes "", :date #inst "2019-12-05T00:00:00.000-00:00"}, :polling-place-url "", :id #uuid "5e262e0e-d971-461c-8b7c-31eeb50d7fcc", :population 6902149, :website "", :polling-place-url-shortened ""})


with echo '...' | jet --to json


I am getting weird issue, it's showing the syntax error near unexpected token ('`


use a vector [] rather than a list (). I don't think the list or seq type is valid edn totally incorrect. see below


it is:

$ echo '(1 2 3)' | jet --from edn --to json


ah my bad 🙂


went and looked at the spec. i assumed there was function invocation going on rather than literal list


@dpsutton EDN doesn't know about function calls, it's just data without evaluation


fwiw if I paste that EDN in a file foo.edn and then do:

cat /tmp/foo.edn | jet --from edn --to json
it works fine. so there's probaby some issue with how you feed the EDN to stdin regarding quotes


Okay. Thanks. I will try


I suspect the single quotes you enclosed it in are being closed prematurely by a quotation in some text


such as the part that mentions "MA's driver's license"


So this can cause the issue?


you need to escape those single quotes or double quotes depending on the outer quotes


or put it all into a file, and use the command to read the file.


then you should not need to figure out how to do shell quoting correctly at all.


How to install jet-cli in win10?


Is it impossible?


hmm, at the moment I haven't got a Windows release for that project, but it's not impossible to make one. So far you're the first to request one. I'll make an issue for it


in the meantime, you can maybe use wsl(2)


@software.dev0218 Is your problem translating edn to json or vice versa? that's also possible with babashka which does have a Windows build:


Yes my purpose is to translate from EDN to JSON


with babashka you can do this:

bb.exe -e '(->> "foo.edn" slurp json/generate-string (spit "foo.json"))'


Ah. Thank you!


sorry, forgot an edn/read-string:

bb -e '(->> "/tmp/foo.edn" slurp edn/read-string json/generate-string (spit "/tmp/foo.json"))'


@software.dev0218 You can install babashka using Ales's scoop bucket:


Is it working on mac and linux as well?


(it = babashka, not scoop)


for mac and linux you can use brew to install


or just curl


I got it. Actually I am in need to translate edn to json with cli on my project


what exactly do you mean with cli?


I am translating edn to json with jet-cli on runtime.


It's Node.js project.


yeah, you can use babashka instead as well


exec(cat ./data.edn | jet --from edn --to json`, (err, stdout, stderr)`


same speedy startup.


Ah. It's okay. Then I will try with babashka


@software.dev0218 Is your project entirely in nodeJS?


If the EDN libraries for nodeJS aren't working for you (I don't know them personally), one other way could also be to use the sci library:

$ node
Welcome to Node.js v12.8.0.
Type ".help" for more information.
> const { evalString, toJS } = require("@borkdude/sci")
> toJS(evalString("{:a 1 :b 2}"))
{ b: 2, a: 1 }
You might need to add a single quote in front of the edn value to prevent evaluating it as Clojure code instead of just data, so like:
> toJS(evalString("'[foo bar]"))
[ 'foo', 'bar' ]


now I'm afk


@borkdude My project is entirely built with Node.js. I tried to use the 'jsden' npm but generated structure was a little mess.


This sci can be installed with npm?


yeah, just npm install @borkdude/sci --save


it is an entire Clojure interpreter, so it does a little more than only converting EDN

👍 4

Ah I see. Then I will try with it