On Monday, October 17, 2016 at 5:51:42 PM UTC-5, Joe Corneli wrote: > > 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))) >
An aside: you can use `pos?` for the latter half of those specs (or just replace the whole thing with pos-int? if you're ok with just fixed precision integers). > > :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? > In short, no. > The behavior I have in mind is (after all) specified in the :fn part of > the fdef. > There is a definite art to writing :fn specs that a) check something useful and b) don't actually repeat the code that is actually in the function. I think this particular spec is more than I would likely try to spec but I don't know that there is a single right answer. > 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? > See my answer in the other thread - the generated function just generates return values according to the :ret spec, without regard to the args passed. Use a custom generator for the function if you want/need 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 clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com 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 clojure+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.