Clojure and me has moved.

Monday, June 23, 2008

Lazier than lazy

This post has moved, go to its new location
If you can't put off what you have to do, ask someone else to do it for you.
(defn off
"Computes a lazy-seq in another (dedicated) thread."
[s]
(let [ex (. java.util.concurrent.Executors newSingleThreadExecutor)]
((fn this [s]
(if s
(let [future-rest (java.util.concurrent.FutureTask. #(rest s))]
(.execute ex future-rest)
(lazy-cons (first s) (this (.get future-rest))))
(.shutdown ex))) s)))

No comments: