As a running example related to the ideas in
http://clojure.org/guides/spec#_spec_ing_functions, please consider the
following function:
(defn mapper [x y]
{:tacos (vec (range x))
:burritos (vec (range y))})
Example input and output:
(mapper 2 4) ;=> {:tacos [0 1], :burritos [0 1 2 3]}
OK, I'll go ahead and write a sensible-seeming spec for this function now,
as follows:
(s/fdef mapper
:args (s/cat :t (s/and integer? #(> % 0))
:b (s/and integer? #(> % 0)))
:ret map?
:fn (s/and #(= (-> % :ret :tacos)
(vec (range (-> % :args :t))))
#(= (-> % :ret :burritos)
(vec (range (-> % :args :b))))))
My first question: Is there a way to only write the spec, and have the
function generated automatically? The behavior I have in mind is (after
all) specified in the :fn part of the fdef.
And, another somewhat related question: if I redefine the function to do
nothing, and then exercise it, I get output that doesn't seem to conform to
the :fn condition given above, but no error -- why is that?
(defn mapper [x y])
(s/exercise-fn `mapper)
;=> ([(1 3) nil] [(1 1) nil] [(4 2) nil] [(2 3) nil] [(6 3) nil] [(15 2633)
nil] [(3 1) nil] [(3 4) nil] [(1 1) nil] [(28 4) nil])
Thanks for any help!
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.