Re: code from spec with clojure.spec?

2016-11-06 Thread Joe Corneli
This is a late reply to my own question but maybe it will be helpful to a 
future searcher.

http://blog.cognitect.com/blog/2016/10/5/interactive-development-with-clojurespec
has an example that showed me more clearly how to use :fn.

Specifically here's how I rewrote the spec:

(s/fdef mapper
:args (s/cat :t :fake.flowrs-tests/positive-integer?
 :b :fake.flowrs-tests/positive-integer?)
:ret map?
:fn (fn [{args :args ret :ret}]
  (= (:tacos ret)
 (vec (range (:t args
  (= (:burritos ret)
 (vec (range (:b args))

(s/exercise-fn `mapper) has the same behavior as before -- it exercises the 
*implementation* of the function.  But (stest/check `mapper) will return 
"Specification-based 
check failed" etc. when the implementation of mapper doesn't match.

As for the other part of the question: turning the logical test of :fn into 
a function that can be run to generate the result, that would require quite 
a bit of code rewriting.  A partial step is just to extract the :fn part of 
the code, and that can be done as follows:

(:fn (apply hash-map (rest (s/form (get (s/registry) 
'fake.flowrs/mapper)
;=>
(clojure.core/fn
 [{args :args, ret :ret}]
 (clojure.core/=
  (:tacos ret)
  (clojure.core/vec (clojure.core/range (:t args
 (clojure.core/=
  (:burritos ret)
  (clojure.core/vec (clojure.core/range (:b args)

The function so-obtained can be evaluated, but it won't "do" the job of the 
mapper function without further code transformation.  I'll leave it at that 
for now.

-- 
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.


code from spec with clojure.spec?

2016-10-17 Thread Joe Corneli
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 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.


Re: breaking homoiconicity?

2015-06-20 Thread Joe Corneli


On Saturday, June 20, 2015 at 4:15:30 AM UTC+1, Sean Corfield wrote:

 (.getTypeName (Class/forName [Ljava.lang.String;)) 
 ;;= java.lang.String[] — that is more readable! 


Thanks, that's helpful for me.  By chance do you know if the class is 
natively recoverable from the TypeName for Clojure/Java?  Class/forName 
can't roundtrip that string.  I could implement a look-up table to 
translate between the two formats, but that seems rather kludgey.

-- 
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.


breaking homoiconicity?

2015-06-19 Thread Joe Corneli
This is an interaction with Clojure via CIDER.

repl [1] (type (into-array String [Awesome]))
[Ljava.lang.String;
repl [2] [Ljava.lang.String;
RuntimeException EOF while reading, starting at line 1  
clojure.lang.Util.runtimeException (Util.java:221)
repl [3] (quote [Ljava.lang.String;)

CIDER won't let me enter [3] claiming that the input is not complete.  
What's going on here, apart from things not working?  If 
[Ljava.lang.String; is a valid return value, and can show up in code that I 
construct programmatically, why is it not valid input?  Is this a flaw in 
CIDER / lein, or are they giving me the right answers here?

-- 
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.