Fork me on GitHub
#clojure-spec
<
2017-06-13
>
carocad11:06:08

hey guys, quick question: what is the clojure/spec.alpha project in github? is it an old version of clojure.spec?

gfredericks11:06:44

I think it's the new version

mpenet11:06:34

yes, the fact that the readme mentions nothing about what it is doesn't help tbh (or how to use it; you have to dig into the google group announcement instead)

carocad11:06:36

wait what! so if I update my version of Clojure then it would break 😞 ?

carocad11:06:43

is it also like that for Cljs?

mpenet11:06:45

yes, you need to update your require calls and potentially clj.spec/invalid kw

mpenet11:06:54

dunno about cljs, but I guess so

hkjels11:06:37

This problem had me spinning for close to an hour

mpenet11:06:26

wasted quite some time on it too, even tho I knew what to do

joshkh14:06:11

is it possible to use spec to ensure that one spec'ed key in a map containing a number is larger than the value another spec'ed key?

joshkh14:06:25

starting with something like this:

(s/def :my.app/start int?)
(s/def :my.app/end int?)

(s/def :my.app/timer
  (s/keys :req [:my.app/start :my.app/end]))

andrewmcveigh14:06:02

Yes, you can do something like

(s/def :my.app/timer
  (s/and (s/keys :req [:my.app/start :my.app/end])
         #(<= (:my.app/start %) (:my.app/end %))))

joshkh14:06:49

thanks. that makes perfect sense.

joshkh14:06:41

s/def takes a keyword and a predicate (or composite of them)

andrewmcveigh14:06:22

Yeah, the 2nd arg is a spec, which can be specs from the spec namespace, sets, or really any function that returns truthy/falsey, or any combination.

andrewmcveigh14:06:15

The docs say spec, spec-name, predicate or regex-op

joshkh14:06:26

i got as far as Usage: (def k spec-form) and didn't bother to read down 😉

joshkh14:06:06

this is probably a dumb question, but when exercising a spec with a custom function (such as the <= above), is clojure generating ints at random and then only filtering out results that pass the spec? or is it truly generating valid specs on the first try?

joshkh14:06:19

in other words, how hard is it working to exercise the spec?

andrewmcveigh15:06:27

s/and uses the first spec as a generator, and the following specs as filters

andrewmcveigh15:06:36

as far as I remember

andrewmcveigh15:06:08

If you want something more intelligent, you'd have to provide the generator yourself

joshkh15:06:21

gotcha, thanks again

mattly16:06:56

is the plan to move clojure.spec.alpha back to just clojure.spec after it's released?

mattly16:06:15

I might just park at the version of clojure 1.9 I'm using until it goes back

royalaid16:06:58

https://groups.google.com/forum/#!msg/clojure/10dbF7w2IQo/ec37TzP5AQAJ <- @mattly, seems that the plan is to move back to a non-alpha namespace after

Alex Miller (Clojure team)16:06:21

It is likely that 1.9 will release before spec is finalized (that is, 1.9 final will depend on clojure.spec.alpha). Being able to do so is the main reason we did the split.

mattly16:06:06

ok, thanks

joshkh16:06:47

can someone help me understand why this is producing a vector of vectors rather than a single collection of numbers?

(s/def :test/coll (s/coll-of number?))
(s/exercise :test/coll)
=> ([[-2.0 -1 0 -2.0 -2.0 1.0 0 0.5 0 0 -0.5 -2.0 0.5 -0.5] [-2.0 -1 0 -2.0 -2.0 1.0 0 0.5 0 0 -0.5 -2.0 0.5 -0.5]]
     [[0 0 -3.0 -1 0 -1 -1 -0.75 -1 -1 0 -1 -2.0]...]

joshkh16:06:47

i would have expected just a list of collections

joshkh16:06:27

i also get some NaNs

joshkh16:06:32

[[0 -4.0 0 26 1 -1.25 NaN] [0 -4.0 0 26 1 -1.25 NaN]]

gfredericks16:06:14

does NaN pass number?

waffletower20:06:59

you probably already answered that for yourself…

(defn nan? [x]
  (if (number? x)
    (not (= (double x) (double x)))
    true))

waffletower20:06:22

(fact "NaNs are true and numbers are false"
  (nan? 0) => false
  (nan? 1) => false
  (nan? 3.4) => false
  (nan? -7.0) => false
  (nan? "yoplait") => true
  (nan? "") => true
  (nan? Float/NaN) => true
  (nan? Double/NaN) => true
  (= (nan? Float/NaN) (number? Float/NaN)) => true          ;; this highlights that (nan?) is not a precise complement of (number?)
  (= (nan? Double/NaN) (number? Double/NaN)) => true
)

gfredericks22:06:14

I didn't; NaN is a pretty thorny case for generators, primarily because they don't equal themselves

gfredericks22:06:58

otherwise I think it's appropriate for a number? generator to generate them

joshkh16:06:01

i don't even know how you'd represent NaN in clj...

waffletower20:06:22

joshkh: Float/NaN and Double/NaN are distinct floating point literals available in clojure. Sorry if I am late

joshkh21:06:16

good to know! since you seem savvy, how does one use the Infinity literal?

waffletower19:07:32

Oops missed your reply sorry 😦 Double/POSITIVE_INFINITY & Double/NEGATIVE_INFINITY

joshkh21:07:01

it's all good! your guidance steered me true. i just had to adjust for javascript which... against all odds... supports positive infinity 🙂

gfredericks16:06:14

it's an instance of Double

joshkh16:06:19

but i'm still more confused about the deeply nested collections

gfredericks16:06:52

read the docstring for s/exercise

gfredericks16:06:25

it's not a coincidence that you have pairs of identical collections

joshkh16:06:50

pairs of generated and conformed values for a spec ahhh

dpsutton16:06:52

it's generating ten collections of things satisfying number?, right?

Alex Miller (Clojure team)16:06:04

@joshkh exercise generates values, and conforms them, then returns a collection of those pairs

Alex Miller (Clojure team)16:06:36

you can also (gen/sample :test/coll) to just get the samples

joshkh16:06:46

that's the second time i've stared right past the docs today. time for a coffee. thanks guys.

Alex Miller (Clojure team)16:06:53

or (map first (s/exercise :test/coll))

Alex Miller (Clojure team)16:06:26

@mpenet I updated the spec.alpha readme a little

mpenet16:06:00

It's not pushed yet, right?

mpenet16:06:26

I was looking at core.specs.alpha :D mixed the two. I guess it might get a readme update as well there

mpenet17:06:15

A lot better, thanks

Alex Miller (Clojure team)17:06:11

certainly better than nothing! I meant to come back to those, just forgot to do so after the release.