Skip to content

Instantly share code, notes, and snippets.

@gfredericks
Created March 17, 2012 17:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gfredericks/2063360 to your computer and use it in GitHub Desktop.
Save gfredericks/2063360 to your computer and use it in GitHub Desktop.
Reconstitutible Iterate
(ns lib-2289.core
(:refer-clojure :exclude [iterate]))
(defprotocol IReconstitutible
(reconstitute [self]))
(extend-type clojure.lang.LazySeq
IReconstitutible
(reconstitute [self]
(reconstitute (.seq self))))
(deftype IterateSeq [f f-form ob]
clojure.lang.Seqable
(seq [self] self)
clojure.lang.ISeq
(first [self] ob)
(next [self] (new IterateSeq f f-form (f ob)))
(more [self] (new IterateSeq f f-form (f ob)))
(cons [self item]
(cons item self))
IReconstitutible
(reconstitute [self]
(list 'iterate f-form ob)))
(defmacro iterate
[f ob]
`(new IterateSeq ~f '~f ~ob))
;; user> (->> (iterate (partial + 7) 18) (drop 82484) reconstitute)
;; (iterate (partial + 7) 577406)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment