Re: meaning of spec/and ?

88 views
Skip to first unread message

Steve Miner

unread,
Jul 21, 2016, 2:30:56 PM7/21/16
to clo...@googlegroups.com
It looks like you’ve got your #s misplaced.  I think you want something like this:

(s/and #(> % 0.0) #(< % 1.0))

Of course, the first predicate expression could be replaced by `pos?`.

The `s/and` returns a single spec that combines multiple specs.  Of course, `clojure.core/and` is basically the logical AND of “truthy” values.

The #(…) form is creating an anonymous function.  In your first case, that creates a reasonable predicate, which works correctly as a spec.

Your second form isn’t doing what you wanted because the anonymous function notation is wrapping the whole `s/and` combining form, and in that context the tests aren't syntactically the appropriate predicates.  You’re getting an extra level of nesting and bad tests.

I suspect that the confusion comes from the similarity between a predicate and a spec.  In a sense, a predicate function is the simplest form of a spec.  However, you need a special way of combining multiple specs, not just the plain logical `and` combination.  So we have `s/and` to do the job.



On Jul 21, 2016, at 1:23 PM, Mars0i <mars...@logical.net> wrote:

With Clojure 1.9.0-alpha10:

user=> (s/def ::interval-with-cloj-and #(and   (> % 0.0) (< % 1.0)))

user=> (s/def ::interval-with-spec-and #(s/and (> % 0.0) (< % 1.0)))

user=> (s/valid? ::interval-with-cloj-and 1.0)
false

That's what I expected.

user=> (s/valid? ::interval-with-spec-and 1.0)
true


That's not what I expected.

In fact, as far as I can tell, (valid? ::interval-with-spec-and x) will return true for any number x.  What does spec/and mean, then?  I thought that in this context it would mean the same as Clojure's normal 'and'.  That's what the first example of its use in the Clojure.spec Guide seems to show.  I must be misunderstanding something basic and perhaps obvious.


Mars0i

unread,
Jul 21, 2016, 6:24:28 PM7/21/16
to Clojure
Ahh...  I realized my mistake very soon after I posted the question, and deleted it.  You must have caught it before it went away.  Your explanation is helpful, though.  Thanks.

adrian...@mail.yu.edu

unread,
Jul 21, 2016, 6:34:37 PM7/21/16
to Clojure
Just for future reference this is a mailing list and not a traditional forum, so after you post something here it will email everyone subscribed. 

Sean Corfield

unread,
Jul 21, 2016, 6:47:41 PM7/21/16
to Clojure Mailing List

And deleting a post from the Google Groups interface still leaves everyone else’s replies – and in this case Steve’s first reply to your included your original post anyway…

 

https://groups.google.com/forum/?hl=en#!topic/clojure/MxJOgQJPGN8

 

Sean Corfield -- (970) FOR-SEAN -- (904) 302-SEAN
An Architect's View -- http://corfield.org/

"If you're not annoying somebody, you're not really alive."
-- Margaret Atwood

Mars0i

unread,
Jul 21, 2016, 8:47:54 PM7/21/16
to Clojure
On Thursday, July 21, 2016 at 5:34:37 PM UTC-5, adrian...@mail.yu.edu wrote:
Just for future reference this is a mailing list and not a traditional forum, so after you post something here it will email everyone subscribed.

Thanks Adrian.  I actually didn't realize that.
Reply all
Reply to author
Forward
0 new messages