有没有办法在Clojure中描述任意懒惰的自递归数据结构? 让我们举例说,我想做这样的事情:
(def inf-seq (fn rec [] (lazy-seq (cons 42 (rec)))))
(take 3 (inf-seq))
但有地图:
(def inf-map (fn rec [] (??? {:a (rec) :b 42})))
(get-in (inf-map) [:a :a :a :b])
答案 0 :(得分:1)
序列延迟不适用于Clojure中的延迟函数求值,您显然需要构建无限嵌套映射。
尝试使用延迟:
user=> (def inf-map (fn rec [] {:a (delay (rec)) :b 42}))
#'user/inf-map
user=> (inf-map)
{:a #<Delay@4e9f9a19: :pending>, :b 42}
user=> @(:a (inf-map))
{:a #<Delay@5afd479c: :pending>, :b 42}