(beginner) iterable schema?

50 views
Skip to first unread message

David Conlin

unread,
Jul 7, 2016, 9:44:48 AM7/7/16
to Plumbing and Graph: the Clojure utility belt
I'm just starting to wrap my feeble brain around schemas, so if this is a stupid question I do apologise, but I've not been able to find any solution in the docs.

Say I have a function that looks something like:

(defn add-2
  [nums]
  (map (partial + 2) nums))

then I can declare "nums" to be a sequence of integers:

(s/defn add-2
  [nums :- [s/Int]]
  (map (partial + 2) nums))

But if I call add-2 with a set, this will not match the schema:

(add-2 #{2 1})

Likewise, I could declare nums to be a set of numbers:

(s/defn add-2
  [nums :- #{s/Int}]
  (map (partial + 2) nums))

but then if I call add-2 with a lazy sequence, the schema will not match:

(add-2 (range))

Both sets and lazy sequences of integers are valid inputs to add-2 - it will accept any iterable of numbers. Is there a (preferably built-in) way of declaring a parameter to be an iterable of some type (in this example Int) which permits both sets and lazy sequences?

Thanks in advance!

Dave

Jason Wolfe

unread,
Jul 7, 2016, 9:59:10 AM7/7/16
to David Conlin, Plumbing and Graph: the Clojure utility belt
Not built-in, the best you could do there for your example would be (s/cond-pre [s/Int] #{s/Int}). 

It should be trivial to add an iterable type though (without changing core) -- here's an example that should be easy to adapt.


Cheers,
Jason



--
You received this message because you are subscribed to the Google Groups "Plumbing and Graph: the Clojure utility belt" group.
To unsubscribe from this group and stop receiving emails from it, send an email to prismatic-plumb...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

David Conlin

unread,
Jul 7, 2016, 2:45:00 PM7/7/16
to Jason Wolfe, Plumbing and Graph: the Clojure utility belt

Thanks Jason, that looks like it'll hit the spot.

I guess the only question is - is this a weird use case? If so, should I sit down and try and work out what I'm doing differently (as someone learning Clojure, probably wrong), if not, should I submit a PR?

Thanks again

Dave

Jason Wolfe

unread,
Jul 8, 2016, 4:32:18 AM7/8/16
to David Conlin, Plumbing and Graph: the Clojure utility belt
Hi David,

It's definitely not 'weird', in that if you wanted to write a schema for any of Clojure's built-in 'seq' functions (map, filter, etc) you would want a 'seqable' schema. (more general than Iterable, since it also includes things like arrays).  Or course, these functions don't care what's inside the seq, so just hinting on the right collection of classes would work.

On the other hand, I don't remember anyone asking for this it in the years that Schema's been available.   I think there may have been a couple occasions where I personally reached for it, but didn't end up really needing it.   Probably the case where you care what's inside (a collection of Ints) but really don't care what format it comes in (set vs. list) is relatively uncommon.

So I'm definitely open to adding a `seqable` schema (and thanks for the offer of the PR!), but would also be interested to hear more about your actual use case. 

Thanks, 
Jason




Reply all
Reply to author
Forward
0 new messages