(另见已关闭http://dev.clojure.org/jira/browse/CLJ-1964)
(require '[clojure.spec :as s]) (s/def ::map-tree (s/map-of keyword? (s/or :tree ::map-tree :leaf nil?))) (s/exercise ::map-tree)
在我的机器上挂起。
来自https://groups.google.com/forum/#!topic/clojure/IvKJc8dEhts的另一个示例,这立即导致我的机器上出现StackOverflowError
`
(require '[clojure.spec.gen :as gen])
(defrecord Tree [name children])
(defrecord Leaf [name])
(s/def ::name string?)
(s/def ::children (s/coll-of (s/or :tree ::Tree, :leaf ::Leaf)))
(s/def ::Leaf (s/with-gen
(s/keys :req-un [::name])
#(gen/fmap (fn [name] (->Leaf name)) (s/gen ::name))))
(s/def ::Tree (s/with-gen
(s/keys :req-un [::name ::children])
#(gen/fmap
(fn [[name children]] (->Tree name children))
(s/gen (s/tuple ::name ::children)))))
;;偶尔生成,但通常堆栈溢出
(binding [s/*recursion-limit* 1]
(gen/generate (s/gen ::Tree)))
StackOverflowError
clojure.lang.RT.seqFrom (RT.java:533)
clojure.lang.RT.seq (RT.java:527)
clojure.core/seq--6221 (core.clj:137)
clojure.core/map/fn--6687 (core.clj:2736)
clojure.lang.LazySeq.sval (LazySeq.java:40)
clojure.lang.LazySeq.seq (LazySeq.java:49)
clojure.lang.RT.seq (RT.java:525)
clojure.core/seq--6221 (core.clj:137)
clojure.core/every? (core.clj:2652)
clojure.spec/tuple-impl/reify--13509 (spec.clj:905)
clojure.spec/gensub (spec.clj:228)
clojure.spec/gen (spec.clj:234)
`