Skip to content

Instantly share code, notes, and snippets.

@mecdemort
Created March 18, 2011 18:38
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 mecdemort/876598 to your computer and use it in GitHub Desktop.
Save mecdemort/876598 to your computer and use it in GitHub Desktop.
;;https://github.com/naleksander/instrumentos/blob/master/src/instrumentos/yield.clj
(defn funnel []
(let [q (java.util.concurrent.SynchronousQueue.)]
[(take-while (partial not= q)
(repeatedly #(.take q)))
(fn
([e] (.put q e))
([] (.put q q)))]))
(defmacro bound-future[& body]
`(future-call (bound-fn [] ~@body)))
(declare yield)
(defmacro yieldish[& content]
`(let[[s# f#] (funnel)]
(binding [yield f#]
(bound-future (do ~@content (yield))) s#)))
(def fibs
(yieldish
(loop [a 0 b 1]
(yield a)
(recur b (+ a b)))))
user> (take 10 fibs)
(0 1 1 2 3 5 8 13 21 34)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment