All Projects → erdos → erdos.yield

erdos / erdos.yield

Licence: EPL-2.0 license
lazy-seq generator in clojure

Programming Languages

clojure
4091 projects

erdos.yield

Python's generator/yield feature in Clojure and ClojureScript to generate lazy sequences.

How is it different from clj-generators and clj-yield? It is much simpler with lower overhead, because it does not depend on core.async or threading in the background. It does simple term rewriting to change imperative style code to functional sequence compositions.

Clojars Project contributions welcome HitCount EPL 2.0

Usage

Use the (gen-seq) macro to create a lazy sequence. Invocations of (yield) in the macro sets the next return value of the sequence.

For example, (gen-seq (yield 1) (yield 2)) will return (1 2). A more complex example:

(gen-seq
 (dotimes [i 3]
   (yield i))
 (yield "Hello")
 (doseq [c "Greetings"]
   (yield c))
 (loop [i 3]
   (when (pos? i)
     (yield i)
     (recur (dec i))))
 (yield "End."))

Returns:

(0 1 2 "Hello" \G \r \e \e \t \i \n \g \s \3 \2 \1 "End.")

See the test cases for more examples.

Use (yield-all xs) instead of (doseq [x xs] (yield x)) to set multiple elements in the sequence.

License

Copyright © 2020 Janos Erdos

This program and the accompanying materials are made available under the terms of the Eclipse Public License 2.0 which is available at http://www.eclipse.org/legal/epl-2.0.

This Source Code may also be made available under the following Secondary Licenses when the conditions for such availability set forth in the Eclipse Public License, v. 2.0 are satisfied: GNU General Public License as published by the Free Software Foundation, either version 2 of the License, or (at your option) any later version, with the GNU Classpath Exception which is available at https://www.gnu.org/software/classpath/license.html.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].