]> gitweb.factorcode.org Git - factor.git/blob - core/sequences/sequences-docs.factor
move if-zero etc to math, remove 1-/1+ from math
[factor.git] / core / sequences / sequences-docs.factor
1 USING: arrays help.markup help.syntax math
2 sequences.private vectors strings kernel math.order layouts
3 quotations generic.single ;
4 IN: sequences
5
6 HELP: sequence
7 { $class-description "A mixin class whose instances are sequences. Custom implementations of the sequence protocol should be declared as instances of this mixin for all sequence functionality to work correctly:"
8     { $code "INSTANCE: my-sequence sequence" }
9 } ;
10
11 HELP: length
12 { $values { "seq" sequence } { "n" "a non-negative integer" } }
13 { $contract "Outputs the length of the sequence. All sequences support this operation." } ;
14
15 HELP: set-length
16 { $values { "n" "a non-negative integer" } { "seq" "a resizable sequence" } }
17 { $contract "Resizes a sequence. The initial contents of the new area is undefined." }
18 { $errors "Throws a " { $link no-method  } " error if the sequence is not resizable, and a " { $link bounds-error } " if the new length is negative." }
19 { $side-effects "seq" } ;
20
21 HELP: lengthen
22 { $values { "n" "a non-negative integer" } { "seq" "a resizable sequence" } }
23 { $contract "Ensures the sequence has a length of at least " { $snippet "n" } " elements. This word differs from " { $link set-length } " in two respects:"
24     { $list
25         { "This word does not shrink the sequence if " { $snippet "n" } " is less than its length." }
26         { "The word doubles the underlying storage of " { $snippet "seq" } ", whereas " { $link set-length } " is permitted to set it to equal " { $snippet "n" } ". This ensures that repeated calls to this word with constant increments of " { $snippet "n" } " do not result in a quadratic amount of copying, so that for example " { $link push-all } " can run efficiently when used in a loop." }
27     }
28 } ;
29
30 HELP: nth
31 { $values { "n" "a non-negative integer" } { "seq" sequence } { "elt" "the element at the " { $snippet "n" } "th index" } }
32 { $contract "Outputs the " { $snippet "n" } "th element of the sequence. Elements are numbered from zero, so the last element has an index one less than the length of the sequence. All sequences support this operation." }
33 { $errors "Throws a " { $link bounds-error } " if the index is negative, or greater than or equal to the length of the sequence." } ;
34
35 HELP: set-nth
36 { $values { "elt" object } { "n" "a non-negative integer" } { "seq" "a mutable sequence" } }
37 { $contract "Sets the " { $snippet "n" } "th element of the sequence. Storing beyond the end of a resizable sequence such as a vector or string buffer grows the sequence." }
38 { $errors "Throws an error if the index is negative, or if the sequence is not resizable and the index is greater than or equal to the length of the sequence."
39 $nl
40 "Throws an error if the sequence cannot hold elements of the given type." }
41 { $side-effects "seq" } ;
42
43 HELP: nths
44 { $values
45      { "indices" sequence } { "seq" sequence }
46      { "seq'" sequence } }
47 { $description "Ouptuts a sequence of elements from the input sequence indexed by the indices." }
48 { $examples 
49     { $example "USING: prettyprint sequences ;"
50                "{ 0 2 } { \"a\" \"b\" \"c\" } nths ."
51                "{ \"a\" \"c\" }"
52     }
53 } ;
54
55 HELP: immutable
56 { $values { "seq" sequence } }
57 { $description "Throws an " { $link immutable } " error." }
58 { $error-description "Thrown if an attempt is made to modify an immutable sequence." } ;
59
60 HELP: new-sequence
61 { $values { "len" "a non-negative integer" } { "seq" sequence } { "newseq" "a mutable sequence" } }
62 { $contract "Outputs a mutable sequence of length " { $snippet "n" } " which can hold the elements of " { $snippet "seq" } ". The initial contents of the sequence are undefined." } ;
63
64 HELP: new-resizable
65 { $values { "len" "a non-negative integer" } { "seq" sequence } { "newseq" "a resizable mutable sequence" } }
66 { $contract "Outputs a resizable mutable sequence with an initial capacity of " { $snippet "n" } " elements and zero length, which can hold the elements of " { $snippet "seq" } "." }
67 { $examples
68     { $example "USING: prettyprint sequences ;" "300 V{ } new-resizable ." "V{ }" }
69     { $example "USING: prettyprint sequences ;" "300 SBUF\" \" new-resizable ." "SBUF\" \"" }
70 } ;
71
72 HELP: like
73 { $values { "seq" sequence } { "exemplar" sequence } { "newseq" "a new sequence" } }
74 { $contract "Outputs a sequence with the same elements as " { $snippet "seq" } ", but " { $emphasis "like" } " the template sequence, in the sense that it either has the same class as the template sequence, or if the template sequence is a virtual sequence, the same class as the template sequence's underlying sequence."
75 $nl
76 "The default implementation does nothing." }
77 { $notes "Unlike " { $link clone-like } ", the output sequence might share storage with the input sequence." } ;
78
79 HELP: empty?
80 { $values { "seq" sequence } { "?" "a boolean" } }
81 { $description "Tests if the sequence has zero length." } ;
82
83 HELP: if-empty
84 { $values { "seq" sequence } { "quot1" quotation } { "quot2" quotation } }
85 { $description "Makes an implicit check if the sequence is empty. An empty sequence is dropped and " { $snippet "quot1" } " is called. Otherwise, if the sequence has any elements, " { $snippet "quot2" } " is called on it." }
86 { $example
87     "USING: kernel prettyprint sequences ;"
88     "{ 1 2 3 } [ \"empty sequence\" ] [ sum ] if-empty ."
89     "6"
90 } ;
91
92 HELP: when-empty
93 { $values
94      { "seq" sequence } { "quot" "the first quotation of an " { $link if-empty } } }
95 { $description "Makes an implicit check if the sequence is empty. An empty sequence is dropped and the " { $snippet "quot" } " is called." }
96 { $examples "This word is equivalent to " { $link if-empty } " with an empty second quotation:"
97     { $example
98     "USING: sequences prettyprint ;"
99     "{ } [ { 4 5 6 } ] [ ] if-empty ."
100     "{ 4 5 6 }"
101     }
102     { $example
103     "USING: sequences prettyprint ;"
104     "{ } [ { 4 5 6 } ] when-empty ."
105     "{ 4 5 6 }"
106     }
107 } ;
108
109 HELP: unless-empty
110 { $values
111      { "seq" sequence } { "quot" "the second quotation of an " { $link if-empty } } }
112 { $description "Makes an implicit check if the sequence is empty. An empty sequence is dropped. Otherwise, the " { $snippet "quot" } " is called on the sequence." }
113 { $examples "This word is equivalent to " { $link if-empty } " with an empty first quotation:"
114     { $example
115     "USING: sequences prettyprint ;"
116     "{ 4 5 6 } [ ] [ sum ] if-empty ."
117     "15"
118     }
119     { $example
120     "USING: sequences prettyprint ;"
121     "{ 4 5 6 } [ sum ] unless-empty ."
122     "15"
123     }
124 } ;
125
126 HELP: delete-all
127 { $values { "seq" "a resizable sequence" } }
128 { $description "Resizes the sequence to zero length, removing all elements. Not all sequences are resizable." }
129 { $errors "Throws a " { $link bounds-error } " if the new length is negative, or if the sequence is not resizable." }
130 { $side-effects "seq" } ;
131
132 HELP: resize
133 { $values { "n" "a non-negative integer" } { "seq" sequence } { "newseq" "a new sequence" } }
134 { $description "Creates a new sequence of the same type as " { $snippet "seq" } " with " { $snippet "n" } " elements, and copies the contents of " { $snippet "seq" } " into the new sequence. If " { $snippet "n" } " exceeds the length of " { $snippet "seq" } ", the remaining elements are filled with a default value; " { $link f } " for arrays and 0 for strings." }
135 { $notes "This generic word is only implemented for strings and arrays." } ;
136
137 HELP: first
138 { $values { "seq" sequence } { "first" "the first element of the sequence" } }
139 { $description "Outputs the first element of the sequence." }
140 { $errors "Throws an error if the sequence is empty." } ;
141
142 HELP: second
143 { $values { "seq" sequence } { "second" "the second element of the sequence" } }
144 { $description "Outputs the second element of the sequence." }
145 { $errors "Throws an error if the sequence contains less than two elements." } ;
146
147 HELP: third
148 { $values { "seq" sequence } { "third" "the third element of the sequence" } }
149 { $description "Outputs the third element of the sequence." }
150 { $errors "Throws an error if the sequence contains less than three elements." } ;
151
152 HELP: fourth
153 { $values { "seq" sequence } { "fourth" "the fourth element of the sequence" } }
154 { $description "Outputs the fourth element of the sequence." }
155 { $errors "Throws an error if the sequence contains less than four elements." } ;
156
157 HELP: push
158 { $values { "elt" object } { "seq" "a resizable mutable sequence" } }
159 { $description "Adds an element at the end of the sequence. The sequence length is adjusted accordingly." }
160 { $errors "Throws an error if " { $snippet "seq" } " is not resizable, or if the type of " { $snippet "elt" } " is not permitted in " { $snippet "seq" } "." }
161 { $side-effects "seq" } ;
162
163 HELP: bounds-check?
164 { $values { "n" "an integer" } { "seq" sequence } { "?" "a boolean" } }
165 { $description "Tests if the index is within the bounds of the sequence." } ;
166
167 HELP: bounds-error
168 { $values { "n" "a positive integer" } { "seq" sequence } }
169 { $description "Throws a " { $link bounds-error } "." }
170 { $error-description "Thrown by " { $link nth } ", " { $link set-nth } " and " { $link set-length } " if the given index lies beyond the bounds of the sequence." } ;
171
172 HELP: bounds-check
173 { $values { "n" "a positive integer" } { "seq" sequence } }
174 { $description "Throws an error if " { $snippet "n" } " is negative or if it is greater than or equal to the length of " { $snippet "seq" } ". Otherwise the two inputs remain on the stack." } ;
175
176 HELP: ?nth
177 { $values { "n" "an integer" } { "seq" sequence } { "elt/f" "an object or " { $link f } } }
178 { $description "A forgiving version of " { $link nth } ". If the index is out of bounds, or if the sequence is " { $link f } ", simply outputs " { $link f } "." } ;
179
180 HELP: nth-unsafe
181 { $values { "n" "an integer" } { "seq" sequence } { "elt" object } }
182 { $contract "Unsafe variant of " { $link nth } " that does not perform bounds checks." } ;
183
184 HELP: set-nth-unsafe
185 { $values { "elt" object } { "n" "an integer" } { "seq" sequence } }
186 { $contract "Unsafe variant of " { $link set-nth } " that does not perform bounds checks." } ;
187
188 HELP: exchange-unsafe
189 { $values { "m" "a non-negative integer" } { "n" "a non-negative integer" } { "seq" "a mutable sequence" } }
190 { $description "Unsafe variant of " { $link exchange } " that does not perform bounds checks." } ;
191
192 HELP: first-unsafe
193 { $values { "seq" sequence } { "first" "the first element" } }
194 { $contract "Unsafe variant of " { $link first } " that does not perform bounds checks." } ;
195
196 HELP: first2-unsafe
197 { $values { "seq" sequence } { "first" "the first element" } { "second" "the second element" } }
198 { $contract "Unsafe variant of " { $link first2 } " that does not perform bounds checks." } ;
199
200 HELP: first3-unsafe
201 { $values { "seq" sequence } { "first" "the first element" } { "second" "the second element" } { "third" "the third element" } }
202 { $contract "Unsafe variant of " { $link first3 } " that does not perform bounds checks." } ;
203
204 HELP: first4-unsafe
205 { $values { "seq" sequence } { "first" "the first element" } { "second" "the second element" } { "third" "the third element" } { "fourth" "the fourth element" } }
206 { $contract "Unsafe variant of " { $link first4 } " that does not perform bounds checks." } ;
207
208 HELP: 1sequence
209 { $values { "obj" object } { "exemplar" sequence } { "seq" sequence } }
210 { $description "Creates a one-element sequence of the same type as " { $snippet "exemplar" } "." } ;
211
212 HELP: 2sequence
213 { $values { "obj1" object } { "obj2" object } { "exemplar" sequence } { "seq" sequence } }
214 { $description "Creates a two-element sequence of the same type as " { $snippet "exemplar" } "." } ;
215
216 HELP: 3sequence
217 { $values { "obj1" object } { "obj2" object } { "obj3" object } { "exemplar" sequence } { "seq" sequence } }
218 { $description "Creates a three-element sequence of the same type as " { $snippet "exemplar" } "." } ;
219
220 HELP: 4sequence
221 { $values { "obj1" object } { "obj2" object } { "exemplar" sequence } { "obj3" object } { "obj4" object } { "seq" sequence } }
222 { $description "Creates a four-element sequence of the same type as " { $snippet "exemplar" } "." } ;
223
224 HELP: first2
225 { $values { "seq" sequence } { "first" "the first element" } { "second" "the second element" } }
226 { $description "Pushes the first two elements of a sequence." }
227 { $errors "Throws an error if the sequence has less than two elements." } ;
228
229 HELP: first3
230 { $values { "seq" sequence } { "first" "the first element" } { "second" "the second element" } { "third" "the third element" } }
231 { $description "Pushes the first three elements of a sequence." }
232 { $errors "Throws an error if the sequence has less than three elements." } ;
233
234 HELP: first4
235 { $values { "seq" sequence } { "first" "the first element" } { "second" "the second element" } { "third" "the third element" } { "fourth" "the fourth element" } }
236 { $description "Pushes the first four elements of a sequence." }
237 { $errors "Throws an error if the sequence has less than four elements." } ;
238
239 HELP: array-capacity
240 { $values { "array" "an array" } { "n" "a non-negative fixnum" } }
241 { $class-description "A predicate class whose instances are valid array sizes for the current architecture. The minimum value is zero and the maximum value is " { $link max-array-capacity } "." }
242 { $description "Low-level array length accessor." }
243 { $warning "This word is in the " { $vocab-link "sequences.private" } " vocabulary because it is unsafe. It does not check types, so improper use can corrupt memory." } ;
244
245 HELP: array-nth
246 { $values { "n" "a non-negative fixnum" } { "array" "an array" }  { "elt" object } }
247 { $description "Low-level array element accessor." }
248 { $warning "This word is in the " { $vocab-link "sequences.private" } " vocabulary because it is unsafe. It does not check types or array bounds, and improper use can corrupt memory. User code must use " { $link nth } " instead." } ;
249
250 HELP: set-array-nth
251 { $values { "elt" object } { "n" "a non-negative fixnum" } { "array" "an array" }  }
252 { $description "Low-level array element mutator." }
253 { $warning "This word is in the " { $vocab-link "sequences.private" } " vocabulary because it is unsafe. It does not check types or array bounds, and improper use can corrupt memory. User code must use " { $link set-nth } " instead." } ;
254
255 HELP: collect
256 { $values { "n" "a non-negative integer" } { "quot" { $quotation "( n -- value )" } } { "into" "a sequence of length at least " { $snippet "n" } } }
257 { $description "A primitive mapping operation that applies a quotation to all integers from 0 up to but not including " { $snippet "n" } ", and collects the results in a new array. User code should use " { $link map } " instead." } ;
258
259 HELP: each
260 { $values { "seq" sequence } { "quot" { $quotation "( elt -- )" } } }
261 { $description "Applies the quotation to each element of the sequence in order." } ;
262
263 HELP: reduce
264 { $values { "seq" sequence } { "identity" object } { "quot" { $quotation "( prev elt -- next )" } } { "result" "the final result" } }
265 { $description "Combines successive elements of the sequence using a binary operation, and outputs the final result. On the first iteration, the two inputs to the quotation are " { $snippet "identity" } ", and the first element of the sequence. On successive iterations, the first input is the result of the previous iteration, and the second input is the corresponding element of the sequence." }
266 { $examples
267     { $example "USING: math prettyprint sequences ;" "{ 1 5 3 } 0 [ + ] reduce ." "9" }
268 } ;
269
270 HELP: reduce-index
271 { $values
272      { "seq" sequence } { "identity" object } { "quot" quotation } }
273 { $description "Combines successive elements of the sequence and their indices binary operations, and outputs the final result. On the first iteration, the three inputs to the quotation are " { $snippet "identity" } ", the first element of the sequence, and its index, 0. On successive iterations, the first input is the result of the previous iteration, the second input is the corresponding element of the sequence, and the third is its index." }
274 { $examples { $example "USING: sequences prettyprint math ;"
275     "{ 10 50 90 } 0 [ + + ] reduce-index ."
276     "153"
277 } } ;
278
279 HELP: accumulate
280 { $values { "identity" object } { "seq" sequence } { "quot" { $quotation "( prev elt -- next )" } } { "final" "the final result" } { "newseq" "a new sequence" } }
281 { $description "Combines successive elements of the sequence using a binary operation, and outputs a sequence of intermediate results together with the final result. On the first iteration, the two inputs to the quotation are " { $snippet "identity" } ", and the first element of the sequence. On successive iterations, the first input is the result of the previous iteration, and the second input is the corresponding element of the sequence."
282 $nl
283 "When given the empty sequence, outputs an empty sequence together with the " { $snippet "identity" } "." }
284 { $examples
285     { $example "USING: math prettyprint sequences ;" "{ 2 2 2 2 2 } 0 [ + ] accumulate . ." "{ 0 2 4 6 8 }\n10" }
286 } ;
287
288 HELP: map
289 { $values { "seq" sequence } { "quot" { $quotation "( old -- new )" } } { "newseq" "a new sequence" } }
290 { $description "Applies the quotation to each element of the sequence in order. The new elements are collected into a sequence of the same class as the input sequence." } ;
291
292 HELP: map-as
293 { $values { "seq" sequence } { "quot" { $quotation "( old -- new )" } } { "newseq" "a new sequence" } { "exemplar" sequence } }
294 { $description "Applies the quotation to each element of the sequence in order. The new elements are collected into a sequence of the same class as " { $snippet "exemplar" } "." }
295 { $examples
296     "The following example converts a string into an array of one-element strings:"
297     { $example "USING: prettyprint strings sequences ;" "\"Hello\" [ 1string ] { } map-as ." "{ \"H\" \"e\" \"l\" \"l\" \"o\" }" }
298     "Note that " { $link map } " could not be used here, because it would create another string to hold results, and one-element strings cannot themselves be elements of strings."
299 } ;
300
301 HELP: each-index
302 { $values
303      { "seq" sequence } { "quot" quotation } }
304 { $description "Calls the quotation with the element of the sequence and its index on the stack, with the index on the top of the stack." }
305 { $examples { $example "USING: sequences prettyprint math ;"
306 "{ 10 20 30 } [ + . ] each-index"
307 "10\n21\n32"
308 } } ;
309
310 HELP: map-index
311 { $values
312   { "seq" sequence } { "quot" quotation } { "newseq" sequence } }
313 { $description "Calls the quotation with the element of the sequence and its index on the stack, with the index on the top of the stack. Collects the outputs of the quotation and outputs them in a sequence of the same type as the input sequence." }
314 { $examples { $example "USING: sequences prettyprint math ;"
315 "{ 10 20 30 } [ + ] map-index ."
316 "{ 10 21 32 }"
317 } } ;
318
319 HELP: change-nth
320 { $values { "i" "a non-negative integer" } { "seq" "a mutable sequence" } { "quot" { $quotation "( elt -- newelt )" } } }
321 { $description "Applies the quotation to the " { $snippet "i" } "th element of the sequence, storing the result back into the sequence." }
322 { $errors "Throws an error if the sequence is immutable, if the index is out of bounds, or the sequence cannot hold elements of the type output by " { $snippet "quot" } "." }
323 { $side-effects "seq" } ;
324
325 HELP: change-each
326 { $values { "seq" "a mutable sequence" } { "quot" { $quotation "( old -- new )" } } }
327 { $description "Applies the quotation to each element yielding a new element, storing the new elements back in the original sequence." }
328 { $errors "Throws an error if the sequence is immutable, or the sequence cannot hold elements of the type output by " { $snippet "quot" } "." }
329 { $side-effects "seq" } ;
330
331 HELP: min-length
332 { $values { "seq1" sequence } { "seq2" sequence } { "n" "a non-negative integer" } }
333 { $description "Outputs the minimum of the lengths of the two sequences." } ;
334
335 HELP: max-length
336 { $values { "seq1" sequence } { "seq2" sequence } { "n" "a non-negative integer" } }
337 { $description "Outputs the maximum of the lengths of the two sequences." } ;
338
339 HELP: 2each
340 { $values { "seq1" sequence } { "seq2" sequence } { "quot" { $quotation "( elt1 elt2 -- )" } } }
341 { $description "Applies the quotation to pairs of elements from " { $snippet "seq1" } " and " { $snippet "seq2" } "." } ;
342
343 HELP: 3each
344 { $values { "seq1" sequence } { "seq2" sequence } { "seq3" sequence } { "quot" { $quotation "( elt1 elt2 elt3 -- )" } } }
345 { $description "Applies the quotation to triples of elements from " { $snippet "seq1" } ", " { $snippet "seq2" } " and " { $snippet "seq3" } "." } ;
346
347 HELP: 2reduce
348 { $values { "seq1" sequence }
349           { "seq2" sequence }
350           { "identity" object }
351           { "quot" { $quotation "( prev elt1 elt2 -- next )" } }
352           { "result" "the final result" } }
353 { $description "Combines successive pairs of elements from the two sequences using a ternary operation. The first input value at each iteration except the first one is the result of the previous iteration. The first input value at the first iteration is " { $snippet "identity" } "." } ;
354
355 HELP: 2map
356 { $values { "seq1" sequence } { "seq2" sequence } { "quot" { $quotation "( elt1 elt2 -- new )" } } { "newseq" "a new sequence" } }
357 { $description "Applies the quotation to each pair of elements in turn, yielding new elements which are collected into a new sequence having the same class as " { $snippet "seq1" } "." } ;
358
359 HELP: 3map
360 { $values { "seq1" sequence } { "seq2" sequence } { "seq3" sequence } { "quot" { $quotation "( elt1 elt2 elt3 -- new )" } } { "newseq" "a new sequence" } }
361 { $description "Applies the quotation to each triple of elements in turn, yielding new elements which are collected into a new sequence having the same class as " { $snippet "seq1" } "." } ;
362
363 HELP: 2map-as
364 { $values { "seq1" sequence } { "seq2" sequence } { "quot" { $quotation "( elt1 elt2 -- new )" } } { "exemplar" sequence } { "newseq" "a new sequence" } }
365 { $description "Applies the quotation to each pair of elements in turn, yielding new elements which are collected into a new sequence having the same class as " { $snippet "exemplar" } "." } ;
366
367 HELP: 3map-as
368 { $values { "seq1" sequence } { "seq2" sequence } { "seq3" sequence } { "quot" { $quotation "( elt1 elt2 elt3 -- new )" } } { "exemplar" sequence } { "newseq" "a new sequence" } }
369 { $description "Applies the quotation to each triple of elements in turn, yielding new elements which are collected into a new sequence having the same class as " { $snippet "exemplar" } "." } ;
370
371 HELP: 2all?
372 { $values { "seq1" sequence } { "seq2" sequence } { "quot" { $quotation "( elt1 elt2 -- ? )" } } { "?" "a boolean" } }
373 { $description "Tests the predicate pairwise against elements of " { $snippet "seq1" } " and " { $snippet "seq2" } "." } ;
374
375 HELP: find
376 { $values { "seq" sequence }
377           { "quot" { $quotation "( elt -- ? )" } }
378           { "i" "the index of the first match, or " { $link f } }
379           { "elt" "the first matching element, or " { $link f } } }
380 { $description "A simpler variant of " { $link find-from } " where the starting index is 0." } ;
381
382 HELP: find-from
383 { $values { "n" "a starting index" }
384           { "seq" sequence }
385           { "quot" { $quotation "( elt -- ? )" } }
386           { "i" "the index of the first match, or " { $link f } }
387           { "elt" "the first matching element, or " { $link f } } }
388 { $description "Applies the quotation to each element of the sequence in turn, until it outputs a true value or the end of the sequence is reached. If the quotation yields a true value for some sequence element, the word outputs the element index and the element itself. Otherwise, the word outputs an index of f and " { $link f } " as the element." } ;
389
390 HELP: find-last
391 { $values { "seq" sequence } { "quot" { $quotation "( elt -- ? )" } } { "i" "the index of the first match, or f" } { "elt" "the first matching element, or " { $link f } } }
392 { $description "A simpler variant of " { $link find-last-from } " where the starting index is one less than the length of the sequence." } ;
393
394 HELP: find-last-from
395 { $values { "n" "a starting index" } { "seq" sequence } { "quot" { $quotation "( elt -- ? )" } } { "i" "the index of the first match, or f" } { "elt" "the first matching element, or " { $link f } } }
396 { $description "Applies the quotation to each element of the sequence in reverse order, until it outputs a true value or the start of the sequence is reached. If the quotation yields a true value for some sequence element, the word outputs the element index and the element itself. Otherwise, the word outputs an index of f and " { $link f } " as the element." } ;
397
398 HELP: map-find
399 { $values { "seq" sequence } { "quot" { $quotation "( elt -- result/f )" } } { "result" "the first non-false result of the quotation" } { "elt" "the first matching element, or " { $link f } } }
400 { $description "Applies the quotation to each element of the sequence, until the quotation outputs a true value. If the quotation ever yields a result which is not " { $link f } ", then the value is output, along with the element of the sequence which yielded this." } ;
401
402 HELP: any?
403 { $values { "seq" sequence } { "quot" { $quotation "( elt -- ? )" } } { "?" "a boolean" } }
404 { $description "Tests if the sequence contains an element satisfying the predicate, by applying the predicate to each element in turn until a true value is found. If the sequence is empty or if the end of the sequence is reached, outputs " { $link f } "." } ;
405
406 HELP: all?
407 { $values { "seq" sequence } { "quot" { $quotation "( elt -- ? )" } } { "?" "a boolean" } }
408 { $description "Tests if all elements in the sequence satisfy the predicate by checking each element in turn. Given an empty sequence, vacuously outputs " { $link t } "." } ;
409
410 HELP: push-if
411 { $values { "elt" object } { "quot" { $quotation "( elt -- ? )" } } { "accum" "a resizable mutable sequence" } }
412 { $description "Adds the element at the end of the sequence if the quotation yields a true value." } 
413 { $notes "This word is a factor of " { $link filter } "." } ;
414
415 HELP: filter
416 { $values { "seq" sequence } { "quot" { $quotation "( elt -- ? )" } } { "subseq" "a new sequence" } }
417 { $description "Applies the quotation to each element in turn, and outputs a new sequence containing the elements of the original sequence for which the quotation output a true value." } ;
418
419 HELP: filter-here
420 { $values { "seq" "a resizable mutable sequence" } { "quot" { $quotation "( elt -- ? )" } } }
421 { $description "Applies the quotation to each element in turn, and removes elements for which the quotation outputs a false value." }
422 { $side-effects "seq" } ;
423
424 HELP: interleave
425 { $values { "seq" sequence } { "between" "a quotation" } { "quot" { $quotation "( elt -- )" } } }
426 { $description "Applies " { $snippet "quot" } " to each element in turn, also invoking " { $snippet "between" } " in-between each pair of elements." }
427 { $example "USING: io sequences ;" "{ \"a\" \"b\" \"c\" } [ \"X\" write ] [ write ] interleave" "aXbXc" } ;
428
429 HELP: index
430 { $values { "obj" object } { "seq" sequence } { "n" "an index" } }
431 { $description "Outputs the index of the first element in the sequence equal to " { $snippet "obj" } ". If no element is found, outputs " { $link f } "." } ;
432
433 HELP: index-from
434 { $values { "obj" object } { "i" "a start index" } { "seq" sequence } { "n" "an index" } }
435 { $description "Outputs the index of the first element in the sequence equal to " { $snippet "obj" } ", starting the search from the " { $snippet "i" } "th element. If no element is found, outputs " { $link f } "." } ;
436
437 HELP: last-index
438 { $values { "obj" object } { "seq" sequence } { "n" "an index" } }
439 { $description "Outputs the index of the last element in the sequence equal to " { $snippet "obj" } "; the sequence is traversed back to front. If no element is found, outputs " { $link f } "." } ;
440
441 HELP: last-index-from
442 { $values { "obj" object } { "i" "a start index" } { "seq" sequence } { "n" "an index" } }
443 { $description "Outputs the index of the last element in the sequence equal to " { $snippet "obj" } ", traversing the sequence backwards starting from the " { $snippet "i" } "th element and finishing at the first. If no element is found, outputs " { $link f } "." } ;
444
445 HELP: member?
446 { $values { "elt" object } { "seq" sequence } { "?" "a boolean" } }
447 { $description "Tests if the sequence contains an element equal to the object." }
448 { $notes "This word uses equality comparison (" { $link = } ")." } ;
449
450 HELP: memq?
451 { $values { "elt" object } { "seq" sequence } { "?" "a boolean" } }
452 { $description "Tests if the sequence contains the object." }
453 { $notes "This word uses identity comparison (" { $link eq? } ")." } ;
454
455 HELP: remove
456 { $values { "elt" object } { "seq" sequence } { "newseq" "a new sequence" } }
457 { $description "Outputs a new sequence containing all elements of the input sequence except for given element." }
458 { $notes "This word uses equality comparison (" { $link = } ")." } ;
459
460 HELP: remq
461 { $values { "elt" object } { "seq" sequence } { "newseq" "a new sequence" } }
462 { $description "Outputs a new sequence containing all elements of the input sequence except those equal to the given element." }
463 { $notes "This word uses identity comparison (" { $link eq? } ")." } ;
464
465 HELP: remove-nth
466 { $values
467      { "n" integer } { "seq" sequence }
468      { "seq'" sequence } }
469 { $description "Creates a new sequence without the element at index " { $snippet "n" } "." }
470 { $examples "Notice that the original sequence is left intact:" { $example "USING: sequences prettyprint kernel ;"
471     "{ 1 2 3 } 1 over remove-nth . ."
472     "{ 1 3 }\n{ 1 2 3 }"
473 } } ;
474
475 HELP: move
476 { $values { "from" "an index in " { $snippet "seq" } } { "to" "an index in " { $snippet "seq" } } { "seq" "a mutable sequence" } }
477 { $description "Sets the element with index " { $snippet "m" } " to the element with index " { $snippet "n" } "." }
478 { $side-effects "seq" } ;
479
480 HELP: delete
481 { $values { "elt" object } { "seq" "a resizable mutable sequence" } }
482 { $description "Removes all elements equal to " { $snippet "elt" } " from " { $snippet "seq" } "." }
483 { $notes "This word uses equality comparison (" { $link = } ")." }
484 { $side-effects "seq" } ;
485
486 HELP: delq
487 { $values { "elt" object } { "seq" "a resizable mutable sequence" } }
488 { $description "Outputs a new sequence containing all elements of the input sequence except the given element." }
489 { $notes "This word uses identity comparison (" { $link eq? } ")." }
490 { $side-effects "seq" } ;
491
492 HELP: delete-nth
493 { $values { "n" "a non-negative integer" } { "seq" "a resizable mutable sequence" } }
494 { $description "Removes the " { $snippet "n" } "th element from the sequence, shifting all other elements down and reducing its length by one." }
495 { $side-effects "seq" } ;
496
497 HELP: delete-slice
498 { $values { "from" "a non-negative integer" } { "to" "a non-negative integer" } { "seq" "a resizable mutable sequence" } }
499 { $description "Removes a range of elements beginning at index " { $snippet "from" } " and ending before index " { $snippet "to" } "." }
500 { $side-effects "seq" } ;
501
502 HELP: replace-slice
503 { $values { "new" sequence } { "seq" sequence } { "from" "a non-negative integer" } { "to" "a non-negative integer" } { "seq'" sequence } }
504 { $description "Replaces a range of elements beginning at index " { $snippet "from" } " and ending before index " { $snippet "to" } " with a new sequence." }
505 { $errors "Throws an error if " { $snippet "new" } " contains elements whose types are not permissible in " { $snippet "seq" } "." } ;
506
507 { push prefix suffix } related-words
508
509 HELP: suffix
510 { $values { "seq" sequence } { "elt" object } { "newseq" sequence } }
511 { $description "Outputs a new sequence obtained by adding " { $snippet "elt" } " at the end of " { $snippet "seq" } "." }
512 { $errors "Throws an error if the type of " { $snippet "elt" } " is not permitted in sequences of the same class as " { $snippet "seq1" } "." }
513 { $examples
514     { $example "USING: prettyprint sequences ;" "{ 1 2 3 } 4 suffix ." "{ 1 2 3 4 }" }
515 } ;
516
517 HELP: prefix
518 { $values { "seq" sequence } { "elt" object } { "newseq" sequence } }
519 { $description "Outputs a new sequence obtained by adding " { $snippet "elt" } " at the beginning of " { $snippet "seq" } "." }
520 { $errors "Throws an error if the type of " { $snippet "elt" } " is not permitted in sequences of the same class as " { $snippet "seq1" } "." } 
521 { $examples
522 { $example "USING: prettyprint sequences ;" "{ 1 2 3 } 0 prefix ." "{ 0 1 2 3 }" }
523 } ;
524
525 HELP: sum-lengths
526 { $values { "seq" "a sequence of sequences" } { "n" integer } }
527 { $description "Outputs the sum of the lengths of all sequences in " { $snippet "seq" } "." } ;
528
529 HELP: concat
530 { $values { "seq" sequence } { "newseq" sequence } }
531 { $description "Concatenates a sequence of sequences together into one sequence. If " { $snippet "seq" } " is empty, outputs " { $snippet "{ }" } ", otherwise the resulting sequence is of the same class as the first element of " { $snippet "seq" } "." }
532 { $errors "Throws an error if one of the sequences in " { $snippet "seq" } " contains elements not permitted in sequences of the same class as the first element of " { $snippet "seq" } "." } ;
533
534 HELP: concat-as
535 { $values { "seq" sequence } { "exemplar" sequence } { "newseq" sequence } }
536 { $description "Concatenates a sequence of sequences together into one sequence with the same type as " { $snippet "exemplar" } "." }
537 { $errors "Throws an error if one of the sequences in " { $snippet "seq" } " contains elements not permitted in sequences of the same class as " { $snippet "exemplar" } "." } ;
538
539 HELP: join
540 { $values { "seq" sequence } { "glue" sequence } { "newseq" sequence } }
541 { $description "Concatenates a sequence of sequences together into one sequence, placing a copy of " { $snippet "glue" } " between each pair of sequences. The resulting sequence is of the same class as " { $snippet "glue" } "." }
542 { $notes "If the " { $snippet "glue" } " sequence is empty, this word calls " { $link concat-as } "." }
543 { $errors "Throws an error if one of the sequences in " { $snippet "seq" } " contains elements not permitted in sequences of the same class as " { $snippet "glue" } "." } ;
544
545 { join concat concat-as } related-words
546
547 HELP: last
548 { $values { "seq" sequence } { "elt" object } }
549 { $description "Outputs the last element of a sequence." }
550 { $errors "Throws an error if the sequence is empty." } ;
551
552 { pop pop* } related-words
553
554 HELP: pop*
555 { $values { "seq" "a resizable mutable sequence" } }
556 { $description "Removes the last element and shortens the sequence." }
557 { $side-effects "seq" }
558 { $errors "Throws an error if the sequence is empty." } ;
559
560 HELP: pop
561 { $values { "seq" "a resizable mutable sequence" } { "elt" object } }
562 { $description "Outputs the last element after removing it and shortening the sequence." }
563 { $side-effects "seq" }
564 { $errors "Throws an error if the sequence is empty." } ;
565
566 HELP: mismatch
567 { $values { "seq1" sequence } { "seq2" sequence } { "i" "an index" } }
568 { $description "Compares pairs of elements up to the minimum of the sequences' lengths, outputting the first index where the two sequences have non-equal elements, or " { $link f } " if all tested elements were equal." } ;
569
570 HELP: flip
571 { $values { "matrix" "a sequence of equal-length sequences" } { "newmatrix" "a sequence of equal-length sequences" } }
572 { $description "Transposes the matrix; that is, rows become columns and columns become rows." }
573 { $examples { $example "USING: prettyprint sequences ;" "{ { 1 2 3 } { 4 5 6 } } flip ." "{ { 1 4 } { 2 5 } { 3 6 } }" } } ;
574
575 HELP: exchange
576 { $values { "m" "a non-negative integer" } { "n" "a non-negative integer" } { "seq" "a mutable sequence" } }
577 { $description "Exchanges the " { $snippet "m" } "th and " { $snippet "n" } "th elements of " { $snippet "seq" } "." } ;
578
579 HELP: reverse-here
580 { $values { "seq" "a mutable sequence" } }
581 { $description "Reverses a sequence in-place." }
582 { $side-effects "seq" } ;
583
584 HELP: padding
585 { $values { "seq" sequence } { "n" "a non-negative integer" } { "elt" object } { "quot" { $quotation "( seq1 seq2 -- newseq )" } } { "newseq" "a new sequence" } }
586 { $description "Outputs a new string sequence of " { $snippet "elt" } " repeated, that when appended to " { $snippet "seq" } ", yields a sequence of length " { $snippet "n" } ". If the length of " { $snippet "seq" } " is greater than " { $snippet "n" } ", this word outputs an empty sequence." } ;
587
588 HELP: pad-head
589 { $values { "seq" sequence } { "n" "a non-negative integer" } { "elt" object } { "padded" "a new sequence" } }
590 { $description "Outputs a new sequence consisting of " { $snippet "seq" } " padded on the left with enough repetitions of " { $snippet "elt" } " to have the result be of length " { $snippet "n" } "." }
591 { $examples { $example "USING: io sequences ;" "{ \"ab\" \"quux\" } [ 5 CHAR: - pad-head print ] each" "---ab\n-quux" } } ;
592
593 HELP: pad-tail
594 { $values { "seq" sequence } { "n" "a non-negative integer" } { "elt" object } { "padded" "a new sequence" } }
595 { $description "Outputs a new sequence consisting of " { $snippet "seq" } " padded on the right with enough repetitions of " { $snippet "elt" } " to have the result be of length " { $snippet "n" } "." }
596 { $examples { $example "USING: io sequences ;" "{ \"ab\" \"quux\" } [ 5 CHAR: - pad-tail print ] each" "ab---\nquux-" } } ;
597
598 HELP: sequence=
599 { $values { "seq1" sequence } { "seq2" sequence } { "?" "a boolean" } }
600 { $description "Tests if the two sequences have the same length and elements. This is weaker than " { $link = } ", since it does not ensure that the sequences are instances of the same class." } ;
601
602 HELP: reversed
603 { $class-description "A virtual sequence which presents a reversed view of an underlying sequence. New instances can be created by calling " { $link <reversed> } "." } ;
604
605 HELP: reverse
606 { $values { "seq" sequence } { "newseq" "a new sequence" } }
607 { $description "Outputs a new sequence having the same elements as " { $snippet "seq" } " but in reverse order." } ;
608
609 { reverse <reversed> reverse-here } related-words
610
611 HELP: <reversed>
612 { $values { "seq" sequence } { "reversed" "a new sequence" } }
613 { $description "Creates an instance of the " { $link reversed } " class." }
614 { $see-also "virtual-sequences" } ;
615
616 HELP: slice-error
617 { $values { "str" "a reason" } }
618 { $description "Throws a " { $link slice-error } "." }
619 { $error-description "Thrown by " { $link <slice> } " if one of the following invalid conditions holds:"
620     { $list
621         "The start index is negative"
622         "The end index is greater than the length of the sequence"
623         "The start index is greater than the end index"
624     }
625 } ;
626
627 HELP: slice
628 { $class-description "A virtual sequence which presents a subrange of the elements of an underlying sequence. New instances can be created by calling " { $link <slice> } ". Convenience words are also provided for creating slices where one endpoint is the start or end of the sequence; see " { $link "sequences-slices" } " for a list."
629 $nl
630 "Slices are mutable if the underlying sequence is mutable, and mutating a slice changes the underlying sequence. However, slices cannot be resized after creation." } ;
631
632 HELP: check-slice
633 { $values { "from" "a non-negative integer" } { "to" "a non-negative integer" } { "seq" sequence } }
634 { $description "Ensures that " { $snippet "m" } " is less than or equal to " { $snippet "m" } ", and that both indices are within bounds for " { $snippet "seq" } "." }
635 { $errors "Throws a " { $link slice-error } " if the preconditions are not met." } ;
636
637 HELP: collapse-slice
638 { $values { "m" "a non-negative integer" } { "n" "a non-negative integer" } { "slice" slice } { "m'" "a non-negative integer" } { "n'" "a non-negative integer" } { "seq" sequence } }
639 { $description "Prepares to take the slice of a slice by adjusting the start and end indices accordingly, and replacing the slice with its underlying sequence." }
640 ;
641
642 HELP: <flat-slice>
643 { $values { "seq" sequence } { "slice" slice } }
644 { $description "Outputs a slice with the same elements as " { $snippet "seq" } ", and " { $snippet "from" } " equal to 0 and " { $snippet "to" } " equal to the length of " { $snippet "seq" } "." }
645 { $notes "Some words create slices then proceed to read the " { $snippet "to" } " and " { $snippet "from" } " slots of the slice. To behave predictably when they are themselves given a slice as input, they apply this word first to get a canonical slice." } ;
646
647 HELP: <slice>
648 { $values { "from" "a non-negative integer" } { "to" "a non-negative integer" } { "seq" sequence } { "slice" slice } }
649 { $description "Outputs a new virtual sequence sharing storage with the subrange of elements in " { $snippet "seq" } " with indices starting from and including " { $snippet "m" } ", and up to but not including " { $snippet "n" } "." }
650 { $errors "Throws an error if " { $snippet "m" } " or " { $snippet "n" } " is out of bounds." }
651 { $notes "Taking the slice of a slice outputs a slice of the underlying sequence of the original slice. Keep this in mind when writing code which depends on the values of " { $snippet "from" } " and " { $snippet "to" } " being equal to the inputs to this word. The " { $link <flat-slice> } " word might be helpful in such situations." } ;
652
653 { <slice> subseq } related-words
654
655 HELP: repetition
656 { $class-description "A virtual sequence consisting of " { $snippet "elt" } " repeated " { $snippet "len" } " times. Repetitions are created by calling " { $link <repetition> } "." } ;
657
658 HELP: <repetition>
659 { $values { "len" "a non-negative integer" } { "elt" object } { "repetition" repetition } }
660 { $description "Creates a new " { $link repetition } "." }
661 { $examples
662     { $example "USING: arrays prettyprint sequences ;" "10 \"X\" <repetition> >array ." "{ \"X\" \"X\" \"X\" \"X\" \"X\" \"X\" \"X\" \"X\" \"X\" \"X\" }" }
663     { $example "USING: prettyprint sequences ;" "10 \"X\" <repetition> concat ." "\"XXXXXXXXXX\"" }
664 } ;
665 HELP: copy
666 { $values { "src" sequence } { "i" "an index in " { $snippet "dest" } } { "dst" "a mutable sequence" } }
667 { $description "Copies all elements of " { $snippet "src" } " to " { $snippet "dest" } ", with destination indices starting from " { $snippet "i" } ". Grows " { $snippet "to" } " first if necessary." }
668 { $side-effects "dest" }
669 { $errors "An error is thrown if " { $snippet "to" } " is not resizable, and not large enough to hold the copied elements." } ;
670
671 HELP: push-all
672 { $values { "src" sequence } { "dest" "a resizable mutable sequence" } }
673 { $description "Appends " { $snippet "src" } " to the end of " { $snippet "dest" } "." }
674 { $side-effects "dest" }
675 { $errors "Throws an error if " { $snippet "src" } " contains elements not permitted in " { $snippet "dest" } "." } ;
676
677 HELP: append
678 { $values { "seq1" sequence } { "seq2" sequence } { "newseq" sequence } }
679 { $description "Outputs a new sequence of the same type as " { $snippet "seq1" } " consisting of the elements of " { $snippet "seq1" } " followed by " { $snippet "seq2" } "." }
680 { $errors "Throws an error if " { $snippet "seq2" } " contains elements not permitted in sequences of the same class as " { $snippet "seq1" } "." }
681 { $examples 
682     { $example "USING: prettyprint sequences ;"
683         "{ 1 2 } B{ 3 4 } append ."
684         "{ 1 2 3 4 }"
685     }
686     { $example "USING: prettyprint sequences strings ;"
687         "\"go\" \"ing\" append ."
688         "\"going\""
689     }
690 } ;
691
692 HELP: append-as
693 { $values { "seq1" sequence } { "seq2" sequence } { "exemplar" sequence } { "newseq" sequence } }
694 { $description "Outputs a new sequence of the same type as " { $snippet "exemplar" } " consisting of the elements of " { $snippet "seq1" } " followed by " { $snippet "seq2" } "." }
695 { $errors "Throws an error if " { $snippet "seq1" } " or " { $snippet "seq2" } " contain elements not permitted in sequences of the same class as " { $snippet "exemplar" } "." }
696 { $examples 
697     { $example "USING: prettyprint sequences ;"
698         "{ 1 2 } B{ 3 4 } B{ } append-as ."
699         "B{ 1 2 3 4 }"
700     }
701     { $example "USING: prettyprint sequences strings ;"
702         "\"go\" \"ing\" SBUF\" \" append-as ."
703         "SBUF\" going\""
704     }
705 } ;
706
707 { append append-as } related-words
708
709 HELP: prepend
710 { $values { "seq1" sequence } { "seq2" sequence } { "newseq" sequence } }
711 { $description "Outputs a new sequence of the same type as " { $snippet "seq2" } " consisting of the elements of " { $snippet "seq2" } " followed by " { $snippet "seq1" } "." }
712 { $errors "Throws an error if " { $snippet "seq1" } " contains elements not permitted in sequences of the same class as " { $snippet "seq2" } "." }
713 { $examples { $example "USING: prettyprint sequences ;"
714         "{ 1 2 } B{ 3 4 } prepend ."
715         "B{ 3 4 1 2 }"
716     }
717     { $example "USING: prettyprint sequences strings ;"
718         "\"go\" \"car\" prepend ."
719         "\"cargo\""
720     }
721 } ;
722
723 HELP: 3append
724 { $values { "seq1" sequence } { "seq2" sequence } { "seq3" sequence } { "newseq" sequence } }
725 { $description "Outputs a new sequence consisting of the elements of " { $snippet "seq1" } ", " { $snippet "seq2" } " and " { $snippet "seq3" } " in turn." }
726 { $errors "Throws an error if " { $snippet "seq2" } " or " { $snippet "seq3" } " contain elements not permitted in sequences of the same class as " { $snippet "seq1" } "." }
727 { $examples
728     { $example "USING: prettyprint sequences ;"
729         "\"a\" \"b\" \"c\" 3append ."
730         "\"abc\""
731     }
732 } ;
733
734 HELP: 3append-as
735 { $values { "seq1" sequence } { "seq2" sequence } { "seq3" sequence } { "exemplar" sequence } { "newseq" sequence } }
736 { $description "Outputs a new sequence consisting of the elements of " { $snippet "seq1" } ", " { $snippet "seq2" } " and " { $snippet "seq3" } " in turn of the same type as " { $snippet "exemplar" } "." }
737 { $errors "Throws an error if " { $snippet "seq1" } ", " { $snippet "seq2" } ", or " { $snippet "seq3" } " contain elements not permitted in sequences of the same class as " { $snippet "exemplar" } "." }
738 { $examples
739     { $example "USING: prettyprint sequences ;"
740         "\"a\" \"b\" \"c\" SBUF\" \" 3append-as ."
741         "SBUF\" abc\""
742     }
743 } ;
744
745 { 3append 3append-as } related-words
746
747 HELP: surround
748 { $values { "seq1" sequence } { "seq2" sequence } { "seq3" sequence } { "newseq" sequence } }
749 { $description "Outputs a new sequence with " { $snippet "seq1" } " inserted between " { $snippet "seq2" } " and " { $snippet "seq3" } "." }
750 { $examples
751     { $example "USING: sequences prettyprint ;"
752                "\"sssssh\" \"(\" \")\" surround ."
753                "\"(sssssh)\""
754     }
755 } ;
756
757 HELP: glue
758 { $values { "seq1" sequence } { "seq2" sequence } { "seq3" sequence } { "newseq" sequence } }
759 { $description "Outputs a new sequence with " { $snippet "seq3" } " inserted between " { $snippet "seq1" } " and " { $snippet "seq2" } "." }
760 { $examples
761     { $example "USING: sequences prettyprint ;"
762                "\"a\" \"b\" \",\" glue ."
763                "\"a,b\""
764     }
765 } ;
766
767 HELP: subseq
768 { $values { "from" "a non-negative integer" } { "to" "a non-negative integer" } { "seq" sequence } { "subseq" "a new sequence" } }
769 { $description "Outputs a new sequence consisting of all elements starting from and including " { $snippet "from" } ", and up to but not including " { $snippet "to" } "." }
770 { $errors "Throws an error if " { $snippet "from" } " or " { $snippet "to" } " is out of bounds." } ;
771
772 HELP: clone-like
773 { $values { "seq" sequence } { "exemplar" sequence } { "newseq" "a new sequence" } }
774 { $description "Outputs a newly-allocated sequence with the same elements as " { $snippet "seq" } " but of the same type as " { $snippet "exemplar" } "." }
775 { $notes "Unlike " { $link like } ", this word always creates a new sequence which never shares storage with the original." } ;
776
777 HELP: head-slice
778 { $values { "seq" sequence } { "n" "a non-negative integer" } { "slice" "a slice" } }
779 { $description "Outputs a virtual sequence sharing storage with the first " { $snippet "n" } " elements of the input sequence." }
780 { $errors "Throws an error if the index is out of bounds." } ;
781
782 HELP: tail-slice
783 { $values { "seq" sequence } { "n" "a non-negative integer" } { "slice" "a slice" } }
784 { $description "Outputs a virtual sequence sharing storage with all elements from the " { $snippet "n" } "th index until the end of the input sequence." }
785 { $errors "Throws an error if the index is out of bounds." } ;
786
787 HELP: but-last-slice
788 { $values { "seq" sequence } { "slice" "a slice" } }
789 { $description "Outputs a virtual sequence sharing storage with all but the last element of the input sequence." }
790 { $errors "Throws an error on an empty sequence." } ;
791
792 HELP: rest-slice
793 { $values { "seq" sequence } { "slice" "a slice" } }
794 { $description "Outputs a virtual sequence sharing storage with all elements from the 1st index until the end of the input sequence." }
795 { $notes "Equivalent to " { $snippet "1 tail" } }
796 { $errors "Throws an error on an empty sequence." } ;
797
798 HELP: head-slice*
799 { $values { "seq" sequence } { "n" "a non-negative integer" } { "slice" "a slice" } }
800 { $description "Outputs a virtual sequence sharing storage with all elements of " { $snippet "seq" } " until the " { $snippet "n" } "th element from the end. In other words, it outputs a sequence of the first " { $snippet "l-n" } " elements of the input sequence, where " { $snippet "l" } " is its length." }
801 { $errors "Throws an error if the index is out of bounds." } ;
802
803 HELP: tail-slice*
804 { $values { "seq" sequence } { "n" "a non-negative integer" } { "slice" "a slice" } }
805 { $description "Outputs a virtual sequence sharing storage with the last " { $snippet "n" } " elements of the input sequence." }
806 { $errors "Throws an error if the index is out of bounds." } ;
807
808 HELP: head
809 { $values { "seq" sequence } { "n" "a non-negative integer" } { "headseq" "a new sequence" } }
810 { $description "Outputs a new sequence consisting of the first " { $snippet "n" } " elements of the input sequence." }
811 { $errors "Throws an error if the index is out of bounds." } ;
812
813 HELP: tail
814 { $values { "seq" sequence } { "n" "a non-negative integer" } { "tailseq" "a new sequence" } }
815 { $description "Outputs a new sequence consisting of the input sequence with the first n items removed." }
816 { $errors "Throws an error if the index is out of bounds." } ;
817
818 HELP: but-last
819 { $values { "seq" sequence } { "headseq" "a new sequence" } }
820 { $description "Outputs a new sequence consisting of the input sequence with the last item removed." }
821 { $errors "Throws an error on an empty sequence." } ;
822
823 HELP: rest
824 { $values { "seq" sequence } { "tailseq" "a new sequence" } }
825 { $description "Outputs a new sequence consisting of the input sequence with the first item removed." }
826 { $errors "Throws an error on an empty sequence." } ;
827
828 HELP: head*
829 { $values { "seq" sequence } { "n" "a non-negative integer" } { "headseq" "a new sequence" } }
830 { $description "Outputs a new sequence consisting of all elements of " { $snippet "seq" } " until the " { $snippet "n" } "th element from the end. In other words, it outputs a sequence of the first " { $snippet "l-n" } " elements of the input sequence, where " { $snippet "l" } " is its length." }
831 { $errors "Throws an error if the index is out of bounds." } ;
832
833 HELP: tail*
834 { $values { "seq" sequence } { "n" "a non-negative integer" } { "tailseq" "a new sequence" } }
835 { $description "Outputs a new sequence consisting of the last " { $snippet "n" } " elements of the input sequence." }
836 { $errors "Throws an error if the index is out of bounds." } ;
837
838 HELP: shorter?
839 { $values { "seq1" sequence } { "seq2" sequence } { "?" "a boolean" } }
840 { $description "Tets if the length of " { $snippet "seq1" } " is smaller than the length of " { $snippet "seq2" } "." } ;
841
842 HELP: head?
843 { $values { "seq" sequence } { "begin" sequence } { "?" "a boolean" } }
844 { $description "Tests if " { $snippet "seq" } " starts with " { $snippet "begin" } ". If " { $snippet "begin" } " is longer than " { $snippet "seq" } ", this word outputs " { $link f } "." } ;
845
846 HELP: tail?
847 { $values { "seq" sequence } { "end" sequence } { "?" "a boolean" } }
848 { $description "Tests if " { $snippet "seq" } " ends with " { $snippet "end" } ". If " { $snippet "end" } " is longer than " { $snippet "seq" } ", this word outputs " { $link f } "." } ;
849
850 { remove remove-nth remq delq delete delete-nth } related-words
851
852 HELP: cut-slice
853 { $values { "seq" sequence } { "n" "a non-negative integer" } { "before-slice" sequence } { "after-slice" "a slice" } }
854 { $description "Outputs a pair of sequences, where " { $snippet "before" } " consists of the first " { $snippet "n" } " elements of " { $snippet "seq" } " and has the same type, while " { $snippet "after" } " is a slice of the remaining elements." }
855 { $notes "Unlike " { $link cut } ", the run time of this word is proportional to the length of " { $snippet "before" } ", not " { $snippet "after" } ", so it is suitable for use in an iterative algorithm which cuts successive pieces off a sequence." } ;
856
857 HELP: cut
858 { $values { "seq" sequence } { "n" "a non-negative integer" } { "before" sequence } { "after" sequence } }
859 { $description "Outputs a pair of sequences, where " { $snippet "before" } " consists of the first " { $snippet "n" } " elements of " { $snippet "seq" } ", while " { $snippet "after" } " holds the remaining elements. Both output sequences have the same type as " { $snippet "seq" } "." }
860 { $notes "Since this word copies the entire tail of the sequence, it should not be used in a loop. If this is important, consider using " { $link cut-slice } " instead, since it returns a slice for the tail instead of copying." } ;
861
862 HELP: cut*
863 { $values { "seq" sequence } { "n" "a non-negative integer" } { "before" sequence } { "after" sequence } }
864 { $description "Outputs a pair of sequences, where " { $snippet "after" } " consists of the last " { $snippet "n" } " elements of " { $snippet "seq" } ", while " { $snippet "before" } " holds the remaining elements. Both output sequences have the same type as " { $snippet "seq" } "." } ;
865
866 HELP: start*
867 { $values { "subseq" sequence } { "seq" sequence } { "n" "a start index" } { "i" "a start index" } }
868 { $description "Outputs the start index of the first contiguous subsequence equal to " { $snippet "subseq" } ", starting the search from the " { $snippet "n" } "th element. If no matching subsequence is found, outputs " { $link f } "." } ;
869
870 HELP: start
871 { $values { "subseq" sequence } { "seq" sequence } { "i" "a start index" } }
872 { $description "Outputs the start index of the first contiguous subsequence equal to " { $snippet "subseq" } ", or " { $link f } " if no matching subsequence is found." } ;
873
874 HELP: subseq?
875 { $values { "subseq" sequence } { "seq" sequence } { "?" "a boolean" } }
876 { $description "Tests if " { $snippet "seq" } " contains the elements of " { $snippet "subseq" } " as a contiguous subsequence." } ;
877
878 HELP: drop-prefix
879 { $values { "seq1" sequence } { "seq2" sequence } { "slice1" "a slice" } { "slice2" "a slice" } }
880 { $description "Outputs a pair of virtual sequences with the common prefix of " { $snippet "seq1" } " and " { $snippet "seq2" } " removed." } ;
881
882 HELP: unclip
883 { $values { "seq" sequence } { "rest" sequence } { "first" object } }
884 { $description "Outputs a tail sequence and the first element of " { $snippet "seq" } "; the tail sequence consists of all elements of " { $snippet "seq" } " but the first." }
885 { $examples
886     { $example "USING: prettyprint sequences ;" "{ 1 2 3 } unclip suffix ." "{ 2 3 1 }" }
887 } ;
888
889 HELP: unclip-slice
890 { $values { "seq" sequence } { "rest-slice" slice } { "first" object } }
891 { $description "Outputs a tail sequence and the first element of " { $snippet "seq" } "; the tail sequence consists of all elements of " { $snippet "seq" } " but the first. Unlike " { $link unclip } ", this word does not make a copy of the input sequence, and runs in constant time." }
892 { $examples { $example "USING: math.order prettyprint sequences ;" "{ 3 -1 -10 5 7 } unclip-slice [ min ] reduce ." "-10" } } ;
893
894 HELP: unclip-last
895 { $values { "seq" sequence } { "butlast" sequence } { "last" object } }
896 { $description "Outputs a head sequence and the last element of " { $snippet "seq" } "; the head sequence consists of all elements of " { $snippet "seq" } " but the last." }
897 { $examples
898     { $example "USING: prettyprint sequences ;" "{ 1 2 3 } unclip-last prefix ." "{ 3 1 2 }" }
899 } ;
900
901 HELP: unclip-last-slice
902 { $values { "seq" sequence } { "butlast-slice" slice } { "last" object } }
903 { $description "Outputs a head sequence and the last element of " { $snippet "seq" } "; the head sequence consists of all elements of " { $snippet "seq" } " but the last Unlike " { $link unclip-last } ", this word does not make a copy of the input sequence, and runs in constant time." } ;
904
905 HELP: sum
906 { $values { "seq" "a sequence of numbers" } { "n" "a number" } }
907 { $description "Outputs the sum of all elements of " { $snippet "seq" } ". Outputs zero given an empty sequence." } ;
908
909 HELP: product
910 { $values { "seq" "a sequence of numbers" } { "n" "a number" } }
911 { $description "Outputs the product of all elements of " { $snippet "seq" } ". Outputs one given an empty sequence." } ;
912
913 HELP: infimum
914 { $values { "seq" "a sequence of real numbers" } { "n" "a number" } }
915 { $description "Outputs the least element of " { $snippet "seq" } "." }
916 { $errors "Throws an error if the sequence is empty." } ;
917
918 HELP: supremum
919 { $values { "seq" "a sequence of real numbers" } { "n" "a number" } }
920 { $description "Outputs the greatest element of " { $snippet "seq" } "." }
921 { $errors "Throws an error if the sequence is empty." } ;
922
923 HELP: produce
924 { $values { "pred" { $quotation "( -- ? )" } } { "quot" { $quotation "( -- obj )" } } { "seq" "a sequence" } }
925 { $description "Calls " { $snippet "pred" } " repeatedly. If the predicate yields " { $link f } ", stops, otherwise, calls " { $snippet "quot" } " to yield a value. Values are accumulated and returned in a sequence at the end." }
926 { $examples
927     "The following example divides a number by two until we reach zero, and accumulates intermediate results:"
928     { $example "USING: kernel math prettyprint sequences ;" "1337 [ dup 0 > ] [ 2/ dup ] produce nip ." "{ 668 334 167 83 41 20 10 5 2 1 0 }" }
929     "The following example collects random numbers as long as they are greater than 1:"
930     { $unchecked-example "USING: kernel prettyprint random sequences ;" "[ 10 random dup 1 > ] [ ] produce nip ." "{ 8 2 2 9 }" }
931 } ;
932
933 HELP: produce-as
934 { $values { "pred" { $quotation "( -- ? )" } } { "quot" { $quotation "( -- obj )" } } { "exemplar" sequence } { "seq" "a sequence" } }
935 { $description "Calls " { $snippet "pred" } " repeatedly. If the predicate yields " { $link f } ", stops, otherwise, calls " { $snippet "quot" } " to yield a value. Values are accumulated and returned in a sequence of type " { $snippet "exemplar" } " at the end." }
936 { $examples "See " { $link produce } " for examples." } ;
937
938 HELP: sigma
939 { $values { "seq" sequence } { "quot" quotation } { "n" number } }
940 { $description "Like map sum, but without creating an intermediate sequence." }
941 { $example
942     "! Find the sum of the squares [0,99]"
943     "USING: math math.ranges sequences prettyprint ;"
944     "100 [1,b] [ sq ] sigma ."
945     "338350"
946 } ;
947
948 HELP: count
949 { $values { "seq" sequence } { "quot" quotation } { "n" integer } }
950 { $description "Efficiently returns the number of elements that the predicate quotation matches." }
951 { $example
952     "USING: math math.ranges sequences prettyprint ;"
953     "100 [1,b] [ even? ] count ."
954     "50"
955 } ;
956
957 HELP: pusher
958 { $values
959      { "quot" "a predicate quotation" }
960      { "quot" quotation } { "accum" vector } }
961 { $description "Creates a new vector to accumulate the values which return true for a predicate.  Returns a new quotation which accepts an object to be tested and stored in the accumulator if the test yields true. The accumulator is left on the stack for convenience." }
962 { $example "! Find all the even numbers:" "USING: prettyprint sequences math kernel ;"
963            "10 [ even? ] pusher [ each ] dip ."
964            "V{ 0 2 4 6 8 }"
965 }
966 { $notes "Used to implement the " { $link filter } " word." } ;
967
968 HELP: trim-head
969 { $values
970      { "seq" sequence } { "quot" quotation }
971      { "newseq" sequence } }
972 { $description "Removes elements starting from the left side of a sequence if they match a predicate. Once an element does not match, the test stops and the rest of the sequence is left on the stack as a new sequence." }
973 { $example "" "USING: prettyprint math sequences ;"
974            "{ 0 0 1 2 3 0 0 } [ zero? ] trim-head ."
975            "{ 1 2 3 0 0 }"
976 } ;
977
978 HELP: trim-head-slice
979 { $values
980      { "seq" sequence } { "quot" quotation }
981      { "slice" slice } }
982 { $description "Removes elements starting from the left side of a sequence if they match a predicate. Once an element does not match, the test stops and the rest of the sequence is left on the stack as a slice" }
983 { $example "" "USING: prettyprint math sequences ;"
984            "{ 0 0 1 2 3 0 0 } [ zero? ] trim-head-slice ."
985            "T{ slice { from 2 } { to 7 } { seq { 0 0 1 2 3 0 0 } } }"
986 } ;
987
988 HELP: trim-tail
989 { $values
990      { "seq" sequence } { "quot" quotation }
991      { "newseq" sequence } }
992 { $description "Removes elements starting from the right side of a sequence if they match a predicate. Once an element does not match, the test stops and the rest of the sequence is left on the stack as a new sequence." }
993 { $example "" "USING: prettyprint math sequences ;"
994            "{ 0 0 1 2 3 0 0 } [ zero? ] trim-tail ."
995            "{ 0 0 1 2 3 }"
996 } ;
997
998 HELP: trim-tail-slice
999 { $values
1000      { "seq" sequence } { "quot" quotation }
1001      { "slice" slice } }
1002 { $description "Removes elements starting from the right side of a sequence if they match a predicate. Once an element does not match, the test stops and the rest of the sequence is left on the stack as a slice." }
1003 { $example "" "USING: prettyprint math sequences ;"
1004            "{ 0 0 1 2 3 0 0 } [ zero? ] trim-tail-slice ."
1005            "T{ slice { from 0 } { to 5 } { seq { 0 0 1 2 3 0 0 } } }"
1006 } ;
1007
1008 HELP: trim
1009 { $values
1010      { "seq" sequence } { "quot" quotation }
1011      { "newseq" sequence } }
1012 { $description "Removes elements starting from the left and right sides of a sequence if they match a predicate. Once an element does not match, the test stops and the rest of the sequence is left on the stack as a new sequence." }
1013 { $example "" "USING: prettyprint math sequences ;"
1014            "{ 0 0 1 2 3 0 0 } [ zero? ] trim ."
1015            "{ 1 2 3 }"
1016 } ;
1017
1018 HELP: trim-slice
1019 { $values
1020      { "seq" sequence } { "quot" quotation }
1021      { "slice" slice } }
1022 { $description "Removes elements starting from the left and right sides of a sequence if they match a predicate. Once an element does not match, the test stops and the rest of the sequence is left on the stack as a slice." }
1023 { $example "" "USING: prettyprint math sequences ;"
1024            "{ 0 0 1 2 3 0 0 } [ zero? ] trim-slice ."
1025            "T{ slice { from 2 } { to 5 } { seq { 0 0 1 2 3 0 0 } } }"
1026 } ;
1027
1028 { trim trim-slice trim-head trim-head-slice trim-tail trim-tail-slice } related-words
1029
1030 HELP: sift
1031 { $values
1032      { "seq" sequence }
1033      { "newseq" sequence } }
1034  { $description "Outputs a new sequence with all instance of " { $link f  } " removed." }
1035  { $examples 
1036     { $example "USING: prettyprint sequences ;"
1037         "{ \"a\" 3 { } f } sift ."
1038         "{ \"a\" 3 { } }"
1039     }
1040 } ;
1041
1042 HELP: harvest
1043 { $values
1044      { "seq" sequence }
1045      { "newseq" sequence } }
1046 { $description "Outputs a new sequence with all empty sequences removed." }
1047 { $examples 
1048     { $example "USING: prettyprint sequences ;"
1049                "{ { } { 2 3 } { 5 } { } } harvest ."
1050                "{ { 2 3 } { 5 } }"
1051     }
1052 } ;
1053
1054 { filter filter-here sift harvest } related-words
1055
1056 HELP: set-first
1057 { $values
1058      { "first" object } { "seq" sequence } }
1059 { $description "Sets the first element of a sequence." }
1060 { $examples 
1061     { $example "USING: prettyprint kernel sequences ;"
1062         "{ 1 2 3 4  } 5 over set-first ."
1063         "{ 5 2 3 4 }"
1064     }
1065 } ;
1066
1067 HELP: set-second
1068 { $values
1069      { "second" object } { "seq" sequence } }
1070 { $description "Sets the second element of a sequence." }
1071 { $examples 
1072     { $example "USING: prettyprint kernel sequences ;"
1073         "{ 1 2 3 4  } 5 over set-second ."
1074         "{ 1 5 3 4 }"
1075     }
1076 } ;
1077
1078 HELP: set-third
1079 { $values
1080      { "third" object } { "seq" sequence } }
1081 { $description "Sets the third element of a sequence." }
1082 { $examples 
1083     { $example "USING: prettyprint kernel sequences ;"
1084         "{ 1 2 3 4  } 5 over set-third ."
1085         "{ 1 2 5 4 }"
1086     }
1087 } ;
1088
1089 HELP: set-fourth
1090 { $values
1091      { "fourth" object } { "seq" sequence } }
1092 { $description "Sets the fourth element of a sequence." }
1093 { $examples 
1094     { $example "USING: prettyprint kernel sequences ;"
1095         "{ 1 2 3 4  } 5 over set-fourth ."
1096         "{ 1 2 3 5 }"
1097     }
1098 } ;
1099
1100 { set-first set-second set-third set-fourth } related-words
1101
1102 HELP: replicate
1103 { $values
1104      { "seq" sequence } { "quot" { $quotation "( -- elt )" } }
1105      { "newseq" sequence } }
1106 { $description "Calls the quotation for every element of the sequence in order. However, the element is not passed to the quotation -- it is dropped, and the quotation produces an element of its own that is collected into a sequence of the same class as the input sequence." }
1107 { $examples 
1108     { $unchecked-example "USING: kernel prettyprint random sequences ;"
1109         "5 [ 100 random ] replicate ."
1110         "{ 52 10 45 81 30 }"
1111     }
1112 } ;
1113
1114 HELP: replicate-as
1115 { $values
1116      { "seq" sequence } { "quot" quotation } { "exemplar" sequence }
1117      { "newseq" sequence } }
1118 { $description "Calls the quotation for every element of the sequence in order. However, the element is not passed to the quotation -- it is dropped, and the quotation produces an element of its own that is collected into a sequence of the same class as the exemplar sequence." }
1119 { $examples 
1120     { $unchecked-example "USING: prettyprint kernel sequences ;"
1121         "5 [ 100 random ] B{ } replicate-as ."
1122         "B{ 44 8 2 33 18 }"
1123     }
1124 } ;
1125 { replicate replicate-as } related-words
1126
1127 HELP: partition
1128 { $values
1129      { "seq" sequence } { "quot" quotation }
1130      { "trueseq" sequence } { "falseseq" sequence } }
1131      { $description "Calls a predicate quotation on each element of the input sequence.  If the test yields true, the element is added to " { $snippet "trueseq" } "; if false, it's added to " { $snippet "falseseq" } "." }
1132 { $examples 
1133     { $example "USING: prettyprint kernel math sequences ;"
1134         "{ 1 2 3 4 5 } [ even? ] partition [ . ] bi@"
1135         "{ 2 4 }\n{ 1 3 5 }"
1136     }
1137 } ;
1138
1139 HELP: virtual-seq
1140 { $values
1141      { "seq" sequence }
1142      { "seq'" sequence } }
1143 { $description "Part of the virtual sequence protocol, this word is used to return an underlying array from which to look up a value at an index given by " { $link virtual@ } "." } ;
1144
1145 HELP: virtual@
1146 { $values
1147      { "n" integer } { "seq" sequence }
1148      { "n'" integer } { "seq'" sequence } }
1149 { $description "Part of the sequence protocol, this word translates the input index " { $snippet "n" } " into an index into the underlying storage returned by " { $link virtual-seq } "." } ;
1150
1151 HELP: 2map-reduce
1152 { $values
1153      { "seq1" sequence } { "seq2" sequence } { "map-quot" quotation } { "reduce-quot" quotation }
1154      { "result" object } }
1155 { $description "Unclips the first element of each sequence and calls " { $snippet "map-quot" } " on both objects. The result of this calculation is passed, along with the rest of both sequences, to " { $link 2reduce } ", with the computed object as the identity." }
1156 { $examples { $example "USING: sequences prettyprint math ;"
1157     "{ 10 30 50 } { 200 400 600 } [ + ] [ + ] 2map-reduce ."
1158     "1290"
1159 } } ;
1160
1161 HELP: 2pusher
1162 { $values
1163      { "quot" quotation }
1164      { "quot" quotation } { "accum1" vector } { "accum2" vector } }
1165 { $description "Creates two new vectors to accumultate values based on a predicate. The first vector accumulates values for which the predicate yields true; the second for false." } ;
1166
1167 HELP: 2reverse-each
1168 { $values
1169      { "seq1" sequence } { "seq2" sequence } { "quot" quotation } }
1170 { $description "Reverse the sequences using the " { $link <reversed> } " word and calls " { $link 2each } " on the reversed sequences." }
1171 { $examples { $example "USING: sequences math prettyprint ;"
1172     "{ 10 20 30 } { 1 2 3 } [ + . ] 2reverse-each"
1173     "33\n22\n11"
1174 } } ;
1175
1176 HELP: 2unclip-slice
1177 { $values
1178      { "seq1" sequence } { "seq2" sequence }
1179      { "rest-slice1" sequence } { "rest-slice2" sequence } { "first1" object } { "first2" object } }
1180 { $description "Unclips the first element of each sequence and leaves two slice elements and the two unclipped objects on the stack." }
1181 { $examples { $example "USING: sequences prettyprint kernel arrays ;"
1182     "{ 1 2 } { 3 4 } 2unclip-slice 4array [ . ] each"
1183     "T{ slice { from 1 } { to 2 } { seq { 1 2 } } }\nT{ slice { from 1 } { to 2 } { seq { 3 4 } } }\n1\n3"
1184 } } ;
1185
1186 HELP: accumulator
1187 { $values
1188      { "quot" quotation }
1189      { "quot'" quotation } { "vec" vector } }
1190 { $description "Creates a new quotation that pushes its result to a vector and outputs that vector on the stack." }
1191 { $examples { $example "USING: sequences prettyprint kernel math ;"
1192     "{ 1 2 } [ 30 + ] accumulator [ each ] dip ."
1193     "V{ 31 32 }"
1194 } } ;
1195
1196 HELP: binary-reduce
1197 { $values
1198      { "seq" sequence } { "start" integer } { "quot" quotation }
1199      { "value" object } }
1200 { $description "Like " { $link reduce } ", but splits the sequence in half recursively until each sequence is small enough, and calls the quotation on these smaller sequences. If the quotation computes values that depend on the size of their input, such as bignum arithmetic, then this algorithm can be more efficient than using " { $link reduce } "." }
1201 { $examples "Computing factorial:"
1202     { $example "USING: prettyprint sequences math ;"
1203     "40 rest-slice 1 [ * ] binary-reduce ."
1204     "20397882081197443358640281739902897356800000000" }
1205 } ;
1206
1207 HELP: follow
1208 { $values
1209      { "obj" object } { "quot" quotation }
1210      { "seq" sequence } }
1211 { $description "Outputs a sequence containing the input object and all of the objects generated by successively feeding the result of the quotation called on the input object to the quotation recursuively. Objects yielded by the quotation are added to the output sequence until the quotation yields " { $link f } ", at which point the recursion terminates." }
1212 { $examples "Get random numbers until zero is reached:"
1213     { $unchecked-example
1214     "USING: random sequences prettyprint math ;"
1215     "100 [ random [ f ] when-zero ] follow ."
1216     "{ 100 86 34 32 24 11 7 2 }"
1217 } } ;
1218
1219 HELP: halves
1220 { $values
1221      { "seq" sequence }
1222      { "first-slice" slice } { "second-slice" slice } }
1223 { $description "Splits a sequence into two slices at the midpoint. If the sequence has an odd number of elements, the extra element is returned in the second slice." }
1224 { $examples { $example "USING: arrays sequences prettyprint kernel ;"
1225     "{ 1 2 3 4 5 } halves [ >array . ] bi@"
1226     "{ 1 2 }\n{ 3 4 5 }"
1227 } } ;
1228
1229 HELP: indices
1230 { $values
1231      { "obj" object } { "seq" sequence }
1232      { "indices" sequence } }
1233 { $description "Compares the input object to every element in the sequence and returns a vector containing the index of every position where the element was found." }
1234 { $examples { $example "USING: sequences prettyprint ;"
1235     "2 { 2 4 2 6 2 8 2 10 } indices ."
1236     "V{ 0 2 4 6 }"
1237 } } ;
1238
1239 HELP: insert-nth
1240 { $values
1241      { "elt" object } { "n" integer } { "seq" sequence }
1242      { "seq'" sequence } }
1243 { $description "Creates a new sequence where the " { $snippet "n" } "th index is set to the input object." }
1244 { $examples { $example "USING: prettyprint sequences ;"
1245     "40 3 { 10 20 30 50 } insert-nth ."
1246     "{ 10 20 30 40 50 }"
1247 } } ;
1248
1249 HELP: map-reduce
1250 { $values
1251      { "seq" sequence } { "map-quot" quotation } { "reduce-quot" quotation }
1252      { "result" object } }
1253 { $description "Unclips the first element of the sequence, calls " { $snippet "map-quot" } " on that element, and proceeds like a " { $link reduce } ", where the calculated element is the identity element and the rest of the sequence is the sequence to reduce." }
1254 { $examples { $example "USING: sequences prettyprint math ;"
1255     "{ 1 3 5 } [ sq ] [ + ] map-reduce ."
1256     "35"
1257 } } ;
1258
1259 HELP: new-like
1260 { $values
1261      { "len" integer } { "exemplar" "an exemplar sequence" } { "quot" quotation }
1262      { "seq" sequence } }
1263 { $description "Creates a new sequence of length " { $snippet "len" } " and calls the quotation with this sequence on the stack. The output of the quotation and the original exemplar are then passed to " { $link like } " so that the output sequence is the exemplar's type." } ;
1264
1265 HELP: push-either
1266 { $values
1267      { "elt" object } { "quot" quotation } { "accum1" vector } { "accum2" vector } }
1268 { $description "Pushes the input object onto one of the accumualators; the first if the quotation yields true, the second if false." } ;
1269
1270 HELP: sequence-hashcode
1271 { $values
1272      { "n" integer } { "seq" sequence }
1273      { "x" integer } }
1274 { $description "Iterates over a sequence, computes a hashcode with " { $link hashcode* } " for each element, and combines them using " { $link sequence-hashcode-step } "." } ;
1275
1276 HELP: sequence-hashcode-step
1277 { $values
1278      { "oldhash" integer } { "newpart" integer }
1279      { "newhash" integer } }
1280 { $description "An implementation word that computes a running hashcode of a sequence using some bit-twiddling. The resulting hashcode is always a fixnum." } ;
1281
1282 HELP: short
1283 { $values
1284      { "seq" sequence } { "n" integer }
1285      { "seq" sequence } { "n'" integer } }
1286 { $description "Returns the input sequence and its length or " { $snippet "n" } ", whichever is less." }
1287 { $examples { $example "USING: sequences kernel prettyprint ;"
1288     "\"abcd\" 3 short [ . ] bi@"
1289     "\"abcd\"\n3"
1290 } } ;
1291
1292 HELP: shorten
1293 { $values
1294      { "n" integer } { "seq" sequence } }
1295 { $description "Shortens a " { $link "growable" } " sequence to by " { $snippet "n" } " elements long." }
1296 { $examples { $example "USING: sequences prettyprint kernel ;"
1297     "V{ 1 2 3 4 5 } 3 over shorten ."
1298     "V{ 1 2 3 }"
1299 } } ;
1300
1301 HELP: iota
1302 { $values { "n" integer } { "iota" iota } }
1303 { $description "Creates an immutable virtual sequence containing the integers from 0 to " { $snippet "n-1" } "." }
1304 { $examples
1305   { $example
1306     "USING: math sequences prettyprint ;"
1307     "3 iota [ sq ] map ."
1308     "{ 0 1 4 }"
1309   }
1310 } ;
1311
1312 HELP: assert-sequence=
1313 { $values
1314     { "a" sequence } { "b" sequence }
1315 }
1316 { $description "Throws an error if all the elements of two sequences, taken pairwise, are not equal." }
1317 { $notes "The sequences need not be of the same type." }
1318 { $examples
1319   { $example
1320     "USING: prettyprint sequences ;"
1321     "{ 1 2 3 } V{ 1 2 3 } assert-sequence="
1322     ""
1323   }
1324 } ;
1325
1326 ARTICLE: "sequences-unsafe" "Unsafe sequence operations"
1327 "The " { $link nth-unsafe } " and " { $link set-nth-unsafe } " sequence protocol bypasses bounds checks for increased performance."
1328 $nl
1329 "These words assume the sequence index given is within bounds; if it is not, memory corruption can occur. Great care must be exercised when using these words. First, make sure the code in question is actually a bottleneck; next, try improving the algorithm first. If all else fails, then the unsafe sequence words can be used."
1330 $nl
1331 "There is a very important invariant these word must preserve: if at some point in time, the length of a sequence was " { $snippet "n" } ", then any future lookups of elements with indices below " { $snippet "n" } " must not crash the VM, even if the sequence length is now less than " { $snippet "n" } ". For example, vectors preserve this invariant by never shrinking the underlying storage, only growing it as necessary."
1332 $nl
1333 "The justification for this is that the VM should not crash if a resizable sequence is resized during the execution of an iteration combinator."
1334 $nl
1335 "Indeed, iteration combinators are the primary use-case for these words; if the iteration index is already guarded by a loop test which ensures it is within bounds, then additional bounds checks are redundant. For example, see the implementation of " { $link each } "." ;
1336
1337 ARTICLE: "sequence-protocol" "Sequence protocol"
1338 "All sequences must be instances of a mixin class:"
1339 { $subsection sequence }
1340 { $subsection sequence? }
1341 "All sequences must know their length:"
1342 { $subsection length }
1343 "At least one of the following two generic words must have a method for accessing elements; the " { $link sequence } " mixin has default definitions which are mutually recursive:"
1344 { $subsection nth }
1345 { $subsection nth-unsafe }
1346 "Note that sequences are always indexed starting from zero."
1347 $nl
1348 "At least one of the following two generic words must have a method for storing elements; the " { $link sequence } " mixin has default definitions which are mutually recursive:"
1349 { $subsection set-nth }
1350 { $subsection set-nth-unsafe }
1351 "Note that even if the sequence is immutable, at least one of the generic words must be specialized, otherwise calling them will result in an infinite recursion. There is a standard word which throws an error indicating a sequence is immutable:"
1352 { $subsection immutable }
1353 "The following two generic words are optional, as not all sequences are resizable:"
1354 { $subsection set-length }
1355 { $subsection lengthen }
1356 "An optional generic word for creating sequences of the same class as a given sequence:"
1357 { $subsection like }
1358 "Optional generic words for optimization purposes:"
1359 { $subsection new-sequence }
1360 { $subsection new-resizable }
1361 { $see-also "sequences-unsafe" } ;
1362
1363 ARTICLE: "virtual-sequences-protocol" "Virtual sequence protocol"
1364 "Virtual sequences must know their length:"
1365 { $subsection length }
1366 "The underlying sequence to look up a value in:"
1367 { $subsection virtual-seq }
1368 "The index of the value in the underlying sequence:"
1369 { $subsection virtual@ } ;
1370
1371 ARTICLE: "virtual-sequences" "Virtual sequences"
1372 "A virtual sequence is an implementation of the " { $link "sequence-protocol" } " which does not store its own elements, and instead computes them, either from scratch or by retrieving them from another sequence."
1373 $nl
1374 "Implementations include the following:"
1375 { $list
1376   { $link reversed }
1377   { $link slice }
1378   { $link iota }
1379 }
1380 "Virtual sequences can be implemented with the " { $link "virtual-sequences-protocol" } ", by translating an index in the virtual sequence into an index in another sequence:"
1381 { $subsection "virtual-sequences-protocol" } ;
1382
1383 ARTICLE: "sequences-integers" "Counted loops"
1384 "Integers support the sequence protocol in a trivial fashion; a non-negative integer presents its non-negative predecessors as elements. For example, the integer 3, when viewed as a sequence, contains the elements 0, 1, and 2. This is very useful for performing counted loops."
1385 $nl
1386 "For example, the " { $link each } " combinator, given an integer, simply calls a quotation that number of times, pushing a counter on each iteration that ranges from 0 up to that integer:"
1387 { $example "3 [ . ] each" "0\n1\n2" }
1388 "A common idiom is to iterate over a sequence, while also maintaining a loop counter. This can be done using " { $link each-index } ", " { $link map-index } " and " { $link reduce-index } "."
1389 $nl
1390 "Combinators that produce new sequences, such as " { $link map } ", will output an array if the input is an integer."
1391 $nl
1392 "More elaborate counted loops can be performed with " { $link "math.ranges" } "." ;
1393
1394 ARTICLE: "sequences-if" "Control flow with sequences"
1395 "To reduce the boilerplate of checking if a sequence is empty or a number is zero, several combinators are provided."
1396 $nl
1397 "Checking if a sequence is empty:"
1398 { $subsection if-empty }
1399 { $subsection when-empty }
1400 { $subsection unless-empty } ;
1401
1402 ARTICLE: "sequences-access" "Accessing sequence elements"
1403 { $subsection ?nth }
1404 "Concise way of extracting one of the first four elements:"
1405 { $subsection first }
1406 { $subsection second }
1407 { $subsection third }
1408 { $subsection fourth }
1409 "Extracting the last element:"
1410 { $subsection last }
1411 "Unpacking sequences:"
1412 { $subsection first2 }
1413 { $subsection first3 }
1414 { $subsection first4 }
1415 { $see-also nth } ;
1416
1417 ARTICLE: "sequences-add-remove" "Adding and removing sequence elements"
1418 "Adding elements:"
1419 { $subsection prefix }
1420 { $subsection suffix }
1421 "Removing elements:"
1422 { $subsection remove }
1423 { $subsection remq }
1424 { $subsection remove-nth } ;
1425
1426 ARTICLE: "sequences-reshape" "Reshaping sequences"
1427 "A " { $emphasis "repetition" } " is a virtual sequence consisting of a single element repeated multiple times:"
1428 { $subsection repetition }
1429 { $subsection <repetition> }
1430 "Reversing a sequence:"
1431 { $subsection reverse }
1432 "A " { $emphasis "reversal" } " presents a reversed view of an underlying sequence:"
1433 { $subsection reversed }
1434 { $subsection <reversed> }
1435 "Transposing a matrix:"
1436 { $subsection flip } ;
1437
1438 ARTICLE: "sequences-appending" "Appending sequences"
1439 { $subsection append }
1440 { $subsection append-as }
1441 { $subsection prepend }
1442 { $subsection 3append }
1443 { $subsection 3append-as }
1444 { $subsection surround }
1445 { $subsection glue }
1446 { $subsection concat }
1447 { $subsection join }
1448 "A pair of words useful for aligning strings:"
1449 { $subsection pad-head }
1450 { $subsection pad-tail } ;
1451
1452 ARTICLE: "sequences-slices" "Subsequences and slices"
1453 "There are two ways to extract a subrange of elements from a sequence. The first approach creates a new sequence of the same type as the input, which does not share storage with the underlying sequence. This takes time proportional to the number of elements being extracted. The second approach creates a " { $emphasis "slice" } ", which is a virtual sequence (see " { $link "virtual-sequences" } ") sharing storage with the original sequence. Slices are constructed in constant time."
1454 $nl
1455 "Some general guidelines for choosing between the two approaches:"
1456 { $list
1457   "If you are using mutable state, the choice has to be made one way or another because of semantics; mutating a slice will change the underlying sequence."
1458   { "Using a slice can improve algorithmic complexity. For example, if each iteration of a loop decomposes a sequence using " { $link first } " and " { $link rest } ", then the loop will run in quadratic time, relative to the length of the sequence. Using " { $link rest-slice } " changes the loop to run in linear time, since " { $link rest-slice } " does not copy any elements. Taking a slice of a slice will “collapse” the slice so to avoid the double indirection, so it is safe to use slices in recursive code." }
1459   "Accessing elements from a concrete sequence (such as a string or an array) is often faster than accessing elements from a slice, because slice access entails additional indirection. However, in some cases, if the slice is immediately consumed by an iteration combinator, the compiler can eliminate the slice allocation and indirect altogether."
1460   "If the slice outlives the original sequence, the original sequence will still remain in memory, since the slice will reference it. This can increase memory consumption unnecessarily."
1461 }
1462 { $heading "Subsequence operations" }
1463 "Extracting a subsequence:"
1464 { $subsection subseq }
1465 { $subsection head }
1466 { $subsection tail }
1467 { $subsection head* }
1468 { $subsection tail* }
1469 "Removing the first or last element:"
1470 { $subsection rest }
1471 { $subsection but-last }
1472 "Taking a sequence apart into a head and a tail:"
1473 { $subsection unclip }
1474 { $subsection unclip-last }
1475 { $subsection cut }
1476 { $subsection cut* }
1477 { $heading "Slice operations" }
1478 "The slice data type:"
1479 { $subsection slice }
1480 { $subsection slice? }
1481 "Extracting a slice:"
1482 { $subsection <slice> }
1483 { $subsection head-slice }
1484 { $subsection tail-slice }
1485 { $subsection head-slice* }
1486 { $subsection tail-slice* }
1487 "Removing the first or last element:"
1488 { $subsection rest-slice }
1489 { $subsection but-last-slice }
1490 "Taking a sequence apart into a head and a tail:"
1491 { $subsection unclip-slice }
1492 { $subsection unclip-last-slice }
1493 { $subsection cut-slice }
1494 "A utility for words which use slices as iterators:"
1495 { $subsection <flat-slice> }
1496 "Replacing slices with new elements:"
1497 { $subsection replace-slice } ;
1498
1499 ARTICLE: "sequences-combinators" "Sequence combinators"
1500 "Iteration:"
1501 { $subsection each }
1502 { $subsection each-index }
1503 { $subsection reduce }
1504 { $subsection interleave }
1505 { $subsection replicate }
1506 { $subsection replicate-as }
1507 "Mapping:"
1508 { $subsection map }
1509 { $subsection map-as }
1510 { $subsection map-index }
1511 { $subsection map-reduce }
1512 { $subsection accumulate }
1513 { $subsection produce }
1514 { $subsection produce-as }
1515 "Filtering:"
1516 { $subsection filter }
1517 { $subsection partition }
1518 "Testing if a sequence contains elements satisfying a predicate:"
1519 { $subsection any? }
1520 { $subsection all? }
1521 { $subsection "sequence-2combinators" }
1522 { $subsection "sequence-3combinators" } ;
1523
1524 ARTICLE: "sequence-2combinators" "Pair-wise sequence combinators"
1525 "There is a set of combinators which traverse two sequences pairwise. If one sequence is shorter than the other, then only the prefix having the length of the minimum of the two is examined."
1526 { $subsection 2each }
1527 { $subsection 2reduce }
1528 { $subsection 2map }
1529 { $subsection 2map-as }
1530 { $subsection 2map-reduce }
1531 { $subsection 2all? } ;
1532
1533 ARTICLE: "sequence-3combinators" "Triple-wise sequence combinators"
1534 "There is a set of combinators which traverse three sequences triple-wise. If one sequence is shorter than the others, then only the prefix having the length of the minimum of the three is examined."
1535 { $subsection 3each }
1536 { $subsection 3map }
1537 { $subsection 3map-as } ;
1538
1539 ARTICLE: "sequences-tests" "Testing sequences"
1540 "Testing for an empty sequence:"
1541 { $subsection empty? }
1542 "Testing indices:"
1543 { $subsection bounds-check? }
1544 "Testing if a sequence contains an object:"
1545 { $subsection member? }
1546 { $subsection memq? }
1547 "Testing if a sequence contains a subsequence:"
1548 { $subsection head? }
1549 { $subsection tail? }
1550 { $subsection subseq? } ;
1551
1552 ARTICLE: "sequences-search" "Searching sequences"
1553 "Finding the index of an element:"
1554 { $subsection index }
1555 { $subsection index-from }
1556 { $subsection last-index }
1557 { $subsection last-index-from }
1558 "Finding the start of a subsequence:"
1559 { $subsection start }
1560 { $subsection start* }
1561 "Finding the index of an element satisfying a predicate:"
1562 { $subsection find }
1563 { $subsection find-from }
1564 { $subsection find-last }
1565 { $subsection find-last-from }
1566 { $subsection map-find } ;
1567
1568 ARTICLE: "sequences-trimming" "Trimming sequences"
1569 "Trimming words:"
1570 { $subsection trim }
1571 { $subsection trim-head }
1572 { $subsection trim-tail }
1573 "Potentially more efficient trim:"
1574 { $subsection trim-slice }
1575 { $subsection trim-head-slice }
1576 { $subsection trim-tail-slice } ;
1577
1578 ARTICLE: "sequences-destructive-discussion" "When to use destructive operations"
1579 "Constructive (non-destructive) operations should be preferred where possible because code without side-effects is usually more re-usable and easier to reason about. There are two main reasons to use destructive operations:"
1580 { $list
1581     "For the side-effect. Some code is simpler to express with destructive operations; constructive operations return new objects, and sometimes ``threading'' the objects through the program manually complicates stack shuffling."
1582     { "As an optimization. Some code can be written to use constructive operations, however would suffer from worse performance. An example is a loop which adds an element to a sequence on each iteration; one could use either " { $link suffix } " or " { $link push } ", however the former copies the entire sequence first, which would cause the loop to run in quadratic time." }
1583 }
1584 "The second reason is much weaker than the first one. In particular, many combinators (see " { $link map } ", " { $link produce } " and " { $link "namespaces-make" } ") as well as more advanced data structures (such as " { $vocab-link "persistent.vectors" } ") alleviate the need for explicit use of side effects." ;
1585
1586 ARTICLE: "sequences-destructive" "Destructive operations"
1587 "These words modify their input, instead of creating a new sequence."
1588 { $subsection "sequences-destructive-discussion" }
1589 "Changing elements:"
1590 { $subsection change-each }
1591 { $subsection change-nth }
1592 "Deleting elements:"
1593 { $subsection delete }
1594 { $subsection delq }
1595 { $subsection delete-nth }
1596 { $subsection delete-slice }
1597 { $subsection delete-all }
1598 { $subsection filter-here }
1599 "Other destructive words:"
1600 { $subsection reverse-here }
1601 { $subsection push-all }
1602 { $subsection move }
1603 { $subsection exchange }
1604 { $subsection copy }
1605 "Many operations have constructive and destructive variants:"
1606 { $table
1607     { "Constructive" "Destructive" }
1608     { { $link suffix } { $link push } }
1609     { { $link but-last } { $link pop* } }
1610     { { $link unclip-last } { $link pop } }
1611     { { $link remove } { $link delete } }
1612     { { $link remq } { $link delq } }
1613     { { $link remove-nth } { $link delete-nth } }
1614     { { $link reverse } { $link reverse-here } }
1615     { { $link append } { $link push-all } }
1616     { { $link map } { $link change-each } }
1617     { { $link filter } { $link filter-here } }
1618 }
1619 { $see-also set-nth push pop "sequences-stacks" } ;
1620
1621 ARTICLE: "sequences-stacks" "Treating sequences as stacks"
1622 "The classical stack operations, modifying a sequence in place:"
1623 { $subsection push }
1624 { $subsection pop }
1625 { $subsection pop* }
1626 { $see-also empty? } ;
1627
1628 ARTICLE: "sequences-comparing" "Comparing sequences"
1629 "Element equality testing:"
1630 { $subsection sequence= }
1631 { $subsection mismatch }
1632 { $subsection drop-prefix }
1633 { $subsection assert-sequence= }
1634 "The " { $link <=> } " generic word performs lexicographic comparison when applied to sequences." ;
1635
1636 ARTICLE: "sequences-f" "The f object as a sequence"
1637 "The " { $link f } " object supports the sequence protocol in a trivial way. It responds with a length of zero and throws an out of bounds error when an attempt is made to access elements." ;
1638
1639 ARTICLE: "sequences" "Sequence operations"
1640 "A " { $emphasis "sequence" } " is a finite, linearly-ordered collection of elements. Words for working with sequences are in the " { $vocab-link "sequences" } " vocabulary."
1641 $nl
1642 "Sequences implement a protocol:"
1643 { $subsection "sequence-protocol" }
1644 { $subsection "sequences-f" }
1645 "Sequence utility words can operate on any object whose class implements the sequence protocol. Most implementations are backed by storage. Some implementations obtain their elements from an underlying sequence, or compute them on the fly. These are known as " { $link "virtual-sequences" } "."
1646 { $subsection "sequences-access" }
1647 { $subsection "sequences-combinators" }
1648 { $subsection "sequences-add-remove" }
1649 { $subsection "sequences-appending" }
1650 { $subsection "sequences-slices" }
1651 { $subsection "sequences-reshape" }
1652 { $subsection "sequences-tests" }
1653 { $subsection "sequences-search" }
1654 { $subsection "sequences-comparing" }
1655 { $subsection "sequences-split" }
1656 { $subsection "grouping" }
1657 { $subsection "sequences-destructive" }
1658 { $subsection "sequences-stacks" }
1659 { $subsection "sequences-sorting" }
1660 { $subsection "binary-search" }
1661 { $subsection "sets" }
1662 { $subsection "sequences-trimming" }
1663 { $subsection "sequences.deep" }
1664 "Using sequences for looping:"
1665 { $subsection "sequences-integers" }
1666 { $subsection "math.ranges" }
1667 "Using sequences for control flow:"
1668 { $subsection "sequences-if" }
1669 "For inner loops:"
1670 { $subsection "sequences-unsafe" } ;
1671
1672 ABOUT: "sequences"