]> gitweb.factorcode.org Git - factor.git/blob - basis/lists/lazy/lazy-docs.factor
Merge commit 'origin/master' into emacs
[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 sequences strings lists ;
4 IN: lists.lazy 
5
6 ABOUT: "lists.lazy"
7
8 ARTICLE: "lists.lazy" "Lazy lists"
9 "The " { $vocab-link "lists.lazy" } " vocabulary implements lazy lists and standard operations to manipulate them."
10 { $subsection { "lists.lazy" "construction" } }
11 { $subsection { "lists.lazy" "manipulation" } }
12 { $subsection { "lists.lazy" "combinators" } }
13 { $subsection { "lists.lazy" "io" } } ;
14
15 ARTICLE: { "lists.lazy" "combinators" } "Combinators for manipulating lazy lists"
16 "The following combinators create lazy lists from other lazy lists:"
17 { $subsection lmap }
18 { $subsection lfilter }
19 { $subsection luntil }
20 { $subsection lwhile }
21 { $subsection lfrom-by }
22 { $subsection lcomp }
23 { $subsection lcomp* } ;
24
25 ARTICLE: { "lists.lazy" "io" } "Lazy list I/O"
26 "Input from a stream can be read through a lazy list, using the following words:"
27 { $subsection lcontents }
28 { $subsection llines } ;
29
30 ARTICLE: { "lists.lazy" "construction" } "Constructing lazy lists"
31 "Words for constructing lazy lists:"
32 { $subsection lazy-cons }
33 { $subsection 1lazy-list }
34 { $subsection 2lazy-list }
35 { $subsection 3lazy-list }
36 { $subsection seq>list }
37 { $subsection >list }
38 { $subsection lfrom } ;
39
40 ARTICLE: { "lists.lazy" "manipulation" } "Manipulating lazy lists"
41 "To make new lazy lists from old ones:"
42 { $subsection <memoized-cons> }
43 { $subsection lappend }
44 { $subsection lconcat }
45 { $subsection lcartesian-product }
46 { $subsection lcartesian-product* }
47 { $subsection lmerge }
48 { $subsection ltake } ;
49
50 HELP: lazy-cons
51 { $values { "car" { $quotation "( -- elt )" } } { "cdr" { $quotation "( -- cons )" } } { "promise" "the resulting cons object" } }
52 { $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." } 
53 { $see-also cons car cdr nil nil? } ;
54
55 { 1lazy-list 2lazy-list 3lazy-list } related-words
56
57 HELP: 1lazy-list
58 { $values { "a" { $quotation "( -- X )" } } { "lazy-cons" "a lazy-cons object" } }
59 { $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." } ;
60
61 HELP: 2lazy-list
62 { $values { "a" { $quotation "( -- X )" } } { "b" { $quotation "( -- X )" } } { "lazy-cons" "a lazy-cons object" } }
63 { $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." } ;
64
65 HELP: 3lazy-list
66 { $values { "a" { $quotation "( -- X )" } } { "b" { $quotation "( -- X )" } } { "c" { $quotation "( -- X )" } } { "lazy-cons" "a lazy-cons object" } }
67 { $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." } ;
68
69 HELP: <memoized-cons>
70 { $values { "cons" "a cons object" } { "memoized-cons" "the resulting memoized-cons object" } }
71 { $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." } 
72 { $see-also cons car cdr nil nil? } ;
73
74 { lazy-map ltake lfilter lappend lfrom lfrom-by lconcat lcartesian-product lcartesian-product* lcomp lcomp* lmerge lwhile luntil } related-words
75
76 HELP: lazy-map
77 { $values { "list" "a cons object" } { "quot" { $quotation "( obj -- X )" } } { "result" "resulting cons object" } }
78 { $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." } ;
79
80 HELP: ltake
81 { $values { "n" "a non negative integer" } { "list" "a cons object" } { "result" "resulting cons object" } }
82 { $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." } ;
83
84 HELP: lfilter
85 { $values { "list" "a cons object" } { "quot" { $quotation "( -- X )" } } { "result" "resulting cons object" } }
86 { $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." } ;
87
88 HELP: lwhile
89 { $values { "list" "a cons object" } { "quot" { $quotation "( X -- ? )" } } { "result" "resulting cons object" } }
90 { $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." } ;
91
92 HELP: luntil
93 { $values { "list" "a cons object" } { "quot" { $quotation "( X -- ? )" } } { "result" "resulting cons object" } }
94 { $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." } ;
95
96 HELP: list>vector
97 { $values { "list" "a cons object" } { "vector" "the list converted to a vector" } }
98 { $description "Convert a list to a vector. If the list is a lazy infinite list then this will enter an infinite loop." } 
99 { $see-also list>array } ;
100
101 HELP: list>array
102 { $values { "list" "a cons object" } { "array" "the list converted to an array" } }
103 { $description "Convert a list to an array. If the list is a lazy infinite list then this will enter an infinite loop." } 
104 { $see-also list>vector } ;
105
106 HELP: lappend
107 { $values { "list1" "a cons object" } { "list2" "a cons object" } { "result" "a lazy list of list2 appended to list1" } }
108 { $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." } ;
109
110 HELP: lfrom-by
111 { $values { "n" "an integer" } { "quot" { $quotation "( -- int )" } } { "list" "a lazy list of integers" } }
112 { $description "Return an infinite lazy list of values starting from n, with each successive value being the result of applying quot to n." } ;
113
114 HELP: lfrom
115 { $values { "n" "an integer" } { "list" "a lazy list of integers" } }
116 { $description "Return an infinite lazy list of incrementing integers starting from n." } ;
117
118 HELP: seq>list
119 { $values { "index" "an integer 0 or greater" } { "seq" "a sequence" } { "list" "a list" } }
120 { $description "Convert the sequence into a list, starting from the 'index' offset into the sequence." } 
121 { $see-also >list } ;
122
123 HELP: >list
124 { $values { "object" "an object" } { "list" "a list" } }
125 { $description "Convert the object into a list. Existing lists are passed through intact, sequences are converted using " { $link seq>list } " and other objects cause an error to be thrown." } 
126 { $see-also seq>list } ;
127     
128 { leach foldl lazy-map ltake lfilter lappend lfrom lfrom-by lconcat lcartesian-product lcartesian-product* lcomp lcomp* lmerge lwhile luntil } related-words
129
130 HELP: lconcat
131 { $values { "list" "a list of lists" } { "result" "a list" } }
132 { $description "Concatenates a list of lists together into one list." } ;
133
134 HELP: lcartesian-product
135 { $values { "list1" "a list" } { "list2" "a list" } { "result" "list of cartesian products" } }
136 { $description "Given two lists, return a list containing the cartesian product of those lists." } ;
137
138 HELP: lcartesian-product*
139 { $values { "lists" "a list of lists" } { "result" "list of cartesian products" } }
140 { $description "Given a list of lists, return a list containing the cartesian product of those lists." } ;
141
142 HELP: lcomp
143 { $values { "list" "a list of lists" } { "quot" { $quotation "( seq -- X )" } } { "result" "the resulting list" } }
144 { $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" } "." } ;
145
146 HELP: lcomp*
147 { $values { "list" "a list of lists" } { "guards" "a sequence of quotations with stack effect ( seq -- bool )" } { "quot" { $quotation "( seq -- X )" } } { "list" "the resulting list" } { "result" "a list" } }
148 { $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" } "." }
149 { $examples
150   { $code "{ 1 2 3 } >list { 4 5 6 } >list 2list { [ first odd? ] } [ first2 + ] lcomp*" }
151 } ;
152
153 HELP: lmerge
154 { $values { "list1" "a list" } { "list2" "a list" } { "result" "lazy list merging list1 and list2" } }
155 { $description "Return the result of merging the two lists in a lazy manner." } 
156 { $examples
157   { $example "USING: lists.lazy prettyprint ;" "{ 1 2 3 } >list { 4 5 6 } >list lmerge list>array ." "{ 1 4 2 5 3 6 }" }
158 } ;
159
160 HELP: lcontents
161 { $values { "stream" "a stream" } { "result" string } }
162 { $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." } 
163 { $see-also llines } ;
164
165 HELP: llines
166 { $values { "stream" "a stream" } { "result" "a list" } }
167 { $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." } 
168 { $see-also lcontents } ;