Fork me on GitHub
#clojure-spec
<
2017-12-27
>
shdzzl04:12:12

I'm trying to work out what is happening with the following snippet, I think I must be misunderstanding how s/cat works:

shdzzl04:12:19

It makes no sense to me that it would be matching the first vector element on ::bound, so I'm not sure how that's the spec it's failing.

flyboarder04:12:59

@shdzzl I had issues wrapping my head around s/cat also, it kinda explodes the argument, is the best way I can describe it

shdzzl04:12:13

I'm not sure I follow, it explodes which argument? I visualize it as kind of zipping predicates to a sequence, so my s/cat example should match any seq of length three and check the first element against ::needs, the second against ::bound and the third against ::body. What am I missing?

flyboarder04:12:22

try (s/and vector? (s/cat .....))

shdzzl04:12:10

Same result.

flyboarder04:12:47

Loading up a repl, one sec

flyboarder05:12:26

@shdzzl well to start with your :needs spec fails

flyboarder05:12:44

thats the real issue

flyboarder05:12:57

(s/def ::needs (s/coll-of symbol?))

flyboarder05:12:09

boot.user=> (s/explain (s/cat :needs ::needs :bound ::bound :body ::body) [['a 'b] {} '(+ a b)])
Success!

shdzzl05:12:51

Uhuh, that does fix it. ::needs wasn't failing when tested on it's own. Is there some flattening or something going on? I'm going to have read over s/* again. Thanks a ton.

flyboarder05:12:39

@shdzzl thats what s/cat is doing (s/cat :needs (s/* symbol?)) is different than when you move that into it’s own spec

shdzzl05:12:39

Ooohhh! When written like that it's more obvious.

flyboarder05:12:18

rule of thumb for (s/cat) build it up one arg at a time

flyboarder05:12:31

otherwise it’s hard to figure out where it fails

shdzzl05:12:07

Good advice.

shdzzl05:12:17

While I'm here, I had another question. Is it possible/advisable to spec constructors (`MyRecord.` for example)?

shdzzl05:12:55

It touches on it in the spec guide, but I was curious if someone had tried it with fdef or similar.

johanatan05:12:49

user> (time (stest/check `a-symbol))
"Elapsed time: 2.153437 msecs"
does anyone have an explanation for why the above completes almost instantly but my repl does hang during the 10s of seconds that it actually takes to check the symbol in question?

tbaldridge05:12:10

@johanatan what does that code print?

johanatan05:12:40

which code? it just prints the elapsed time line, then hangs until the check actually completes

johanatan05:12:49

then i get my REPL prompt back to execute more lines

tbaldridge05:12:52

and what does it return when it completes?

johanatan05:12:12

let me check. didn't wait for it to complete before because i was trying to debug why a loop/recur was spinning constantly instead of waiting for check to complete

johanatan05:12:36

@tbaldridge it returned the typical map result from check

johanatan05:12:31

({:spec #object[clojure.spec.alpha$fspec_impl$reify__2451 0x6d92492f "clojure.spec.alpha$fspec_impl$reify__2451@6d92492f"], :clojure.spec.test.check/ret {:result true, :num-tests 1000, :seed 1514353342056}, :sym my-namespace/a-symbol})

tbaldridge05:12:57

So one last thing to try:

tbaldridge05:12:08

(do (time (stest/check a-symbol)) nil)`

tbaldridge05:12:43

bleh you get the point, wrap it in a do, nil. that will remove any questions about REPL printing taking a long time

tbaldridge05:12:49

which is normally what it is in cases like this

johanatan05:12:55

hmm, that actually returned immediately like before but also gave me back control at the REPL prompt (i'm assuming the check is happening in the bkg and i do have a process called main doing a lot of CPU work in my procmon)

johanatan05:12:43

[i think main is the cider repl server if i'm not mistaken-- last time i killed it, cider immediately printed out a bunch of stuff indicating unhappiness lol]

johanatan06:12:23

check returns a lazyseq

johanatan06:12:39

so wrapping it in doall seems to have done the trick

johanatan06:12:16

[i suppose the result wasn't being dropped on the floor previously because the repl itself was waiting for the value to be returned so that it could print it]

johanatan06:12:26

thanks for the help!

flyboarder06:12:54

@shdzzl sorry I havn’t tried that one

shdzzl06:12:59

No problem, I'm going to look into it when I get the time.