Source

ClojureCheck / src / main / clojure / clojurecheck / core.clj

Diff from to

src/main/clojure/clojurecheck/core.clj

         gen (zipmap (map (fn [k] `(quote ~k)) ks) (vals bindings))]
     `(transform ~gen (fn [{:syms [~@ks]}] ~@body))))
 
+;; ## Data structure generators
+(defn list
+  "Generates a list based on the given generator. The length of
+  the list is an integer generator. The default grows with the
+  size guidance. The size guidance is passed verbatim to the
+  item generator."
+  {:added "2.0"}
+  [item & {:keys [length] :or {length (int)}}]
+  (generators {len      length
+               elements (repeat item)}
+    (take len elements)))
+
+(defn vec
+  "Generates a vector based on the given generator. The length of
+  the vector is an integer generator. The default grows with the
+  size guidance. The size guidance is passed verbatim to the item
+  generator."
+  {:added "2.0"}
+  [item & {:keys [length] :or {length (int)}}]
+  (generators {elems (list item :length length)}
+    (clojure.core/vec elems)))
+
+(defn set
+  "Generates a set based on the given generator. The size of
+  the set is an integer generator. The default grows with the
+  size guidance. The size guidance is passed verbatim to the
+  item generator."
+  {:added "2.0"}
+  [item & {:keys [length] :or {length (int)}}]
+  (generators {elems (list item :length length)}
+    (clojure.core/set elems)))
+
+(defn sorted-set
+  "Generates a sorted-set based on the given generator. The size of
+  the sorted-set is an integer generator. The default grows with the
+  size guidance. The size guidance is passed verbatim to the item
+  generator."
+  {:added "2.0"}
+  [item & {:keys [length] :or {length (int)}}]
+  (generators {elems (list item :length length)}
+    (apply clojure.core/sorted-set elems)))
+
+(defn hash-map
+  "Generates a hash-map based on the given generators. The size of
+  the hash-map is an integer generator. The default grows with the
+  size guidance. The size guidance is passed verbatim to the key
+  and value generators."
+  {:added "2.0"}
+  [keys vals & {:keys [length] :or {length (int)}}]
+  (generators {len length
+               ks  (repeat keys)
+               vs  (repeat vals)}
+    (zipmap (take len ks) (take len vs))))
+
+(defn sorted-map
+  "Generates a sorted-map based on the given generators. The size of
+  the sorted-map is an integer generator. The default grows with the
+  size guidance. The size guidance is passed verbatim to the key and
+  value generators."
+  {:added "2.0"}
+  [keys vals & {:keys [length] :or {length (int)}}]
+  (generators {len length
+               ks  (repeat keys)
+               vs  (repeat vals)}
+    (apply clojure.core/sorted-map (interleave (take len ks) (take len vs)))))
+
+(defn string
+  "Generates a string taking characters from the given generator. The
+  length of the string is an integer generator. The default grows with
+  the size guidance. The size guidance is passed verbatim to the
+  character generator."
+  {:added "2.1"}
+  [characters & {:keys [length] :or {length (int)}}]
+  (generators {chs (list characters :length length)}
+    (apply str chs)))
+
 (defn transform
   "Generates a value according to `gen` and calls `f` with the generated
   value—not the domain value. The return value of `f` becomes the new
         (when (pred (.value dom-value))
           dom-value)))))
 
-(defn list
-  "Generates a list based on the given generator. The length of
-  the list is an integer generator. The default grows with the
-  size guidance. The size guidance is passed verbatim to the
-  item generator."
-  {:added "2.0"}
-  [item & {:keys [length] :or {length (int)}}]
-  (let-gen [len      length
-            elements (repeat len item)]
-    elements))
-
-(defn vec
-  "Generates a vector based on the given generator. The length of
-  the vector is an integer generator. The default grows with the
-  size guidance. The size guidance is passed verbatim to the item
-  generator."
-  {:added "2.0"}
-  [item & {:keys [length] :or {length (int)}}]
-  (let-gen [elems (list item :length length)]
-    (clojure.core/vec elems)))
-
-(defn set
-  "Generates a set based on the given generator. The size of
-  the set is an integer generator. The default grows with the
-  size guidance. The size guidance is passed verbatim to the
-  item generator."
-  {:added "2.0"}
-  [item & {:keys [length] :or {length (int)}}]
-  (let-gen [elems (list item :length length)]
-    (clojure.core/set elems)))
-
-(defn sorted-set
-  "Generates a sorted-set based on the given generator. The size of
-  the sorted-set is an integer generator. The default grows with the
-  size guidance. The size guidance is passed verbatim to the item
-  generator."
-  {:added "2.0"}
-  [item & {:keys [length] :or {length (int)}}]
-  (let-gen [elems (list item :length length)]
-    (apply clojure.core/sorted-set elems)))
-
-(defn hash-map
-  "Generates a hash-map based on the given generators. The size of
-  the hash-map is an integer generator. The default grows with the
-  size guidance. The size guidance is passed verbatim to the key
-  and value generators."
-  {:added "2.0"}
-  [keys vals & {:keys [length] :or {length (int)}}]
-  (let-gen [len length
-            ks  (list keys :length (constantly len))
-            vs  (list vals :length (constantly len))]
-    (zipmap ks vs)))
-
-(defn sorted-map
-  "Generates a sorted-map based on the given generators. The size of
-  the sorted-map is an integer generator. The default grows with the
-  size guidance. The size guidance is passed verbatim to the key and
-  value generators."
-  {:added "2.0"}
-  [keys vals & {:keys [length] :or {length (int)}}]
-  (let-gen [len length
-            ks  (list keys :length (constantly len))
-            vs  (list vals :length (constantly len))]
-    (apply clojure.core/sorted-map (interleave ks vs))))
-
-(defn string
-  "Generates a string taking characters from the given generator. The
-  length of the string is an integer generator. The default grows with
-  the size guidance. The size guidance is passed verbatim to the
-  character generator."
-  {:added "2.1"}
-  [characters & {:keys [length] :or {length (int)}}]
-  (let-gen [chs (list characters :length length)]
-    (apply str chs)))
-
 (defn sized
   "Modify the size guidance according to f and pass it on to the
   given generator. If f is not a function it will be taken turned