Created
March 17, 2012 17:44
-
-
Save gfredericks/2063360 to your computer and use it in GitHub Desktop.
Reconstitutible Iterate
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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