A lazy sequence implementation inspired by Clojure's. It is an alternative to SRFI 41. See this article for the motivation of creating this egg and a comparison with SRFI 41.
- make-lazy-seq bodyprocedure
Returns a lazy-seq object. body is a thunk which will be called when the sequence is realized. It is expected to return one of the following things:
- (lazy-seq body ...)syntax
Convenience syntax for make-lazy-seq with body ... being the thunk's body.
- lazy-seq-realized? seqprocedure
Predicate for checking whether seq has already been realized.
- lazy-null? seqprocedure
Predicate for checking whether seq is null. Realizes seq.
- lazy-seq->list seqprocedure
Completely realizes seq and returns a list of its elements. Should not be called on infinite sequences.
- lazy-head seqprocedure
Realizes seq and returns its head.
- lazy-tail seqprocedure
Realizes seq and returns its tail.
- lazy-length seqprocedure
Completely realizes seq and returns its length. Should not be called on infinite sequences.
- lazy-append-map proc seqs ...procedure
- lazy-reverse seqprocedure
- lazy-take-while pred? seqprocedure
Returns a lazy-seq of all leading elements of seq satisfying pred?.
- lazy-drop-while pred? seqprocedure
Returns a lazy-seq of seq without all leading elements satisfying pred?.
- lazy-ref n seqprocedure
Realizes seq up to the nth element and returns that element.
- lazy-map proc seqs ...procedure
Returns a lazy-seq of applying proc to each element in seqs. Terminates with the sortest of seqs.
- lazy-each proc seqs ...procedure
Completely realizes seqs and applies proc to each item for its side-effect. Terminates with the sortest of seqs.
- lazy-fold kons init seqprocedure
Completely realizes seq, iterating over each value and accumulating a result, similar to fold from srfi-1.
- lazy-flatten seqprocedure
- lazy-iterate proc xprocedure
Returns a lazy-seq with a head of x and a tail of applying proc to the preceding element.
- lazy-repeatedly thunkprocedure
Returns an infinite lazy-seq of the return value of thunk at the time an element is realized.
- lazy-numbers #!key (step 1) (start 0) countprocedure
Returns a lazy-seq of numbers starting at start and increasing by step. When count is a positive number the sequence terminates after count elements. Otherwise it is infinte.
- input-port->lazy-seq port readprocedure
Returns a lazy-seq of the results of applying read to port (which must be an input port). The sequence terminates when read returns #!eof. The port is not closed automatically by this procedure.
(define odd-numbers (let next ((n 0)) (lazy-seq (if (odd? n) (cons n (next (+ n 1))) (next (+ n 1)))))) (lazy-ref 5 odd-numbers) ; => 11
Note that odd-numbers can be defined more conveniently like this:
(define odd-numbers (lazy-filter odd? (lazy-numbers)))
The source code is hosted at Bitbucket. Feel free to fork it and send pull requests there.
- Add lazy-length, lazy-reverse and lazy-cycle
- Add lazy-take-while and lazy-drop-while
- Initial release
Copyright (c) 2012, Moritz Heidkamp All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. Neither the name of the author nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.