Skip to content

Instantly share code, notes, and snippets.

@trptcolin
Created June 20, 2010 01:30
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 trptcolin/445462 to your computer and use it in GitHub Desktop.
Save trptcolin/445462 to your computer and use it in GitHub Desktop.
(defmacro lazy-cons
"Expands to code which produces a seq object whose first is
first-expr and whose rest is rest-expr, neither of which is
evaluated until first/rest is called. Each expr will be evaluated at most
once per step in the sequence, e.g. calling first/rest repeatedly on the
same node of the seq evaluates first/rest-expr once - the values they yield are
cached."
[first-expr & rest-expr]
(list 'new 'clojure.lang.LazyCons (list `fn (list [] first-expr) (list* [(gensym)] rest-expr))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment