Wiki
Download
Manual
Eggs
API
Tests
Bugs
show
edit
history
You can edit this page using
wiki syntax
for markup.
Article contents:
== Outdated egg! This is an egg for CHICKEN 4, the unsupported old release. You're almost certainly looking for [[/eggref/5/lazy-seq|the CHICKEN 5 version of this egg]], if it exists. If it does not exist, there may be equivalent functionality provided by another egg; have a look at the [[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]] == lazy-seq A lazy sequence implementation inspired by [[http://clojure.org/lazy|Clojure's]]. It is an alternative to [[srfi-41|SRFI 41]]. See [[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:]] === API <procedure>(make-lazy-seq body)</procedure> 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: * The empty list to signify the end of the sequence * A pair with the sequence's head in the {{car}} and a {{lazy-seq}} representing the sequence's tail in the {{cdr}} * Another {{lazy-seq}} which will be realized recursively when the sequence is realized <syntax>(lazy-seq body ...)</syntax> Convenience syntax for {{make-lazy-seq}} with {{body ...}} being the thunk's body. <procedure>(lazy-seq? seq)</procedure> Predicate for checking whether {{seq}} is a {{lazy-seq}}. <procedure>(lazy-seq-realized? seq)</procedure> Predicate for checking whether {{seq}} has already been realized. <procedure>(lazy-null? seq)</procedure> Predicate for checking whether {{seq}} is null. Realizes {{seq}}. <procedure>(lazy-list . elements)</procedure> Returns a realized {{lazy-seq}} of {{elements}}. <constant>lazy-null</constant> A null {{lazy-seq}}. <procedure>(lazy-seq->list seq)</procedure> Completely realizes {{seq}} and returns a list of its elements. Should not be called on infinite sequences. <procedure>(list->lazy-seq list)</procedure> Turns {{list}} into a realized {{lazy-seq}}. <procedure>(lazy-head seq)</procedure> Realizes {{seq}} and returns its head. <procedure>(lazy-tail seq)</procedure> Realizes {{seq}} and returns its tail. <procedure>(lazy-length seq)</procedure> Completely realizes {{seq}} and returns its length. Should not be called on infinite sequences. <procedure>(lazy-append seqs ...)</procedure> Returns a {{lazy-seq}} representing the concatenation of {{seqs}}. <procedure>(lazy-concatenate seq)</procedure> Returns a {{lazy-seq}} representing the concatenation of the {{lazy-seq}}s contained in {{seq}}. <procedure>(lazy-append-map proc seqs ...)</procedure> Returns a {{lazy-seq}} which is the concatenation of {{lazy-seq}}s resulting from applying {{proc}} to each element in {{seqs}}. <procedure>(lazy-reverse seq)</procedure> Returns a {{lazy-seq}} of the reversed {{seq}}. Note that even realizing just the head of the returned {{lazy-seq}} will realize {{seq}} completely. Infinite sequences can't be reversed. <procedure>(lazy-take n seq)</procedure> Returns a {{lazy-seq}} of the first {{n}} elements of {{seq}}. <procedure>(lazy-drop n seq)</procedure> Returns a {{lazy-seq}} of all but the first {{n}} elements of {{seq}}. <procedure>(lazy-take-while pred? seq)</procedure> Returns a {{lazy-seq}} of all leading elements of {{seq}} satisfying {{pred?}}. <procedure>(lazy-drop-while pred? seq)</procedure> Returns a {{lazy-seq}} of {{seq}} without all leading elements satisfying {{pred?}}. <procedure>(lazy-ref n seq)</procedure> Realizes {{seq}} up to the {{n}}th element and returns that element. <procedure>(lazy-map proc seqs ...)</procedure> Returns a {{lazy-seq}} of applying {{proc}} to each element in {{seqs}}. Terminates with the sortest of {{seqs}}. <procedure>(lazy-filter pred? seq)</procedure> Returns a {{lazy-seq}} of elements from {{seq}} satisfying {{pred?}}. <procedure>(lazy-each proc seqs ...)</procedure> Completely realizes {{seqs}} and applies {{proc}} to each item for its side-effect. Terminates with the sortest of {{seqs}}. <procedure>(lazy-fold kons init seq)</procedure> Completely realizes {{seq}}, iterating over each value and accumulating a result, similar to {{fold}} from {{srfi-1}}. <procedure>(lazy-flatten seq)</procedure> Returns a {{lazy-seq}} of elements from {{seq}}, expanding any contained {{lazy-seq}} elements. The result is a single flat {{lazy-seq}}. <procedure>(lazy-iterate proc x)</procedure> Returns a {{lazy-seq}} with a head of {{x}} and a tail of applying {{proc}} to the preceding element. <procedure>(lazy-repeat x)</procedure> Returns an infinite {{lazy-seq}} of {{x}}. <procedure>(lazy-repeatedly thunk)</procedure> Returns an infinite {{lazy-seq}} of the return value of {{thunk}} at the time an element is realized. <procedure>(lazy-cycle seq)</procedure> Returns a {{lazy-seq}} which infinitely cycles through {{seq}}. <procedure>(lazy-numbers #!key (step 1) (start 0) count)</procedure> 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. <procedure>(input-port->lazy-seq port read)</procedure> 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. <procedure>(lazy-seq->string seq)</procedure> Completely realizes a {{seq}} of characters and returns a string consisting of those characters. Should not be called on infinite sequences. <procedure>(string->lazy-seq string)</procedure> Turns {{string}} into a realized {{lazy-seq}} of characters. === Examples ==== Defining a custom {{lazy-seq}} <enscript language="scheme"> (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 </enscript> Note that {{odd-numbers}} can be defined more conveniently like this: <enscript language="scheme"> (define odd-numbers (lazy-filter odd? (lazy-numbers))) </enscript> === About this egg ==== Source The [[https://bitbucket.org/DerGuteMoritz/lazy-seq|source code is hosted at Bitbucket]]. Feel free to fork it and send pull requests there. ==== Author [[/users/moritz-heidkamp|Moritz Heidkamp]] ==== Version history ; 2 : First version to be compatible with CHICKEN 5. Various code improvements. ; 0.0.7 : Add lazy-seq->string and string->lazy-seq ; 0.0.6 : Add {{lazy-fold}}, {{lazy-concatenate}} and {{lazy-flatten}} (thanks to Caolan McMahon for the latter two). Make printer realize head. ; 0.0.5 : Fix tests ; 0.0.4 : Add {{lazy-append-map}}, {{lazy-null}} and {{lazy-list}} ; 0.0.3 : Add {{lazy-length}}, {{lazy-reverse}} and {{lazy-cycle}} ; 0.0.2 : Add {{lazy-take-while}} and {{lazy-drop-while}} ; 0.0.1 : Initial release ==== License Copyright (c) 2018, 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.
Description of your changes:
I would like to authenticate
Authentication
Username:
Password:
Spam control
What do you get when you add 9 to 0?