clojure-spec

About: http://clojure.org/about/spec Guide: http://clojure.org/guides/spec API: https://clojure.github.io/spec.alpha/clojure.spec.alpha-api.html
misha 2020-07-01T13:16:14.417200Z

:harold:

:1
=> :1

(keyword "foo" "1")
=> :foo/1

:foo/1
Syntax error reading source at (REPL:1:1).
Invalid token: :foo/1

alexmiller 2020-07-01T13:17:19.418Z

there's a lot of tedious history here but keywords with numbers as the name part are technically not valid in the reader

alexmiller 2020-07-01T13:18:08.418900Z

due to a long-standing bug in the regex for keywords, things like :1 are actually read however and we've decided not to make them invalid

misha 2020-07-01T13:18:54.419700Z

backward compatibility is tough

alexmiller 2020-07-01T13:19:05.419900Z

and just generally, there are a lot of keywords you can make programatically that are not print/read roundtrippable (https://clojure.org/guides/faq#unreadable_keywords)

misha 2020-07-01T13:21:09.421800Z

this is I am aware of, yes. trying to choose generated keyword format, suitable for all of: item spec (:foo/i1) cat/or branch spec for that item (:i1), conformed value walking (idx) etc.

misha 2020-07-01T13:25:08.423900Z

@alexmiller is it possible to get quoted form of lambda verbatim as edn? e.g. #(+ % 2) so it could be printed out exactly like this #(+ % 2) ?

misha 2020-07-01T13:25:30.424300Z

'#(+ % 2)
=> (fn* [p1__7217#] (+ p1__7217# 2))

alexmiller 2020-07-01T13:25:33.424400Z

no?

misha 2020-07-01T13:25:42.424600Z

ok :(

alexmiller 2020-07-01T13:26:28.425500Z

if you're worried about the gensyms, you can transform to (fn [%] (+ % 2)) - spec does this for forms

alexmiller 2020-07-01T13:28:06.427500Z

(as an aside, there is a ticket and some work on better controlling gensym construction - this is often a tricky case when trying to symbolically compare macroexpanded code chunks)

misha 2020-07-01T13:29:20.427700Z

I am generating lambda forms for generated specs, and often fn one is way too long, so I might as well just defn it:

(defn <=10? [x] (<= x 10))
(defn >=5? [x] (>= x 5))
(s/def :user/root (s/and number? >=5? <=10?))

misha 2020-07-01T13:30:55.428900Z

which is, arguably, better, than

(s/def :user/root (s/and number? (fn [x] (>= x 5)) (fn [x] (<= x 10))))

misha 2020-07-02T08:36:46.430700Z

not gonna: • complicates lib code • less granular spec errors • less similar to source json-schema, so when(if) you gonna debug things – you will have to recalculate another transformation in your mind

vemv 2020-07-02T19:03:32.431600Z

https://clojuredocs.org/clojure.spec.alpha/int-in

👍 1
misha 2020-07-03T15:39:32.435400Z

> range from start (inclusive) to end (exclusive). - only for ints - (ex)Inclusiveness is baked in - requires both limits - specifically range spec was not the point, but readability of inline functions was

misha 2020-07-01T13:32:13.429400Z

but then, there are things like ratios, decimals, etc :opieop:

Jan K 2020-07-01T23:55:56.429700Z

You could also do #(<= 5 % 10)