Join GitHub today
GitHub is home to over 20 million developers working together to host and review code, manage projects, and build software together.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
|Failed to load latest commit information.|
Clj-yield provides functionality similar to Python's yield statement. For example: (def *sequence* (with-yielding [out 5] (loop [x 10] (if (pos? x) (do (yield out x) (recur (dec x))))))) *sequence* => (10 9 8 7 6 5 4 3 2 1) With-yielding will run its body in a background thread and return a lazy sequence of the yielded results. You can specify how far the producer thread can get ahead of the consumers (5 above). The yield function will block if the producer gets too far ahead. This allows you to make arbitrary non-lazy code operate in a lazy manner. If the lazy sequence ever becomes garbage collectable, the yield function will throw a java.lang.InterruptedException. The body of the with-yielding should return. I wrote 'yield' for a couple different reasons: 1) I wanted some code to generate a lazy sequence, but it was cumbersome to write it using lazy-seq. 'Yield' allows me to write it as a simple loop. 2) I wanted to use transient data structures while calculating a lazy sequence. Since 'yield' computes its output using a (single) background thread, this isn't a problem. (Note: you can still spawn new threads inside the body of a with-yielding.) 3) I wanted to move a time consuming operation into another thread. Lazy sequences acting as buffered queues are great at sychronizing producer and consumer threads.