(defn unfold [step seed]
(if-let [[val new-seed] (step seed)]
(cons val (lazy-seq (unfold step new-seed)))
nil))
Example usage:
(defn fib-step [[x y]] [x [y (+ x y)]])
(take 10 (unfold fib-step [0 1])) ;=> (0 1 1 2 3 5 8 13 21 34)
(defn decreasing [x] (if (neg? x) nil [x (dec x)]))
(unfold decreasing 5) ;=> (5 4 3 2 1 0)
Does this or something like it exist in clojure standard (or commonly used) libs? If not, is there a reason why? The closest I've found is this blog post:
http://www.matlux.net/blog/2014/05/04/anamorphic-adventure-in-clojure