((section 2 "Outdated egg!" (p "This is an egg for CHICKEN 4, the unsupported old release.  You're almost certainly looking for " (int-link "/eggref/5/lazy-seq" "the CHICKEN 5 version of this egg") ", if it exists.") (p "If it does not exist, there may be equivalent functionality provided by another egg; have a look at the " (link "https://wiki.call-cc.org/chicken-projects/egg-index-5.html" "egg index") ". Otherwise, please consider porting this egg to the current version of CHICKEN.") (tags "egg")) (section 2 "lazy-seq" (p "A lazy sequence implementation inspired by " (link "http://clojure.org/lazy" "Clojure's") ". It is an alternative to " (int-link "srfi-41" "SRFI 41") ". See " (link "http://ceaude.twoticketsplease.de/articles/lazy-sequences-in-scheme.html" "this article for the motivation of creating this egg and a comparison with SRFI 41") ".") (toc) (section 3 "API" (def (sig (procedure "(make-lazy-seq body)" (id make-lazy-seq))) (p "Returns a " (tt "lazy-seq") " object. " (tt "body") " is a thunk which will be called when the sequence is realized. It is expected to return one of the following things:") (ul (li "The empty list to signify the end of the sequence") (li "A pair with the sequence's head in the " (tt "car") " and a " (tt "lazy-seq") " representing the sequence's tail  in the " (tt "cdr")) (li "Another " (tt "lazy-seq") " which will be realized recursively when the sequence is realized"))) (def (sig (syntax "(lazy-seq body ...)" (id lazy-seq))) (p "Convenience syntax for " (tt "make-lazy-seq") " with " (tt "body ...") " being the thunk's body.")) (def (sig (procedure "(lazy-seq? seq)" (id lazy-seq?))) (p "Predicate for checking whether " (tt "seq") " is a " (tt "lazy-seq") ".")) (def (sig (procedure "(lazy-seq-realized? seq)" (id lazy-seq-realized?))) (p "Predicate for checking whether " (tt "seq") " has already been realized.")) (def (sig (procedure "(lazy-null? seq)" (id lazy-null?))) (p "Predicate for checking whether " (tt "seq") " is null. Realizes " (tt "seq") ".")) (def (sig (procedure "(lazy-list . elements)" (id lazy-list))) (p "Returns a realized " (tt "lazy-seq") " of " (tt "elements") ".")) (def (sig (constant "lazy-null" (id lazy-null))) (p "A null " (tt "lazy-seq") ".")) (def (sig (procedure "(lazy-seq->list seq)" (id lazy-seq->list))) (p "Completely realizes " (tt "seq") " and returns a list of its elements. Should not be called on infinite sequences.")) (def (sig (procedure "(list->lazy-seq list)" (id list->lazy-seq))) (p "Turns " (tt "list") " into a realized " (tt "lazy-seq") ".")) (def (sig (procedure "(lazy-head seq)" (id lazy-head))) (p "Realizes " (tt "seq") " and returns its head.")) (def (sig (procedure "(lazy-tail seq)" (id lazy-tail))) (p "Realizes " (tt "seq") " and returns its tail.")) (def (sig (procedure "(lazy-length seq)" (id lazy-length))) (p "Completely realizes " (tt "seq") " and returns its length. Should not be called on infinite sequences.")) (def (sig (procedure "(lazy-append seqs ...)" (id lazy-append))) (p "Returns a " (tt "lazy-seq") " representing the concatenation of " (tt "seqs") ".")) (def (sig (procedure "(lazy-concatenate seq)" (id lazy-concatenate))) (p "Returns a " (tt "lazy-seq") " representing the concatenation of the " (tt "lazy-seq") "s contained in " (tt "seq") ".")) (def (sig (procedure "(lazy-append-map proc seqs ...)" (id lazy-append-map))) (p "Returns a " (tt "lazy-seq") " which is the concatenation of " (tt "lazy-seq") "s resulting from applying " (tt "proc") " to each element in " (tt "seqs") ".")) (def (sig (procedure "(lazy-reverse seq)" (id lazy-reverse))) (p "Returns a " (tt "lazy-seq") " of the reversed " (tt "seq") ". Note that even realizing just the head of the returned " (tt "lazy-seq") " will realize " (tt "seq") " completely. Infinite sequences can't be reversed.")) (def (sig (procedure "(lazy-take n seq)" (id lazy-take))) (p "Returns a " (tt "lazy-seq") " of the first " (tt "n") " elements of " (tt "seq") ".")) (def (sig (procedure "(lazy-drop n seq)" (id lazy-drop))) (p "Returns a " (tt "lazy-seq") " of all but the first " (tt "n") " elements of " (tt "seq") ".")) (def (sig (procedure "(lazy-take-while pred? seq)" (id lazy-take-while))) (p "Returns a " (tt "lazy-seq") " of all leading elements of " (tt "seq") " satisfying " (tt "pred?") ".")) (def (sig (procedure "(lazy-drop-while pred? seq)" (id lazy-drop-while))) (p "Returns a " (tt "lazy-seq") " of " (tt "seq") " without all leading elements satisfying " (tt "pred?") ".")) (def (sig (procedure "(lazy-ref n seq)" (id lazy-ref))) (p "Realizes " (tt "seq") " up to the " (tt "n") "th element and returns that element.")) (def (sig (procedure "(lazy-map proc seqs ...)" (id lazy-map))) (p "Returns a " (tt "lazy-seq") " of applying " (tt "proc") " to each element in " (tt "seqs") ". Terminates with the sortest of " (tt "seqs") ".")) (def (sig (procedure "(lazy-filter pred? seq)" (id lazy-filter))) (p "Returns a " (tt "lazy-seq") " of elements from " (tt "seq") " satisfying " (tt "pred?") ".")) (def (sig (procedure "(lazy-each proc seqs ...)" (id lazy-each))) (p "Completely realizes " (tt "seqs") " and applies " (tt "proc") " to each item for its side-effect. Terminates with the sortest of " (tt "seqs") ".")) (def (sig (procedure "(lazy-fold kons init seq)" (id lazy-fold))) (p "Completely realizes " (tt "seq") ", iterating over each value and accumulating a result, similar to " (tt "fold") " from " (tt "srfi-1") ".")) (def (sig (procedure "(lazy-flatten seq)" (id lazy-flatten))) (p "Returns a " (tt "lazy-seq") " of elements from " (tt "seq") ", expanding any contained " (tt "lazy-seq") " elements. The result is a single flat " (tt "lazy-seq") ".")) (def (sig (procedure "(lazy-iterate proc x)" (id lazy-iterate))) (p "Returns a " (tt "lazy-seq") " with a head of " (tt "x") " and a tail of applying " (tt "proc") " to the preceding element.")) (def (sig (procedure "(lazy-repeat x)" (id lazy-repeat))) (p "Returns an infinite " (tt "lazy-seq") " of " (tt "x") ".")) (def (sig (procedure "(lazy-repeatedly thunk)" (id lazy-repeatedly))) (p "Returns an infinite " (tt "lazy-seq") " of the return value of " (tt "thunk") " at the time an element is realized.")) (def (sig (procedure "(lazy-cycle seq)" (id lazy-cycle))) (p "Returns a " (tt "lazy-seq") " which infinitely cycles through " (tt "seq") ".")) (def (sig (procedure "(lazy-numbers #!key (step 1) (start 0) count)" (id lazy-numbers))) (p "Returns a " (tt "lazy-seq") " of numbers starting at " (tt "start") " and increasing by " (tt "step") ". When " (tt "count") " is a positive number the sequence terminates after " (tt "count") " elements. Otherwise it is infinte.")) (def (sig (procedure "(input-port->lazy-seq port read)" (id input-port->lazy-seq))) (p "Returns a " (tt "lazy-seq") " of the results of applying " (tt "read") " to " (tt "port") " (which must be an input port). The sequence terminates when " (tt "read") " returns " (tt "#!eof") ". The " (tt "port") " is not closed automatically by this procedure.")) (def (sig (procedure "(lazy-seq->string seq)" (id lazy-seq->string))) (p "Completely realizes a " (tt "seq") " of characters and returns a string consisting of those characters. Should not be called on infinite sequences.")) (def (sig (procedure "(string->lazy-seq string)" (id string->lazy-seq))) (p "Turns " (tt "string") " into a realized " (tt "lazy-seq") " of characters."))) (section 3 "Examples" (section 4 ("Defining a custom " (tt "lazy-seq")) (highlight scheme "(define odd-numbers\n  (let next ((n 0))\n    (lazy-seq\n      (if (odd? n)\n        (cons n (next (+ n 1)))\n        (next (+ n 1))))))\n\n(lazy-ref 5 odd-numbers) ; => 11") (p "Note that " (tt "odd-numbers") " can be defined more conveniently like this:") (highlight scheme "(define odd-numbers\n  (lazy-filter odd? (lazy-numbers)))"))) (section 3 "About this egg" (section 4 "Source" (p "The " (link "https://bitbucket.org/DerGuteMoritz/lazy-seq" "source code is hosted at Bitbucket") ". Feel free to fork it and send pull requests there.")) (section 4 "Author" (p (int-link "/users/moritz-heidkamp" "Moritz Heidkamp"))) (section 4 "Version history" (dl (dt "2") (dd "First version to be compatible with CHICKEN 5. Various code improvements.") (dt "0.0.7") (dd "Add lazy-seq->string and string->lazy-seq") (dt "0.0.6") (dd "Add " (tt "lazy-fold") ", " (tt "lazy-concatenate") " and " (tt "lazy-flatten") " (thanks to Caolan McMahon for the latter two). Make printer realize head.") (dt "0.0.5") (dd "Fix tests") (dt "0.0.4") (dd "Add " (tt "lazy-append-map") ", " (tt "lazy-null") " and " (tt "lazy-list")) (dt "0.0.3") (dd "Add " (tt "lazy-length") ", " (tt "lazy-reverse") " and " (tt "lazy-cycle")) (dt "0.0.2") (dd "Add " (tt "lazy-take-while") " and " (tt "lazy-drop-while")) (dt "0.0.1") (dd "Initial release"))) (section 4 "License" (pre " Copyright (c) 2018, Moritz Heidkamp\n All rights reserved.\n \n Redistribution and use in source and binary forms, with or without\n modification, are permitted provided that the following conditions are\n met:\n \n Redistributions of source code must retain the above copyright\n notice, this list of conditions and the following disclaimer.\n \n Redistributions in binary form must reproduce the above copyright\n notice, this list of conditions and the following disclaimer in the\n documentation and/or other materials provided with the distribution.\n \n Neither the name of the author nor the names of its contributors may\n be used to endorse or promote products derived from this software\n without specific prior written permission.\n \n THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS\n FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE\n COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,\n INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\n (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR\n SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)\n HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,\n STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED\n OF THE POSSIBILITY OF SUCH DAMAGE.")))))