]> gitweb.factorcode.org Git - factor.git/blob - basis/lists/lazy/lazy-docs.factor
fa43da2186b2bd211a896301c7f7f6523c409756
[factor.git] / basis / lists / lazy / lazy-docs.factor
1 ! Copyright (C) 2006 Chris Double.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: help.markup help.syntax kernel lists math sequences
4 strings ;
5 IN: lists.lazy
6
7 ABOUT: "lists.lazy"
8
9 ARTICLE: "lists.lazy" "Lazy lists"
10 "The " { $vocab-link "lists.lazy" } " vocabulary implements lazy lists and standard operations to manipulate them."
11 { $subsections
12     { "lists.lazy" "construction" }
13     { "lists.lazy" "manipulation" }
14     { "lists.lazy" "combinators" }
15     { "lists.lazy" "io" }
16 } ;
17
18 ARTICLE: { "lists.lazy" "combinators" } "Combinators for manipulating lazy lists"
19 "The following combinators create lazy lists from other lazy lists:"
20 { $subsections
21     lmap-lazy
22     lfilter
23     luntil
24     lwhile
25     lfrom-by
26     lcartesian-map
27     lcartesian-map*
28 } ;
29
30 ARTICLE: { "lists.lazy" "io" } "Lazy list I/O"
31 "Input from a stream can be read through a lazy list, using the following words:"
32 { $subsections
33     lcontents
34     llines
35 } ;
36
37 ARTICLE: { "lists.lazy" "construction" } "Constructing lazy lists"
38 "Words for constructing lazy lists:"
39 { $subsections
40     lazy-cons
41     1lazy-list
42     2lazy-list
43     3lazy-list
44     sequence-tail>list
45     >list
46     lfrom
47 } ;
48
49 ARTICLE: { "lists.lazy" "manipulation" } "Manipulating lazy lists"
50 "To make new lazy lists from old ones:"
51 { $subsections
52     <memoized-cons>
53     lappend-lazy
54     lconcat
55     lcartesian-product
56     lcartesian-product*
57     lmerge
58     ltake
59 } ;
60
61 HELP: lazy-cons
62 { $values { "car" { $quotation ( -- elt ) } } { "cdr" { $quotation ( -- cons ) } } { "promise" "the resulting cons object" } }
63 { $description "Constructs a cons object for a lazy list from two quotations. The " { $snippet "car" } " quotation should return the head of the list, and the " { $snippet "cons" } " quotation the tail when called. When " { $link cons } " or " { $link cdr } " are called on the lazy-cons object then the appropriate quotation is called." }
64 { $see-also cons car cdr nil nil? } ;
65
66 { 1lazy-list 2lazy-list 3lazy-list } related-words
67
68 HELP: 1lazy-list
69 { $values { "a" { $quotation ( -- X ) } } { "lazy-cons" "a lazy-cons object" } }
70 { $description "Create a lazy list with 1 element. The element is the result of calling the quotation. The quotation is only called when the list element is requested." } ;
71
72 HELP: 2lazy-list
73 { $values { "a" { $quotation ( -- X ) } } { "b" { $quotation ( -- X ) } } { "lazy-cons" "a lazy-cons object" } }
74 { $description "Create a lazy list with 2 elements. The elements are the result of calling the quotations. The quotations are only called when the list elements are requested." } ;
75
76 HELP: 3lazy-list
77 { $values { "a" { $quotation ( -- X ) } } { "b" { $quotation ( -- X ) } } { "c" { $quotation ( -- X ) } } { "lazy-cons" "a lazy-cons object" } }
78 { $description "Create a lazy list with 3 elements. The elements are the result of calling the quotations. The quotations are only called when the list elements are requested." } ;
79
80 HELP: <memoized-cons>
81 { $values { "cons" "a cons object" } { "memoized-cons" "the resulting memoized-cons object" } }
82 { $description "Constructs a cons object that wraps an existing cons object. Requests for the car, cdr and nil? will be remembered after the first call, and the previous result returned on subsequent calls." }
83 { $see-also cons car cdr nil nil? } ;
84
85 { lmap-lazy ltake lfilter lappend-lazy lfrom lfrom-by lconcat lcartesian-product lcartesian-product* lcartesian-map lcartesian-map* lmerge lwhile luntil } related-words
86
87 HELP: lmap-lazy
88 { $values { "list" "a cons object" } { "quot" { $quotation ( obj -- X ) } } { "result" "resulting cons object" } }
89 { $description "Perform a similar functionality to that of the " { $link map } " word, but in a lazy manner. No evaluation of the list elements occurs initially but a " { $link lazy-map } " object is returned which conforms to the list protocol. Calling " { $link car } ", " { $link cdr } " or " { $link nil? } " on this will evaluate elements as required." } ;
90
91 HELP: ltake
92 { $values { "n" "a non negative integer" } { "list" "a cons object" } { "result" "resulting cons object" } }
93 { $description "Outputs a lazy list containing the first n items in the list. This is done a lazy manner. No evaluation of the list elements occurs initially but a " { $link lazy-take } " object is returned which conforms to the list protocol. Calling " { $link car } ", " { $link cdr } " or " { $link nil? } " on this will evaluate elements as required." } ;
94
95 HELP: lfilter
96 { $values { "list" "a cons object" } { "quot" { $quotation ( elt -- ? ) } } { "result" "resulting cons object" } }
97 { $description "Perform a similar functionality to that of the " { $link filter } " word, but in a lazy manner. No evaluation of the list elements occurs initially but a " { $link lazy-filter } " object is returned which conforms to the list protocol. Calling " { $link car } ", " { $link cdr } " or " { $link nil? } " on this will evaluate elements as required." } ;
98
99 HELP: lwhile
100 { $values { "list" "a cons object" } { "quot" { $quotation ( elt -- ? ) } } { "result" "resulting cons object" } }
101 { $description "Outputs a lazy list containing the first items in the list as long as " { $snippet "quot" } " evaluates to t. No evaluation of the list elements occurs initially but a " { $link lazy-while } " object is returned with conforms to the list protocol. Calling " { $link car } ", " { $link cdr } " or " { $link nil? } " on this will evaluate elements as required." } ;
102
103 HELP: luntil
104 { $values { "list" "a cons object" } { "quot" { $quotation ( elt -- ? ) } } { "result" "resulting cons object" } }
105 { $description "Outputs a lazy list containing the first items in the list until after " { $snippet "quot" } " evaluates to t. No evaluation of the list elements occurs initially but a " { $link lazy-while } " object is returned with conforms to the list protocol. Calling " { $link car } ", " { $link cdr } " or " { $link nil? } " on this will evaluate elements as required." } ;
106
107 HELP: lappend-lazy
108 { $values { "list1" "a cons object" } { "list2" "a cons object" } { "result" "a lazy list of list2 appended to list1" } }
109 { $description "Perform a similar functionality to that of the " { $link append } " word, but in a lazy manner. No evaluation of the list elements occurs initially but a " { $link lazy-append } " object is returned which conforms to the list protocol. Calling " { $link car } ", " { $link cdr } " or " { $link nil? } " on this will evaluate elements as required. Successive calls to " { $link cdr } " will iterate through list1, followed by list2." } ;
110
111 HELP: lfrom-by
112 { $values { "n" integer } { "quot" { $quotation ( n -- o ) } } { "result" "a lazy list of integers" } }
113 { $description "Return an infinite lazy list of values starting from n, with each successive value being the result of applying quot to the previous value." } ;
114
115 HELP: lfrom
116 { $values { "n" integer } { "result" "a lazy list of integers" } }
117 { $description "Return an infinite lazy list of incrementing integers starting from n." } ;
118
119 HELP: sequence-tail>list
120 { $values { "index" "an integer 0 or greater" } { "seq" sequence } { "list" "a list" } }
121 { $description "Convert the sequence into a list, starting from " { $snippet "index" } "." }
122 { $see-also >list } ;
123
124 { leach foldl lmap-lazy ltake lfilter lappend-lazy lfrom lfrom-by lconcat lcartesian-product lcartesian-product* lcartesian-map lcartesian-map* lmerge lwhile luntil } related-words
125
126 HELP: lconcat
127 { $values { "list" "a list of lists" } { "result" "a list" } }
128 { $description "Concatenates a list of lists together into one list." } ;
129
130 HELP: lcartesian-product
131 { $values { "list1" "a list" } { "list2" "a list" } { "result" "list of cartesian products" } }
132 { $description "Given two lists, return a list containing the cartesian product of those lists." } ;
133
134 HELP: lcartesian-product*
135 { $values { "lists" "a list of lists" } { "result" "list of cartesian products" } }
136 { $description "Given a list of lists, return a list containing the cartesian product of those lists." } ;
137
138 HELP: lcartesian-map
139 { $values { "list" "a list of lists" } { "quot" { $quotation ( elt1 elt2 -- newelt ) } } { "result" "the resulting list" } }
140 { $description "Get the cartesian product of the lists in " { $snippet "list" } " and call " { $snippet "quot" } " call with each element from the cartesian product on the stack, the result of which is returned in the final " { $snippet "list" } "." } ;
141
142 HELP: lcartesian-map*
143 { $values { "list" "a list of lists" } { "guards" "a sequence of quotations with stack effect ( elt1 elt2 -- ? )" } { "quot" { $quotation ( elt1 elt2 -- newelt ) } } { "result" "a list" } }
144 { $description "Get the cartesian product of the lists in " { $snippet "list" } ", filter it by applying each guard quotation to it and call " { $snippet "quot" } " call with each element from the remaining cartesian product items on the stack, the result of which is returned in the final " { $snippet "list" } "." }
145 { $examples
146   { $code "{ 1 2 3 } >list { 4 5 6 } >list 2list { [ drop odd? ] } [ + ] lcartesian-map*" }
147 } ;
148
149 HELP: lmerge
150 { $values { "list1" "a list" } { "list2" "a list" } { "result" "lazy list merging list1 and list2" } }
151 { $description "Return the result of merging the two lists in a lazy manner." }
152 { $examples
153   { $example "USING: lists lists.lazy prettyprint ;" "{ 1 2 3 } >list { 4 5 6 } >list lmerge list>array ." "{ 1 4 2 5 3 6 }" }
154 } ;
155
156 HELP: lcontents
157 { $values { "stream" "a stream" } { "result" string } }
158 { $description "Returns a lazy list of all characters in the file. " { $link car } " returns the next character in the file, " { $link cdr } " returns the remaining characters as a lazy list. " { $link nil? } " indicates end of file." }
159 { $see-also llines } ;
160
161 HELP: llines
162 { $values { "stream" "a stream" } { "result" "a list" } }
163 { $description "Returns a lazy list of all lines in the file. " { $link car } " returns the next lines in the file, " { $link cdr } " returns the remaining lines as a lazy list. " { $link nil? } " indicates end of file." }
164 { $see-also lcontents } ;