Skip to content

Instantly share code, notes, and snippets.

@richhickey
Forked from oranenj/scratch.clj
Created February 22, 2010 21:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save richhickey/311542 to your computer and use it in GitHub Desktop.
Save richhickey/311542 to your computer and use it in GitHub Desktop.
;sample use idea:
(defn mapx
"Returns a lazy sequence consisting of the result of applying f to the
set of first items of each coll, followed by applying f to the set
of second items in each coll, until any one of the colls is
exhausted. Any remaining items in other colls are ignored. Function
f should accept number-of-colls arguments."
[f coll]
(gen-iter iter [f seq-cell]
(<< has-item seq-cell)
(f (<< item seq-cell))
(>> move! seq-cell) this
(iter f (make-cell sentry (sequence @seq-cell)))
(iter-seq (iter f (cell (sequence coll))))))
(defmacro gen-iter-fn
[name [op coll] has-item-body item-body move!-body]
`(defn ~name [~op ~coll]
(letfn
[(iter# [~op ~coll]
(reify :as ~'this
~'Iter
(~'has-item [] ~@has-item-body)
(~'item [] ~@item-body)
(~'move! [] ~@move!-body)
~'Transient
(~'value-of []
(reify Editable
(transient-of [sentry#]
(iter# ~op (make-cell sentry# (sequence (deref ~coll)))))))))]
(iter-seq (iter# ~op (cell (sequence ~coll)))))))
(gen-iter-fn filterx [pred coll]
[(loop [cc coll]
(when (and (<< has-item cc) (not (pred (<< item cc))))
(recur (>> move! cc))))
(<< has-item coll)]
[(<< item coll)]
[(>> move! coll) this])
(gen-iter-fn mapx [f coll]
[(<< has-item coll)]
[(f (<< item coll))]
[(>> move! coll) this])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment