]> gitweb.factorcode.org Git - factor.git/blob - core/sequences/sequences-docs.factor
Merge branch 'for-slava' of git://git.rfc1149.net/factor
[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.standard ;
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 { if-empty when-empty unless-empty } related-words
127
128 HELP: delete-all
129 { $values { "seq" "a resizable sequence" } }
130 { $description "Resizes the sequence to zero length, removing all elements. Not all sequences are resizable." }
131 { $errors "Throws a " { $link bounds-error } " if the new length is negative, or if the sequence is not resizable." }
132 { $side-effects "seq" } ;
133
134 HELP: resize
135 { $values { "n" "a non-negative integer" } { "seq" sequence } { "newseq" "a new sequence" } }
136 { $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." }
137 { $notes "This generic word is only implemented for strings and arrays." } ;
138
139 HELP: first
140 { $values { "seq" sequence } { "first" "the first element of the sequence" } }
141 { $description "Outputs the first element of the sequence." }
142 { $errors "Throws an error if the sequence is empty." } ;
143
144 HELP: second
145 { $values { "seq" sequence } { "second" "the second element of the sequence" } }
146 { $description "Outputs the second element of the sequence." }
147 { $errors "Throws an error if the sequence contains less than two elements." } ;
148
149 HELP: third
150 { $values { "seq" sequence } { "third" "the third element of the sequence" } }
151 { $description "Outputs the third element of the sequence." }
152 { $errors "Throws an error if the sequence contains less than three elements." } ;
153
154 HELP: fourth
155 { $values { "seq" sequence } { "fourth" "the fourth element of the sequence" } }
156 { $description "Outputs the fourth element of the sequence." }
157 { $errors "Throws an error if the sequence contains less than four elements." } ;
158
159 HELP: push
160 { $values { "elt" object } { "seq" "a resizable mutable sequence" } }
161 { $description "Adds an element at the end of the sequence. The sequence length is adjusted accordingly." }
162 { $errors "Throws an error if " { $snippet "seq" } " is not resizable, or if the type of " { $snippet "elt" } " is not permitted in " { $snippet "seq" } "." }
163 { $side-effects "seq" } ;
164
165 HELP: bounds-check?
166 { $values { "n" "an integer" } { "seq" sequence } { "?" "a boolean" } }
167 { $description "Tests if the index is within the bounds of the sequence." } ;
168
169 HELP: bounds-error
170 { $values { "n" "a positive integer" } { "seq" sequence } }
171 { $description "Throws a " { $link bounds-error } "." }
172 { $error-description "Thrown by " { $link nth } ", " { $link set-nth } " and " { $link set-length } " if the given index lies beyond the bounds of the sequence." } ;
173
174 HELP: bounds-check
175 { $values { "n" "a positive integer" } { "seq" sequence } }
176 { $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." } ;
177
178 HELP: ?nth
179 { $values { "n" "an integer" } { "seq" sequence } { "elt/f" "an object or " { $link f } } }
180 { $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 } "." } ;
181
182 HELP: nth-unsafe
183 { $values { "n" "an integer" } { "seq" sequence } { "elt" object } }
184 { $contract "Unsafe variant of " { $link nth } " that does not perform bounds checks." } ;
185
186 HELP: set-nth-unsafe
187 { $values { "elt" object } { "n" "an integer" } { "seq" sequence } }
188 { $contract "Unsafe variant of " { $link set-nth } " that does not perform bounds checks." } ;
189
190 HELP: exchange-unsafe
191 { $values { "m" "a non-negative integer" } { "n" "a non-negative integer" } { "seq" "a mutable sequence" } }
192 { $description "Unsafe variant of " { $link exchange } " that does not perform bounds checks." } ;
193
194 HELP: first-unsafe
195 { $values { "seq" sequence } { "first" "the first element" } }
196 { $contract "Unsafe variant of " { $link first } " that does not perform bounds checks." } ;
197
198 HELP: first2-unsafe
199 { $values { "seq" sequence } { "first" "the first element" } { "second" "the second element" } }
200 { $contract "Unsafe variant of " { $link first2 } " that does not perform bounds checks." } ;
201
202 HELP: first3-unsafe
203 { $values { "seq" sequence } { "first" "the first element" } { "second" "the second element" } { "third" "the third element" } }
204 { $contract "Unsafe variant of " { $link first3 } " that does not perform bounds checks." } ;
205
206 HELP: first4-unsafe
207 { $values { "seq" sequence } { "first" "the first element" } { "second" "the second element" } { "third" "the third element" } { "fourth" "the fourth element" } }
208 { $contract "Unsafe variant of " { $link first4 } " that does not perform bounds checks." } ;
209
210 HELP: 1sequence
211 { $values { "obj" object } { "exemplar" sequence } { "seq" sequence } }
212 { $description "Creates a one-element sequence of the same type as " { $snippet "exemplar" } "." } ;
213
214 HELP: 2sequence
215 { $values { "obj1" object } { "obj2" object } { "exemplar" sequence } { "seq" sequence } }
216 { $description "Creates a two-element sequence of the same type as " { $snippet "exemplar" } "." } ;
217
218 HELP: 3sequence
219 { $values { "obj1" object } { "obj2" object } { "obj3" object } { "exemplar" sequence } { "seq" sequence } }
220 { $description "Creates a three-element sequence of the same type as " { $snippet "exemplar" } "." } ;
221
222 HELP: 4sequence
223 { $values { "obj1" object } { "obj2" object } { "exemplar" sequence } { "obj3" object } { "obj4" object } { "seq" sequence } }
224 { $description "Creates a four-element sequence of the same type as " { $snippet "exemplar" } "." } ;
225
226 HELP: first2
227 { $values { "seq" sequence } { "first" "the first element" } { "second" "the second element" } }
228 { $description "Pushes the first two elements of a sequence." }
229 { $errors "Throws an error if the sequence has less than two elements." } ;
230
231 HELP: first3
232 { $values { "seq" sequence } { "first" "the first element" } { "second" "the second element" } { "third" "the third element" } }
233 { $description "Pushes the first three elements of a sequence." }
234 { $errors "Throws an error if the sequence has less than three elements." } ;
235
236 HELP: first4
237 { $values { "seq" sequence } { "first" "the first element" } { "second" "the second element" } { "third" "the third element" } { "fourth" "the fourth element" } }
238 { $description "Pushes the first four elements of a sequence." }
239 { $errors "Throws an error if the sequence has less than four elements." } ;
240
241 HELP: array-capacity
242 { $values { "array" "an array" } { "n" "a non-negative fixnum" } }
243 { $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 } "." }
244 { $description "Low-level array length accessor." }
245 { $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." } ;
246
247 HELP: array-nth
248 { $values { "n" "a non-negative fixnum" } { "array" "an array" }  { "elt" object } }
249 { $description "Low-level array element accessor." }
250 { $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." } ;
251
252 HELP: set-array-nth
253 { $values { "elt" object } { "n" "a non-negative fixnum" } { "array" "an array" }  }
254 { $description "Low-level array element mutator." }
255 { $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." } ;
256
257 HELP: collect
258 { $values { "n" "a non-negative integer" } { "quot" { $quotation "( n -- value )" } } { "into" "a sequence of length at least " { $snippet "n" } } }
259 { $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." } ;
260
261 HELP: each
262 { $values { "seq" sequence } { "quot" { $quotation "( elt -- )" } } }
263 { $description "Applies the quotation to each element of the sequence in order." } ;
264
265 HELP: reduce
266 { $values { "seq" sequence } { "identity" object } { "quot" { $quotation "( prev elt -- next )" } } { "result" "the final result" } }
267 { $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." }
268 { $examples
269     { $example "USING: math prettyprint sequences ;" "{ 1 5 3 } 0 [ + ] reduce ." "9" }
270 } ;
271
272 HELP: reduce-index
273 { $values
274      { "seq" sequence } { "identity" object } { "quot" quotation } }
275 { $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." }
276 { $examples { $example "USING: sequences prettyprint math ;"
277     "{ 10 50 90 } 0 [ + + ] reduce-index ."
278     "153"
279 } } ;
280
281 HELP: accumulate
282 { $values { "identity" object } { "seq" sequence } { "quot" { $quotation "( prev elt -- next )" } } { "final" "the final result" } { "newseq" "a new sequence" } }
283 { $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."
284 $nl
285 "When given the empty sequence, outputs an empty sequence together with the " { $snippet "identity" } "." }
286 { $examples
287     { $example "USING: math prettyprint sequences ;" "{ 2 2 2 2 2 } 0 [ + ] accumulate . ." "{ 0 2 4 6 8 }\n10" }
288 } ;
289
290 HELP: map
291 { $values { "seq" sequence } { "quot" { $quotation "( old -- new )" } } { "newseq" "a new sequence" } }
292 { $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." } ;
293
294 HELP: map-as
295 { $values { "seq" sequence } { "quot" { $quotation "( old -- new )" } } { "newseq" "a new sequence" } { "exemplar" sequence } }
296 { $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" } "." }
297 { $examples
298     "The following example converts a string into an array of one-element strings:"
299     { $example "USING: prettyprint strings sequences ;" "\"Hello\" [ 1string ] { } map-as ." "{ \"H\" \"e\" \"l\" \"l\" \"o\" }" }
300     "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."
301 } ;
302
303 HELP: each-index
304 { $values
305      { "seq" sequence } { "quot" quotation } }
306 { $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." }
307 { $examples { $example "USING: sequences prettyprint math ;"
308 "{ 10 20 30 } [ + . ] each-index"
309 "10\n21\n32"
310 } } ;
311
312 HELP: map-index
313 { $values
314      { "seq" sequence } { "quot" quotation } }
315 { $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." }
316 { $examples { $example "USING: sequences prettyprint math ;"
317 "{ 10 20 30 } [ + ] map-index ."
318 "{ 10 21 32 }"
319 } } ;
320
321 HELP: change-nth
322 { $values { "i" "a non-negative integer" } { "seq" "a mutable sequence" } { "quot" { $quotation "( elt -- newelt )" } } }
323 { $description "Applies the quotation to the " { $snippet "i" } "th element of the sequence, storing the result back into the sequence." }
324 { $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" } "." }
325 { $side-effects "seq" } ;
326
327 HELP: change-each
328 { $values { "seq" "a mutable sequence" } { "quot" { $quotation "( old -- new )" } } }
329 { $description "Applies the quotation to each element yielding a new element, storing the new elements back in the original sequence." }
330 { $errors "Throws an error if the sequence is immutable, or the sequence cannot hold elements of the type output by " { $snippet "quot" } "." }
331 { $side-effects "seq" } ;
332
333 HELP: min-length
334 { $values { "seq1" sequence } { "seq2" sequence } { "n" "a non-negative integer" } }
335 { $description "Outputs the minimum of the lengths of the two sequences." } ;
336
337 HELP: max-length
338 { $values { "seq1" sequence } { "seq2" sequence } { "n" "a non-negative integer" } }
339 { $description "Outputs the maximum of the lengths of the two sequences." } ;
340
341 HELP: 2each
342 { $values { "seq1" sequence } { "seq2" sequence } { "quot" { $quotation "( elt1 elt2 -- )" } } }
343 { $description "Applies the quotation to pairs of elements from " { $snippet "seq1" } " and " { $snippet "seq2" } "." } ;
344
345 HELP: 3each
346 { $values { "seq1" sequence } { "seq2" sequence } { "seq3" sequence } { "quot" { $quotation "( elt1 elt2 elt3 -- )" } } }
347 { $description "Applies the quotation to triples of elements from " { $snippet "seq1" } ", " { $snippet "seq2" } " and " { $snippet "seq3" } "." } ;
348
349 HELP: 2reduce
350 { $values { "seq1" sequence }
351           { "seq2" sequence }
352           { "identity" object }
353           { "quot" { $quotation "( prev elt1 elt2 -- next )" } }
354           { "result" "the final result" } }
355 { $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" } "." } ;
356
357 HELP: 2map
358 { $values { "seq1" sequence } { "seq2" sequence } { "quot" { $quotation "( elt1 elt2 -- new )" } } { "newseq" "a new sequence" } }
359 { $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" } "." } ;
360
361 HELP: 3map
362 { $values { "seq1" sequence } { "seq2" sequence } { "seq3" sequence } { "quot" { $quotation "( elt1 elt2 elt3 -- new )" } } { "newseq" "a new sequence" } }
363 { $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" } "." } ;
364
365 HELP: 2map-as
366 { $values { "seq1" sequence } { "seq2" sequence } { "quot" { $quotation "( elt1 elt2 -- new )" } } { "exemplar" sequence } { "newseq" "a new sequence" } }
367 { $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" } "." } ;
368
369 HELP: 3map-as
370 { $values { "seq1" sequence } { "seq2" sequence } { "seq3" sequence } { "quot" { $quotation "( elt1 elt2 elt3 -- new )" } } { "exemplar" sequence } { "newseq" "a new sequence" } }
371 { $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" } "." } ;
372
373 HELP: 2all?
374 { $values { "seq1" sequence } { "seq2" sequence } { "quot" { $quotation "( elt1 elt2 -- ? )" } } { "?" "a boolean" } }
375 { $description "Tests the predicate pairwise against elements of " { $snippet "seq1" } " and " { $snippet "seq2" } "." } ;
376
377 HELP: find
378 { $values { "seq" sequence }
379           { "quot" { $quotation "( elt -- ? )" } }
380           { "i" "the index of the first match, or " { $link f } }
381           { "elt" "the first matching element, or " { $link f } } }
382 { $description "A simpler variant of " { $link find-from } " where the starting index is 0." } ;
383
384 HELP: find-from
385 { $values { "n" "a starting index" }
386           { "seq" sequence }
387           { "quot" { $quotation "( elt -- ? )" } }
388           { "i" "the index of the first match, or " { $link f } }
389           { "elt" "the first matching element, or " { $link f } } }
390 { $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." } ;
391
392 HELP: find-last
393 { $values { "seq" sequence } { "quot" { $quotation "( elt -- ? )" } } { "i" "the index of the first match, or f" } { "elt" "the first matching element, or " { $link f } } }
394 { $description "A simpler variant of " { $link find-last-from } " where the starting index is one less than the length of the sequence." } ;
395
396 HELP: find-last-from
397 { $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 } } }
398 { $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." } ;
399
400 HELP: any?
401 { $values { "seq" sequence } { "quot" { $quotation "( elt -- ? )" } } { "?" "a boolean" } }
402 { $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 } "." } ;
403
404 HELP: all?
405 { $values { "seq" sequence } { "quot" { $quotation "( elt -- ? )" } } { "?" "a boolean" } }
406 { $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 } "." } ;
407
408 HELP: push-if
409 { $values { "elt" object } { "quot" { $quotation "( elt -- ? )" } } { "accum" "a resizable mutable sequence" } }
410 { $description "Adds the element at the end of the sequence if the quotation yields a true value." } 
411 { $notes "This word is a factor of " { $link filter } "." } ;
412
413 HELP: filter
414 { $values { "seq" sequence } { "quot" { $quotation "( elt -- ? )" } } { "subseq" "a new sequence" } }
415 { $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." } ;
416
417 HELP: filter-here
418 { $values { "seq" "a resizable mutable sequence" } { "quot" { $quotation "( elt -- ? )" } } }
419 { $description "Applies the quotation to each element in turn, and removes elements for which the quotation outputs a false value." }
420 { $side-effects "seq" } ;
421
422 HELP: interleave
423 { $values { "seq" sequence } { "between" "a quotation" } { "quot" { $quotation "( elt -- )" } } }
424 { $description "Applies " { $snippet "quot" } " to each element in turn, also invoking " { $snippet "between" } " in-between each pair of elements." }
425 { $example "USING: io sequences ;" "{ \"a\" \"b\" \"c\" } [ \"X\" write ] [ write ] interleave" "aXbXc" } ;
426
427 HELP: index
428 { $values { "obj" object } { "seq" sequence } { "n" "an index" } }
429 { $description "Outputs the index of the first element in the sequence equal to " { $snippet "obj" } ". If no element is found, outputs " { $link f } "." } ;
430
431 HELP: index-from
432 { $values { "obj" object } { "i" "a start index" } { "seq" sequence } { "n" "an index" } }
433 { $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 } "." } ;
434
435 HELP: last-index
436 { $values { "obj" object } { "seq" sequence } { "n" "an index" } }
437 { $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 } "." } ;
438
439 HELP: last-index-from
440 { $values { "obj" object } { "i" "a start index" } { "seq" sequence } { "n" "an index" } }
441 { $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 } "." } ;
442
443 HELP: member?
444 { $values { "elt" object } { "seq" sequence } { "?" "a boolean" } }
445 { $description "Tests if the sequence contains an element equal to the object." }
446 { $notes "This word uses equality comparison (" { $link = } ")." } ;
447
448 HELP: memq?
449 { $values { "elt" object } { "seq" sequence } { "?" "a boolean" } }
450 { $description "Tests if the sequence contains the object." }
451 { $notes "This word uses identity comparison (" { $link eq? } ")." } ;
452
453 HELP: remove
454 { $values { "elt" object } { "seq" sequence } { "newseq" "a new sequence" } }
455 { $description "Outputs a new sequence containing all elements of the input sequence except for given element." }
456 { $notes "This word uses equality comparison (" { $link = } ")." } ;
457
458 HELP: remq
459 { $values { "elt" object } { "seq" sequence } { "newseq" "a new sequence" } }
460 { $description "Outputs a new sequence containing all elements of the input sequence except those equal to the given element." }
461 { $notes "This word uses identity comparison (" { $link eq? } ")." } ;
462
463 HELP: remove-nth
464 { $values
465      { "n" integer } { "seq" sequence }
466      { "seq'" sequence } }
467 { $description "Creates a new sequence without the element at index " { $snippet "n" } "." }
468 { $examples "Notice that the original sequence is left intact:" { $example "USING: sequences prettyprint kernel ;"
469     "{ 1 2 3 } 1 over remove-nth . ."
470     "{ 1 3 }\n{ 1 2 3 }"
471 } } ;
472
473 HELP: move
474 { $values { "from" "an index in " { $snippet "seq" } } { "to" "an index in " { $snippet "seq" } } { "seq" "a mutable sequence" } }
475 { $description "Sets the element with index " { $snippet "m" } " to the element with index " { $snippet "n" } "." }
476 { $side-effects "seq" } ;
477
478 HELP: delete
479 { $values { "elt" object } { "seq" "a resizable mutable sequence" } }
480 { $description "Removes all elements equal to " { $snippet "elt" } " from " { $snippet "seq" } "." }
481 { $notes "This word uses equality comparison (" { $link = } ")." }
482 { $side-effects "seq" } ;
483
484 HELP: delq
485 { $values { "elt" object } { "seq" "a resizable mutable sequence" } }
486 { $description "Outputs a new sequence containing all elements of the input sequence except the given element." }
487 { $notes "This word uses identity comparison (" { $link eq? } ")." }
488 { $side-effects "seq" } ;
489
490 HELP: delete-nth
491 { $values { "n" "a non-negative integer" } { "seq" "a resizable mutable sequence" } }
492 { $description "Removes the " { $snippet "n" } "th element from the sequence, shifting all other elements down and reducing its length by one." }
493 { $side-effects "seq" } ;
494
495 HELP: delete-slice
496 { $values { "from" "a non-negative integer" } { "to" "a non-negative integer" } { "seq" "a resizable mutable sequence" } }
497 { $description "Removes a range of elements beginning at index " { $snippet "from" } " and ending before index " { $snippet "to" } "." }
498 { $side-effects "seq" } ;
499
500 HELP: replace-slice
501 { $values { "new" sequence } { "seq" "a mutable sequence" } { "from" "a non-negative integer" } { "to" "a non-negative integer" } }
502 { $description "Replaces a range of elements beginning at index " { $snippet "from" } " and ending before index " { $snippet "to" } " with a new sequence." }
503 { $notes "If the " { $snippet "to - from" } " is equal to the length of " { $snippet "new" } ", the sequence remains the same size, and does not have to support resizing. However, if " { $snippet "to - from" } " is not equal to the length of " { $snippet "new" } ", the " { $link set-length } " word is called on " { $snippet "seq" } ", so fixed-size sequences should not be passed in this case." }
504 { $errors "Throws an error if " { $snippet "new" } " contains elements whose types are not permissible in " { $snippet "seq" } "." }
505 { $side-effects "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: join
535 { $values { "seq" sequence } { "glue" sequence } { "newseq" sequence } }
536 { $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" } "." }
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 "glue" } "." } ;
538
539 { join concat } related-words
540
541 HELP: peek
542 { $values { "seq" sequence } { "elt" object } }
543 { $description "Outputs the last element of a sequence." }
544 { $errors "Throws an error if the sequence is empty." } ;
545
546 { peek pop pop* } related-words
547
548 HELP: pop*
549 { $values { "seq" "a resizable mutable sequence" } }
550 { $description "Removes the last element and shortens the sequence." }
551 { $side-effects "seq" }
552 { $errors "Throws an error if the sequence is empty." } ;
553
554 HELP: pop
555 { $values { "seq" "a resizable mutable sequence" } { "elt" object } }
556 { $description "Outputs the last element after removing it and shortening the sequence." }
557 { $side-effects "seq" }
558 { $errors "Throws an error if the sequence is empty." } ;
559
560 HELP: mismatch
561 { $values { "seq1" sequence } { "seq2" sequence } { "i" "an index" } }
562 { $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." } ;
563
564 HELP: flip
565 { $values { "matrix" "a sequence of equal-length sequences" } { "newmatrix" "a sequence of equal-length sequences" } }
566 { $description "Transposes the matrix; that is, rows become columns and columns become rows." }
567 { $examples { $example "USING: prettyprint sequences ;" "{ { 1 2 3 } { 4 5 6 } } flip ." "{ { 1 4 } { 2 5 } { 3 6 } }" } } ;
568
569 HELP: exchange
570 { $values { "m" "a non-negative integer" } { "n" "a non-negative integer" } { "seq" "a mutable sequence" } }
571 { $description "Exchanges the " { $snippet "m" } "th and " { $snippet "n" } "th elements of " { $snippet "seq" } "." } ;
572
573 HELP: reverse-here
574 { $values { "seq" "a mutable sequence" } }
575 { $description "Reverses a sequence in-place." }
576 { $side-effects "seq" } ;
577
578 HELP: padding
579 { $values { "seq" sequence } { "n" "a non-negative integer" } { "elt" object } { "quot" { $quotation "( seq1 seq2 -- newseq )" } } { "newseq" "a new sequence" } }
580 { $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." } ;
581
582 HELP: pad-head
583 { $values { "seq" sequence } { "n" "a non-negative integer" } { "elt" object } { "padded" "a new sequence" } }
584 { $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" } "." }
585 { $examples { $example "USING: io sequences ;" "{ \"ab\" \"quux\" } [ 5 CHAR: - pad-head print ] each" "---ab\n-quux" } } ;
586
587 HELP: pad-tail
588 { $values { "seq" sequence } { "n" "a non-negative integer" } { "elt" object } { "padded" "a new sequence" } }
589 { $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" } "." }
590 { $examples { $example "USING: io sequences ;" "{ \"ab\" \"quux\" } [ 5 CHAR: - pad-tail print ] each" "ab---\nquux-" } } ;
591
592 HELP: sequence=
593 { $values { "seq1" sequence } { "seq2" sequence } { "?" "a boolean" } }
594 { $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." } ;
595
596 HELP: reversed
597 { $class-description "A virtual sequence which presents a reversed view of an underlying sequence. New instances can be created by calling " { $link <reversed> } "." } ;
598
599 HELP: reverse
600 { $values { "seq" sequence } { "newseq" "a new sequence" } }
601 { $description "Outputs a new sequence having the same elements as " { $snippet "seq" } " but in reverse order." } ;
602
603 { reverse <reversed> reverse-here } related-words
604
605 HELP: <reversed>
606 { $values { "seq" sequence } { "reversed" "a new sequence" } }
607 { $description "Creates an instance of the " { $link reversed } " class." }
608 { $see-also "virtual-sequences" } ;
609
610 HELP: slice-error
611 { $values { "str" "a reason" } }
612 { $description "Throws a " { $link slice-error } "." }
613 { $error-description "Thrown by " { $link <slice> } " if one of the following invalid conditions holds:"
614     { $list
615         "The start index is negative"
616         "The end index is greater than the length of the sequence"
617         "The start index is greater than the end index"
618     }
619 } ;
620
621 HELP: slice
622 { $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> } "."
623 $nl
624 "Slices are mutable if the underlying sequence is mutable, and mutating a slice changes the underlying sequence. However, slices cannot be resized after creation." } ;
625
626 HELP: check-slice
627 { $values { "from" "a non-negative integer" } { "to" "a non-negative integer" } { "seq" sequence } }
628 { $description "Ensures that " { $snippet "m" } " is less than or equal to " { $snippet "m" } ", and that both indices are within bounds for " { $snippet "seq" } "." }
629 { $errors "Throws a " { $link slice-error } " if the preconditions are not met." } ;
630
631 HELP: collapse-slice
632 { $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 } }
633 { $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." }
634 ;
635
636 HELP: <flat-slice>
637 { $values { "seq" sequence } { "slice" slice } }
638 { $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" } "." }
639 { $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." } ;
640
641 HELP: <slice>
642 { $values { "from" "a non-negative integer" } { "to" "a non-negative integer" } { "seq" sequence } { "slice" slice } }
643 { $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" } "." }
644 { $errors "Throws an error if " { $snippet "m" } " or " { $snippet "n" } " is out of bounds." }
645 { $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." } ;
646
647 { <slice> subseq } related-words
648
649 HELP: repetition
650 { $class-description "A virtual sequence consisting of " { $snippet "elt" } " repeated " { $snippet "len" } " times. Repetitions are created by calling " { $link <repetition> } "." } ;
651
652 HELP: <repetition>
653 { $values { "len" "a non-negative integer" } { "elt" object } { "repetition" repetition } }
654 { $description "Creates a new " { $link repetition } "." }
655 { $examples
656     { $example "USING: arrays prettyprint sequences ;" "10 \"X\" <repetition> >array ." "{ \"X\" \"X\" \"X\" \"X\" \"X\" \"X\" \"X\" \"X\" \"X\" \"X\" }" }
657     { $example "USING: prettyprint sequences ;" "10 \"X\" <repetition> concat ." "\"XXXXXXXXXX\"" }
658 } ;
659 HELP: copy
660 { $values { "src" sequence } { "i" "an index in " { $snippet "dest" } } { "dst" "a mutable sequence" } }
661 { $description "Copies all elements of " { $snippet "src" } " to " { $snippet "dest" } ", with destination indices starting from " { $snippet "i" } ". Grows " { $snippet "to" } " first if necessary." }
662 { $side-effects "dest" }
663 { $errors "An error is thrown if " { $snippet "to" } " is not resizable, and not large enough to hold the copied elements." } ;
664
665 HELP: push-all
666 { $values { "src" sequence } { "dest" "a resizable mutable sequence" } }
667 { $description "Appends " { $snippet "src" } " to the end of " { $snippet "dest" } "." }
668 { $side-effects "dest" }
669 { $errors "Throws an error if " { $snippet "src" } " contains elements not permitted in " { $snippet "dest" } "." } ;
670
671 HELP: append
672 { $values { "seq1" sequence } { "seq2" sequence } { "newseq" sequence } }
673 { $description "Outputs a new sequence of the same type as " { $snippet "seq1" } " consisting of the elements of " { $snippet "seq1" } " followed by " { $snippet "seq2" } "." }
674 { $errors "Throws an error if " { $snippet "seq2" } " contains elements not permitted in sequences of the same class as " { $snippet "seq1" } "." }
675 { $examples 
676     { $example "USING: prettyprint sequences ;"
677         "{ 1 2 } B{ 3 4 } append ."
678         "{ 1 2 3 4 }"
679     }
680     { $example "USING: prettyprint sequences strings ;"
681         "\"go\" \"ing\" append ."
682         "\"going\""
683     }
684 } ;
685
686 HELP: append-as
687 { $values { "seq1" sequence } { "seq2" sequence } { "exemplar" sequence } { "newseq" sequence } }
688 { $description "Outputs a new sequence of the same type as " { $snippet "exemplar" } " consisting of the elements of " { $snippet "seq1" } " followed by " { $snippet "seq2" } "." }
689 { $errors "Throws an error if " { $snippet "seq1" } " or " { $snippet "seq2" } " contain elements not permitted in sequences of the same class as " { $snippet "exemplar" } "." }
690 { $examples 
691     { $example "USING: prettyprint sequences ;"
692         "{ 1 2 } B{ 3 4 } B{ } append-as ."
693         "B{ 1 2 3 4 }"
694     }
695     { $example "USING: prettyprint sequences strings ;"
696         "\"go\" \"ing\" SBUF\" \" append-as ."
697         "SBUF\" going\""
698     }
699 } ;
700
701 { append append-as } related-words
702
703 HELP: prepend
704 { $values { "seq1" sequence } { "seq2" sequence } { "newseq" sequence } }
705 { $description "Outputs a new sequence of the same type as " { $snippet "seq2" } " consisting of the elements of " { $snippet "seq2" } " followed by " { $snippet "seq1" } "." }
706 { $errors "Throws an error if " { $snippet "seq1" } " contains elements not permitted in sequences of the same class as " { $snippet "seq2" } "." }
707 { $examples { $example "USING: prettyprint sequences ;"
708         "{ 1 2 } B{ 3 4 } prepend ."
709         "B{ 3 4 1 2 }"
710     }
711     { $example "USING: prettyprint sequences strings ;"
712         "\"go\" \"car\" prepend ."
713         "\"cargo\""
714     }
715 } ;
716
717 HELP: 3append
718 { $values { "seq1" sequence } { "seq2" sequence } { "seq3" sequence } { "newseq" sequence } }
719 { $description "Outputs a new sequence consisting of the elements of " { $snippet "seq1" } ", " { $snippet "seq2" } " and " { $snippet "seq3" } " in turn." }
720 { $errors "Throws an error if " { $snippet "seq2" } " or " { $snippet "seq3" } " contain elements not permitted in sequences of the same class as " { $snippet "seq1" } "." }
721 { $examples
722     { $example "USING: prettyprint sequences ;"
723         "\"a\" \"b\" \"c\" 3append ."
724         "\"abc\""
725     }
726 } ;
727
728 HELP: 3append-as
729 { $values { "seq1" sequence } { "seq2" sequence } { "seq3" sequence } { "exemplar" sequence } { "newseq" sequence } }
730 { $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" } "." }
731 { $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" } "." }
732 { $examples
733     { $example "USING: prettyprint sequences ;"
734         "\"a\" \"b\" \"c\" SBUF\" \" 3append-as ."
735         "SBUF\" abc\""
736     }
737 } ;
738
739 { 3append 3append-as } related-words
740
741 HELP: surround
742 { $values { "seq1" sequence } { "seq2" sequence } { "seq3" sequence } { "newseq" sequence } }
743 { $description "Outputs a new sequence with " { $snippet "seq1" } " inserted between " { $snippet "seq2" } " and " { $snippet "seq3" } "." }
744 { $examples
745     { $example "USING: sequences prettyprint ;"
746                "\"sssssh\" \"(\" \")\" surround ."
747                "\"(sssssh)\""
748     }
749 } ;
750
751 HELP: glue
752 { $values { "seq1" sequence } { "seq2" sequence } { "seq3" sequence } { "newseq" sequence } }
753 { $description "Outputs a new sequence with " { $snippet "seq3" } " inserted between " { $snippet "seq1" } " and " { $snippet "seq2" } "." }
754 { $examples
755     { $example "USING: sequences prettyprint ;"
756                "\"a\" \"b\" \",\" glue ."
757                "\"a,b\""
758     }
759 } ;
760
761 HELP: subseq
762 { $values { "from" "a non-negative integer" } { "to" "a non-negative integer" } { "seq" sequence } { "subseq" "a new sequence" } }
763 { $description "Outputs a new sequence consisting of all elements starting from and including " { $snippet "from" } ", and up to but not including " { $snippet "to" } "." }
764 { $errors "Throws an error if " { $snippet "from" } " or " { $snippet "to" } " is out of bounds." } ;
765
766 HELP: clone-like
767 { $values { "seq" sequence } { "exemplar" sequence } { "newseq" "a new sequence" } }
768 { $description "Outputs a newly-allocated sequence with the same elements as " { $snippet "seq" } " but of the same type as " { $snippet "exemplar" } "." }
769 { $notes "Unlike " { $link like } ", this word always creates a new sequence which never shares storage with the original." } ;
770
771 HELP: head-slice
772 { $values { "seq" sequence } { "n" "a non-negative integer" } { "slice" "a slice" } }
773 { $description "Outputs a virtual sequence sharing storage with the first " { $snippet "n" } " elements of the input sequence." }
774 { $errors "Throws an error if the index is out of bounds." } ;
775
776 HELP: tail-slice
777 { $values { "seq" sequence } { "n" "a non-negative integer" } { "slice" "a slice" } }
778 { $description "Outputs a virtual sequence sharing storage with all elements from the " { $snippet "n" } "th index until the end of the input sequence." }
779 { $errors "Throws an error if the index is out of bounds." } ;
780
781 HELP: but-last-slice
782 { $values { "seq" sequence } { "slice" "a slice" } }
783 { $description "Outputs a virtual sequence sharing storage with all but the last element of the input sequence." }
784 { $errors "Throws an error on an empty sequence." } ;
785
786 HELP: rest-slice
787 { $values { "seq" sequence } { "slice" "a slice" } }
788 { $description "Outputs a virtual sequence sharing storage with all elements from the 1st index until the end of the input sequence." }
789 { $notes "Equivalent to " { $snippet "1 tail" } }
790 { $errors "Throws an error on an empty sequence." } ;
791
792 HELP: head-slice*
793 { $values { "seq" sequence } { "n" "a non-negative integer" } { "slice" "a slice" } }
794 { $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." }
795 { $errors "Throws an error if the index is out of bounds." } ;
796
797 HELP: tail-slice*
798 { $values { "seq" sequence } { "n" "a non-negative integer" } { "slice" "a slice" } }
799 { $description "Outputs a virtual sequence sharing storage with the last " { $snippet "n" } " elements of the input sequence." }
800 { $errors "Throws an error if the index is out of bounds." } ;
801
802 HELP: head
803 { $values { "seq" sequence } { "n" "a non-negative integer" } { "headseq" "a new sequence" } }
804 { $description "Outputs a new sequence consisting of the first " { $snippet "n" } " elements of the input sequence." }
805 { $errors "Throws an error if the index is out of bounds." } ;
806
807 HELP: tail
808 { $values { "seq" sequence } { "n" "a non-negative integer" } { "tailseq" "a new sequence" } }
809 { $description "Outputs a new sequence consisting of the input sequence with the first n items removed." }
810 { $errors "Throws an error if the index is out of bounds." } ;
811
812 HELP: but-last
813 { $values { "seq" sequence } { "headseq" "a new sequence" } }
814 { $description "Outputs a new sequence consisting of the input sequence with the last item removed." }
815 { $errors "Throws an error on an empty sequence." } ;
816
817 HELP: rest
818 { $values { "seq" sequence } { "tailseq" "a new sequence" } }
819 { $description "Outputs a new sequence consisting of the input sequence with the first item removed." }
820 { $errors "Throws an error on an empty sequence." } ;
821
822 HELP: head*
823 { $values { "seq" sequence } { "n" "a non-negative integer" } { "headseq" "a new sequence" } }
824 { $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." }
825 { $errors "Throws an error if the index is out of bounds." } ;
826
827 HELP: tail*
828 { $values { "seq" sequence } { "n" "a non-negative integer" } { "tailseq" "a new sequence" } }
829 { $description "Outputs a new sequence consisting of the last " { $snippet "n" } " elements of the input sequence." }
830 { $errors "Throws an error if the index is out of bounds." } ;
831
832 HELP: shorter?
833 { $values { "seq1" sequence } { "seq2" sequence } { "?" "a boolean" } }
834 { $description "Tets if the length of " { $snippet "seq1" } " is smaller than the length of " { $snippet "seq2" } "." } ;
835
836 HELP: head?
837 { $values { "seq" sequence } { "begin" sequence } { "?" "a boolean" } }
838 { $description "Tests if " { $snippet "seq" } " starts with " { $snippet "begin" } ". If " { $snippet "begin" } " is longer than " { $snippet "seq" } ", this word outputs " { $link f } "." } ;
839
840 HELP: tail?
841 { $values { "seq" sequence } { "end" sequence } { "?" "a boolean" } }
842 { $description "Tests if " { $snippet "seq" } " ends with " { $snippet "end" } ". If " { $snippet "end" } " is longer than " { $snippet "seq" } ", this word outputs " { $link f } "." } ;
843
844 { remove remove-nth remq delq delete delete-nth } related-words
845
846 HELP: cut-slice
847 { $values { "seq" sequence } { "n" "a non-negative integer" } { "before-slice" sequence } { "after-slice" "a slice" } }
848 { $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." }
849 { $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." } ;
850
851 HELP: cut
852 { $values { "seq" sequence } { "n" "a non-negative integer" } { "before" sequence } { "after" sequence } }
853 { $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" } "." }
854 { $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." } ;
855
856 HELP: cut*
857 { $values { "seq" sequence } { "n" "a non-negative integer" } { "before" sequence } { "after" sequence } }
858 { $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" } "." } ;
859
860 HELP: start*
861 { $values { "subseq" sequence } { "seq" sequence } { "n" "a start index" } { "i" "a start index" } }
862 { $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 } "." } ;
863
864 HELP: start
865 { $values { "subseq" sequence } { "seq" sequence } { "i" "a start index" } }
866 { $description "Outputs the start index of the first contiguous subsequence equal to " { $snippet "subseq" } ", or " { $link f } " if no matching subsequence is found." } ;
867
868 HELP: subseq?
869 { $values { "subseq" sequence } { "seq" sequence } { "?" "a boolean" } }
870 { $description "Tests if " { $snippet "seq" } " contains the elements of " { $snippet "subseq" } " as a contiguous subsequence." } ;
871
872 HELP: drop-prefix
873 { $values { "seq1" sequence } { "seq2" sequence } { "slice1" "a slice" } { "slice2" "a slice" } }
874 { $description "Outputs a pair of virtual sequences with the common prefix of " { $snippet "seq1" } " and " { $snippet "seq2" } " removed." } ;
875
876 HELP: unclip
877 { $values { "seq" sequence } { "rest" sequence } { "first" object } }
878 { $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." }
879 { $examples
880     { $example "USING: prettyprint sequences ;" "{ 1 2 3 } unclip suffix ." "{ 2 3 1 }" }
881 } ;
882
883 HELP: unclip-slice
884 { $values { "seq" sequence } { "rest-slice" slice } { "first" object } }
885 { $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." }
886 { $examples { $example "USING: math.order prettyprint sequences ;" "{ 3 -1 -10 5 7 } unclip-slice [ min ] reduce ." "-10" } } ;
887
888 HELP: unclip-last
889 { $values { "seq" sequence } { "butlast" sequence } { "last" object } }
890 { $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." }
891 { $examples
892     { $example "USING: prettyprint sequences ;" "{ 1 2 3 } unclip-last prefix ." "{ 3 1 2 }" }
893 } ;
894
895 HELP: unclip-last-slice
896 { $values { "seq" sequence } { "butlast-slice" slice } { "last" object } }
897 { $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." } ;
898
899 HELP: sum
900 { $values { "seq" "a sequence of numbers" } { "n" "a number" } }
901 { $description "Outputs the sum of all elements of " { $snippet "seq" } ". Outputs zero given an empty sequence." } ;
902
903 HELP: product
904 { $values { "seq" "a sequence of numbers" } { "n" "a number" } }
905 { $description "Outputs the product of all elements of " { $snippet "seq" } ". Outputs one given an empty sequence." } ;
906
907 HELP: infimum
908 { $values { "seq" "a sequence of real numbers" } { "n" "a number" } }
909 { $description "Outputs the least element of " { $snippet "seq" } "." }
910 { $errors "Throws an error if the sequence is empty." } ;
911
912 HELP: supremum
913 { $values { "seq" "a sequence of real numbers" } { "n" "a number" } }
914 { $description "Outputs the greatest element of " { $snippet "seq" } "." }
915 { $errors "Throws an error if the sequence is empty." } ;
916
917 HELP: produce
918 { $values { "pred" { $quotation "( -- ? )" } } { "quot" { $quotation "( -- obj )" } } { "tail" "a quotation" } { "seq" "a sequence" } }
919 { $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." }
920 { $examples
921     "The following example divides a number by two until we reach zero, and accumulates intermediate results:"
922     { $example "USING: kernel math prettyprint sequences ;" "1337 [ dup 0 > ] [ 2/ dup ] [ ] produce nip ." "{ 668 334 167 83 41 20 10 5 2 1 0 }" }
923     "The " { $snippet "tail" } " quotation is used when the predicate produces more than one output value. In this case, we have to drop this value even if the predicate fails in order for stack inference to calculate a stack effect for the " { $link produce } " call:"
924     { $unchecked-example "USING: kernel prettyprint random sequences ;" "[ 10 random dup 1 > ] [ ] [ drop ] produce ." "{ 8 2 2 9 }" }
925 } ;
926
927 HELP: produce-as
928 { $values { "pred" { $quotation "( -- ? )" } } { "quot" { $quotation "( -- obj )" } } { "tail" "a quotation" } { "exemplar" sequence } { "seq" "a sequence" } }
929 { $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." }
930 { $examples
931     "The following example divides a number by two until we reach zero, and accumulates intermediate results:"
932     { $example "USING: kernel math prettyprint sequences ;" "1337 [ dup 0 > ] [ 2/ dup ] [ ] V{ } produce-as nip ." "V{ 668 334 167 83 41 20 10 5 2 1 0 }" }
933     "The " { $snippet "tail" } " quotation is used when the predicate produces more than one output value. In this case, we have to drop this value even if the predicate fails in order for stack inference to calculate a stack effect for the " { $link produce } " call:"
934     { $unchecked-example "USING: kernel prettyprint random sequences ;" "[ 10 random dup 1 > ] [ ] [ drop ] B{ } produce-as ." "B{ 8 2 2 9 }" }
935 } ;
936
937 HELP: sigma
938 { $values { "seq" sequence } { "quot" quotation } { "n" number } }
939 { $description "Like map sum, but without creating an intermediate sequence." }
940 { $example
941     "! Find the sum of the squares [0,99]"
942     "USING: math math.ranges sequences prettyprint ;"
943     "100 [1,b] [ sq ] sigma ."
944     "338350"
945 } ;
946
947 HELP: count
948 { $values { "seq" sequence } { "quot" quotation } { "n" integer } }
949 { $description "Efficiently returns the number of elements that the predicate quotation matches." }
950 { $example
951     "USING: math math.ranges sequences prettyprint ;"
952     "100 [1,b] [ even? ] count ."
953     "50"
954 } ;
955
956 HELP: pusher
957 { $values
958      { "quot" "a predicate quotation" }
959      { "quot" quotation } { "accum" vector } }
960 { $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." }
961 { $example "! Find all the even numbers:" "USING: prettyprint sequences math kernel ;"
962            "10 [ even? ] pusher [ each ] dip ."
963            "V{ 0 2 4 6 8 }"
964 }
965 { $notes "Used to implement the " { $link filter } " word." } ;
966
967 HELP: trim-head
968 { $values
969      { "seq" sequence } { "quot" quotation }
970      { "newseq" sequence } }
971 { $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." }
972 { $example "" "USING: prettyprint math sequences ;"
973            "{ 0 0 1 2 3 0 0 } [ zero? ] trim-head ."
974            "{ 1 2 3 0 0 }"
975 } ;
976
977 HELP: trim-head-slice
978 { $values
979      { "seq" sequence } { "quot" quotation }
980      { "slice" slice } }
981 { $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" }
982 { $example "" "USING: prettyprint math sequences ;"
983            "{ 0 0 1 2 3 0 0 } [ zero? ] trim-head-slice ."
984            "T{ slice { from 2 } { to 7 } { seq { 0 0 1 2 3 0 0 } } }"
985 } ;
986
987 HELP: trim-tail
988 { $values
989      { "seq" sequence } { "quot" quotation }
990      { "newseq" sequence } }
991 { $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." }
992 { $example "" "USING: prettyprint math sequences ;"
993            "{ 0 0 1 2 3 0 0 } [ zero? ] trim-tail ."
994            "{ 0 0 1 2 3 }"
995 } ;
996
997 HELP: trim-tail-slice
998 { $values
999      { "seq" sequence } { "quot" quotation }
1000      { "slice" slice } }
1001 { $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." }
1002 { $example "" "USING: prettyprint math sequences ;"
1003            "{ 0 0 1 2 3 0 0 } [ zero? ] trim-tail-slice ."
1004            "T{ slice { from 0 } { to 5 } { seq { 0 0 1 2 3 0 0 } } }"
1005 } ;
1006
1007 HELP: trim
1008 { $values
1009      { "seq" sequence } { "quot" quotation }
1010      { "newseq" sequence } }
1011 { $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." }
1012 { $example "" "USING: prettyprint math sequences ;"
1013            "{ 0 0 1 2 3 0 0 } [ zero? ] trim ."
1014            "{ 1 2 3 }"
1015 } ;
1016
1017 HELP: trim-slice
1018 { $values
1019      { "seq" sequence } { "quot" quotation }
1020      { "slice" slice } }
1021 { $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." }
1022 { $example "" "USING: prettyprint math sequences ;"
1023            "{ 0 0 1 2 3 0 0 } [ zero? ] trim-slice ."
1024            "T{ slice { from 2 } { to 5 } { seq { 0 0 1 2 3 0 0 } } }"
1025 } ;
1026
1027 { trim trim-slice trim-head trim-head-slice trim-tail trim-tail-slice } related-words
1028
1029 HELP: sift
1030 { $values
1031      { "seq" sequence }
1032      { "newseq" sequence } }
1033  { $description "Outputs a new sequence with all instance of " { $link f  } " removed." }
1034  { $examples 
1035     { $example "USING: prettyprint sequences ;"
1036         "{ \"a\" 3 { } f } sift ."
1037         "{ \"a\" 3 { } }"
1038     }
1039 } ;
1040
1041 HELP: harvest
1042 { $values
1043      { "seq" sequence }
1044      { "newseq" sequence } }
1045 { $description "Outputs a new sequence with all empty sequences removed." }
1046 { $examples 
1047     { $example "USING: prettyprint sequences ;"
1048                "{ { } { 2 3 } { 5 } { } } harvest ."
1049                "{ { 2 3 } { 5 } }"
1050     }
1051 } ;
1052
1053 { filter filter-here sift harvest } related-words
1054
1055 HELP: set-first
1056 { $values
1057      { "first" object } { "seq" sequence } }
1058 { $description "Sets the first element of a sequence." }
1059 { $examples 
1060     { $example "USING: prettyprint kernel sequences ;"
1061         "{ 1 2 3 4  } 5 over set-first ."
1062         "{ 5 2 3 4 }"
1063     }
1064 } ;
1065
1066 HELP: set-second
1067 { $values
1068      { "second" object } { "seq" sequence } }
1069 { $description "Sets the second element of a sequence." }
1070 { $examples 
1071     { $example "USING: prettyprint kernel sequences ;"
1072         "{ 1 2 3 4  } 5 over set-second ."
1073         "{ 1 5 3 4 }"
1074     }
1075 } ;
1076
1077 HELP: set-third
1078 { $values
1079      { "third" object } { "seq" sequence } }
1080 { $description "Sets the third element of a sequence." }
1081 { $examples 
1082     { $example "USING: prettyprint kernel sequences ;"
1083         "{ 1 2 3 4  } 5 over set-third ."
1084         "{ 1 2 5 4 }"
1085     }
1086 } ;
1087
1088 HELP: set-fourth
1089 { $values
1090      { "fourth" object } { "seq" sequence } }
1091 { $description "Sets the fourth element of a sequence." }
1092 { $examples 
1093     { $example "USING: prettyprint kernel sequences ;"
1094         "{ 1 2 3 4  } 5 over set-fourth ."
1095         "{ 1 2 3 5 }"
1096     }
1097 } ;
1098
1099 { set-first set-second set-third set-fourth } related-words
1100
1101 HELP: replicate
1102 { $values
1103      { "seq" sequence } { "quot" { $quotation "( -- elt )" } }
1104      { "newseq" sequence } }
1105 { $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." }
1106 { $examples 
1107     { $unchecked-example "USING: prettyprint kernel sequences ;"
1108         "5 [ 100 random ] replicate ."
1109         "{ 52 10 45 81 30 }"
1110     }
1111 } ;
1112
1113 HELP: replicate-as
1114 { $values
1115      { "seq" sequence } { "quot" quotation } { "exemplar" sequence }
1116      { "newseq" sequence } }
1117 { $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." }
1118 { $examples 
1119     { $unchecked-example "USING: prettyprint kernel sequences ;"
1120         "5 [ 100 random ] B{ } replicate-as ."
1121         "B{ 44 8 2 33 18 }"
1122     }
1123 } ;
1124 { replicate replicate-as } related-words
1125
1126 HELP: partition
1127 { $values
1128      { "seq" sequence } { "quot" quotation }
1129      { "trueseq" sequence } { "falseseq" sequence } }
1130      { $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" } "." }
1131 { $examples 
1132     { $example "USING: prettyprint kernel math sequences ;"
1133         "{ 1 2 3 4 5 } [ even? ] partition [ . ] bi@"
1134         "{ 2 4 }\n{ 1 3 5 }"
1135     }
1136 } ;
1137
1138 HELP: virtual-seq
1139 { $values
1140      { "seq" sequence }
1141      { "seq'" sequence } }
1142 { $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@ } "." } ;
1143
1144 HELP: virtual@
1145 { $values
1146      { "n" integer } { "seq" sequence }
1147      { "n'" integer } { "seq'" sequence } }
1148 { $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 } "." } ;
1149
1150 HELP: 2map-reduce
1151 { $values
1152      { "seq1" sequence } { "seq2" sequence } { "map-quot" quotation } { "reduce-quot" quotation }
1153      { "result" object } }
1154 { $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." }
1155 { $examples { $example "USING: sequences prettyprint math ;"
1156     "{ 10 30 50 } { 200 400 600 } [ + ] [ + ] 2map-reduce ."
1157     "1290"
1158 } } ;
1159
1160 HELP: 2pusher
1161 { $values
1162      { "quot" quotation }
1163      { "quot" quotation } { "accum1" vector } { "accum2" vector } }
1164 { $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." } ;
1165
1166 HELP: 2reverse-each
1167 { $values
1168      { "seq1" sequence } { "seq2" sequence } { "quot" quotation } }
1169 { $description "Reverse the sequences using the " { $link <reversed> } " word and calls " { $link 2each } " on the reversed sequences." }
1170 { $examples { $example "USING: sequences math prettyprint ;"
1171     "{ 10 20 30 } { 1 2 3 } [ + . ] 2reverse-each"
1172     "33\n22\n11"
1173 } } ;
1174
1175 HELP: 2unclip-slice
1176 { $values
1177      { "seq1" sequence } { "seq2" sequence }
1178      { "rest-slice1" sequence } { "rest-slice2" sequence } { "first1" object } { "first2" object } }
1179 { $description "Unclips the first element of each sequence and leaves two slice elements and the two unclipped objects on the stack." }
1180 { $examples { $example "USING: sequences prettyprint kernel arrays ;"
1181     "{ 1 2 } { 3 4 } 2unclip-slice 4array [ . ] each"
1182     "T{ slice { from 1 } { to 2 } { seq { 1 2 } } }\nT{ slice { from 1 } { to 2 } { seq { 3 4 } } }\n1\n3"
1183 } } ;
1184
1185 HELP: accumulator
1186 { $values
1187      { "quot" quotation }
1188      { "quot'" quotation } { "vec" vector } }
1189 { $description "Creates a new quotation that pushes its result to a vector and outputs that vector on the stack." }
1190 { $examples { $example "USING: sequences prettyprint kernel math ;"
1191     "{ 1 2 } [ 30 + ] accumulator [ each ] dip ."
1192     "V{ 31 32 }"
1193 } } ;
1194
1195 HELP: binary-reduce
1196 { $values
1197      { "seq" sequence } { "start" integer } { "quot" quotation }
1198      { "value" object } }
1199 { $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 } "." }
1200 { $examples "Computing factorial:"
1201     { $example "USING: prettyprint sequences math ;"
1202     "40 rest-slice 1 [ * ] binary-reduce ."
1203     "20397882081197443358640281739902897356800000000" }
1204 } ;
1205
1206 HELP: follow
1207 { $values
1208      { "obj" object } { "quot" quotation }
1209      { "seq" sequence } }
1210 { $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." }
1211 { $examples "Get random numbers until zero is reached:"
1212     { $unchecked-example
1213     "USING: random sequences prettyprint math ;"
1214     "100 [ random dup zero? [ drop f ] when ] follow ."
1215     "{ 100 86 34 32 24 11 7 2 }"
1216 } } ;
1217
1218 HELP: halves
1219 { $values
1220      { "seq" sequence }
1221      { "first-slice" slice } { "second-slice" slice } }
1222 { $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." }
1223 { $examples { $example "USING: arrays sequences prettyprint kernel ;"
1224     "{ 1 2 3 4 5 } halves [ >array . ] bi@"
1225     "{ 1 2 }\n{ 3 4 5 }"
1226 } } ;
1227
1228 HELP: indices
1229 { $values
1230      { "obj" object } { "seq" sequence }
1231      { "indices" sequence } }
1232 { $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." }
1233 { $examples { $example "USING: sequences prettyprint ;"
1234     "2 { 2 4 2 6 2 8 2 10 } indices ."
1235     "V{ 0 2 4 6 }"
1236 } } ;
1237
1238 HELP: insert-nth
1239 { $values
1240      { "elt" object } { "n" integer } { "seq" sequence }
1241      { "seq'" sequence } }
1242 { $description "Creates a new sequence where the " { $snippet "n" } "th index is set to the input object." }
1243 { $examples { $example "USING: prettyprint sequences ;"
1244     "40 3 { 10 20 30 50 } insert-nth ."
1245     "{ 10 20 30 40 50 }"
1246 } } ;
1247
1248 HELP: map-reduce
1249 { $values
1250      { "seq" sequence } { "map-quot" quotation } { "reduce-quot" quotation }
1251      { "result" object } }
1252 { $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." }
1253 { $examples { $example "USING: sequences prettyprint math ;"
1254     "{ 1 3 5 } [ sq ] [ + ] map-reduce ."
1255     "35"
1256 } } ;
1257
1258 HELP: new-like
1259 { $values
1260      { "len" integer } { "exemplar" "an exemplar sequence" } { "quot" quotation }
1261      { "seq" sequence } }
1262 { $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." } ;
1263
1264 HELP: push-either
1265 { $values
1266      { "elt" object } { "quot" quotation } { "accum1" vector } { "accum2" vector } }
1267 { $description "Pushes the input object onto one of the accumualators; the first if the quotation yields true, the second if false." } ;
1268
1269 HELP: sequence-hashcode
1270 { $values
1271      { "n" integer } { "seq" sequence }
1272      { "x" integer } }
1273 { $description "Iterates over a sequence, computes a hashcode with " { $link hashcode* } " for each element, and combines them using " { $link sequence-hashcode-step } "." } ;
1274
1275 HELP: sequence-hashcode-step
1276 { $values
1277      { "oldhash" integer } { "newpart" integer }
1278      { "newhash" integer } }
1279 { $description "An implementation word that computes a running hashcode of a sequence using some bit-twiddling. The resulting hashcode is always a fixnum." } ;
1280
1281 HELP: short
1282 { $values
1283      { "seq" sequence } { "n" integer }
1284      { "seq" sequence } { "n'" integer } }
1285 { $description "Returns the input sequence and its length or " { $snippet "n" } ", whichever is less." }
1286 { $examples { $example "USING: sequences kernel prettyprint ;"
1287     "\"abcd\" 3 short [ . ] bi@"
1288     "\"abcd\"\n3"
1289 } } ;
1290
1291 HELP: shorten
1292 { $values
1293      { "n" integer } { "seq" sequence } }
1294 { $description "Shortens a " { $link "growable" } " sequence to by " { $snippet "n" } " elements long." }
1295 { $examples { $example "USING: sequences prettyprint kernel ;"
1296     "V{ 1 2 3 4 5 } 3 over shorten ."
1297     "V{ 1 2 3 }"
1298 } } ;
1299
1300 HELP: iota
1301 { $values { "n" integer } { "iota" iota } }
1302 { $description "Creates an immutable virtual sequence containing the integers from 0 to " { $snippet "n-1" } "." }
1303 { $examples
1304   { $example
1305     "USING: math sequences prettyprint ;"
1306     "3 iota [ sq ] map ."
1307     "{ 0 1 4 }"
1308   }
1309 } ;
1310
1311 ARTICLE: "sequences-unsafe" "Unsafe sequence operations"
1312 "The " { $link nth-unsafe } " and " { $link set-nth-unsafe } " sequence protocol bypasses bounds checks for increased performance."
1313 $nl
1314 "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."
1315 $nl
1316 "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."
1317 $nl
1318 "The justification for this is that the VM should not crash if a resizable sequence is resized during the execution of an iteration combinator."
1319 $nl
1320 "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 } "." ;
1321
1322 ARTICLE: "sequence-protocol" "Sequence protocol"
1323 "All sequences must be instances of a mixin class:"
1324 { $subsection sequence }
1325 { $subsection sequence? }
1326 "All sequences must know their length:"
1327 { $subsection length }
1328 "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:"
1329 { $subsection nth }
1330 { $subsection nth-unsafe }
1331 "Note that sequences are always indexed starting from zero."
1332 $nl
1333 "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:"
1334 { $subsection set-nth }
1335 { $subsection set-nth-unsafe }
1336 "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:"
1337 { $subsection immutable }
1338 "The following two generic words are optional, as not all sequences are resizable:"
1339 { $subsection set-length }
1340 { $subsection lengthen }
1341 "An optional generic word for creating sequences of the same class as a given sequence:"
1342 { $subsection like }
1343 "Optional generic words for optimization purposes:"
1344 { $subsection new-sequence }
1345 { $subsection new-resizable }
1346 { $see-also "sequences-unsafe" } ;
1347
1348 ARTICLE: "virtual-sequences-protocol" "Virtual sequence protocol"
1349 "Virtual sequences must know their length:"
1350 { $subsection length }
1351 "The underlying sequence to look up a value in:"
1352 { $subsection virtual-seq }
1353 "The index of the value in the underlying sequence:"
1354 { $subsection virtual@ } ;
1355
1356 ARTICLE: "virtual-sequences" "Virtual sequences"
1357 "Virtual sequences allow different ways of accessing a sequence without having to create a new sequence or a new data structure altogether. To do this, they translate the virtual index into a normal index into an underlying sequence using the " { $link "virtual-sequences-protocol" } "."
1358 { $subsection "virtual-sequences-protocol" } ;
1359
1360 ARTICLE: "sequences-integers" "Integer sequences and counted loops"
1361 "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."
1362 $nl
1363 "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:"
1364 { $example "3 [ . ] each" "0\n1\n2" }
1365 "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 } "."
1366 $nl
1367 "Combinators that produce new sequences, such as " { $link map } ", will output an array if the input is an integer." ;
1368
1369 ARTICLE: "sequences-access" "Accessing sequence elements"
1370 { $subsection ?nth }
1371 "Concise way of extracting one of the first four elements:"
1372 { $subsection first }
1373 { $subsection second }
1374 { $subsection third }
1375 { $subsection fourth }
1376 "Unpacking sequences:"
1377 { $subsection first2 }
1378 { $subsection first3 }
1379 { $subsection first4 }
1380 { $see-also nth peek } ;
1381
1382 ARTICLE: "sequences-add-remove" "Adding and removing sequence elements"
1383 "Adding elements:"
1384 { $subsection prefix }
1385 { $subsection suffix }
1386 "Removing elements:"
1387 { $subsection remove }
1388 { $subsection remq }
1389 { $subsection remove-nth } ;
1390
1391 ARTICLE: "sequences-reshape" "Reshaping sequences"
1392 "A " { $emphasis "repetition" } " is a virtual sequence consisting of a single element repeated multiple times:"
1393 { $subsection repetition }
1394 { $subsection <repetition> }
1395 "Reversing a sequence:"
1396 { $subsection reverse }
1397 "A " { $emphasis "reversal" } " presents a reversed view of an underlying sequence:"
1398 { $subsection reversed }
1399 { $subsection <reversed> }
1400 "Transposing a matrix:"
1401 { $subsection flip } ;
1402
1403 ARTICLE: "sequences-appending" "Appending sequences"
1404 { $subsection append }
1405 { $subsection append-as }
1406 { $subsection prepend }
1407 { $subsection 3append }
1408 { $subsection 3append-as }
1409 { $subsection surround }
1410 { $subsection glue }
1411 { $subsection concat }
1412 { $subsection join }
1413 "A pair of words useful for aligning strings:"
1414 { $subsection pad-head }
1415 { $subsection pad-tail } ;
1416
1417 ARTICLE: "sequences-slices" "Subsequences and slices"
1418 "Extracting a subsequence:"
1419 { $subsection subseq }
1420 { $subsection head }
1421 { $subsection tail }
1422 { $subsection head* }
1423 { $subsection tail* }
1424 "Removing the first or last element:"
1425 { $subsection rest }
1426 { $subsection but-last }
1427 "Taking a sequence apart into a head and a tail:"
1428 { $subsection unclip }
1429 { $subsection unclip-last }
1430 { $subsection cut }
1431 { $subsection cut* }
1432 "A " { $emphasis "slice" } " is a virtual sequence which presents as view of a subsequence of an underlying sequence:"
1433 { $subsection slice }
1434 { $subsection slice? }
1435 "Extracting a slice:"
1436 { $subsection <slice> }
1437 { $subsection head-slice }
1438 { $subsection tail-slice }
1439 { $subsection head-slice* }
1440 { $subsection tail-slice* }
1441 "Removing the first or last element:"
1442 { $subsection rest-slice }
1443 { $subsection but-last-slice }
1444 "Taking a sequence apart into a head and a tail:"
1445 { $subsection unclip-slice }
1446 { $subsection unclip-last-slice }
1447 { $subsection cut-slice }
1448 "A utility for words which use slices as iterators:"
1449 { $subsection <flat-slice> } ;
1450
1451 ARTICLE: "sequences-combinators" "Sequence combinators"
1452 "Iteration:"
1453 { $subsection each }
1454 { $subsection each-index }
1455 { $subsection reduce }
1456 { $subsection interleave }
1457 { $subsection replicate }
1458 { $subsection replicate-as }
1459 "Mapping:"
1460 { $subsection map }
1461 { $subsection map-as }
1462 { $subsection map-index }
1463 { $subsection accumulate }
1464 { $subsection produce }
1465 { $subsection produce-as }
1466 "Filtering:"
1467 { $subsection push-if }
1468 { $subsection filter }
1469 "Testing if a sequence contains elements satisfying a predicate:"
1470 { $subsection any? }
1471 { $subsection all? }
1472 { $subsection "sequence-2combinators" }
1473 { $subsection "sequence-3combinators" } ;
1474
1475 ARTICLE: "sequence-2combinators" "Pair-wise sequence combinators"
1476 "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."
1477 { $subsection 2each }
1478 { $subsection 2reduce }
1479 { $subsection 2map }
1480 { $subsection 2map-as }
1481 { $subsection 2all? } ;
1482
1483 ARTICLE: "sequence-3combinators" "Triple-wise sequence combinators"
1484 "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."
1485 { $subsection 3each }
1486 { $subsection 3map }
1487 { $subsection 3map-as } ;
1488
1489 ARTICLE: "sequences-tests" "Testing sequences"
1490 "Testing for an empty sequence:"
1491 { $subsection empty? }
1492 "Testing indices:"
1493 { $subsection bounds-check? }
1494 "Testing if a sequence contains an object:"
1495 { $subsection member? }
1496 { $subsection memq? }
1497 "Testing if a sequence contains a subsequence:"
1498 { $subsection head? }
1499 { $subsection tail? }
1500 { $subsection subseq? } ;
1501
1502 ARTICLE: "sequences-search" "Searching sequences"
1503 "Finding the index of an element:"
1504 { $subsection index }
1505 { $subsection index-from }
1506 { $subsection last-index }
1507 { $subsection last-index-from }
1508 "Finding the start of a subsequence:"
1509 { $subsection start }
1510 { $subsection start* }
1511 "Finding the index of an element satisfying a predicate:"
1512 { $subsection find }
1513 { $subsection find-from }
1514 { $subsection find-last }
1515 { $subsection find-last-from } ;
1516
1517 ARTICLE: "sequences-trimming" "Trimming sequences"
1518 "Trimming words:"
1519 { $subsection trim }
1520 { $subsection trim-head }
1521 { $subsection trim-tail }
1522 "Potentially more efficient trim:"
1523 { $subsection trim-slice }
1524 { $subsection trim-head-slice }
1525 { $subsection trim-tail-slice } ;
1526
1527 ARTICLE: "sequences-destructive-discussion" "When to use destructive operations"
1528 "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:"
1529 { $list
1530     "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."
1531     { "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." }
1532 }
1533 "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." ;
1534
1535 ARTICLE: "sequences-destructive" "Destructive operations"
1536 "These words modify their input, instead of creating a new sequence."
1537 { $subsection "sequences-destructive-discussion" }
1538 "Changing elements:"
1539 { $subsection change-each }
1540 { $subsection change-nth }
1541 "Deleting elements:"
1542 { $subsection delete }
1543 { $subsection delq }
1544 { $subsection delete-nth }
1545 { $subsection delete-slice }
1546 { $subsection delete-all }
1547 { $subsection filter-here }
1548 "Other destructive words:"
1549 { $subsection reverse-here }
1550 { $subsection push-all }
1551 { $subsection move }
1552 { $subsection exchange }
1553 { $subsection copy }
1554 { $subsection replace-slice }
1555 "Many operations have constructive and destructive variants:"
1556 { $table
1557     { "Constructive" "Destructive" }
1558     { { $link suffix } { $link push } }
1559     { { $link but-last } { $link pop* } }
1560     { { $link unclip-last } { $link pop } }
1561     { { $link remove } { $link delete } }
1562     { { $link remq } { $link delq } }
1563     { { $link remove-nth } { $link delete-nth } }
1564     { { $link reverse } { $link reverse-here } }
1565     { { $link append } { $link push-all } }
1566     { { $link map } { $link change-each } }
1567     { { $link filter } { $link filter-here } }
1568 }
1569 { $see-also set-nth push pop "sequences-stacks" } ;
1570
1571 ARTICLE: "sequences-stacks" "Treating sequences as stacks"
1572 "The classical stack operations, modifying a sequence in place:"
1573 { $subsection peek }
1574 { $subsection push }
1575 { $subsection pop }
1576 { $subsection pop* }
1577 { $see-also empty? } ;
1578
1579 ARTICLE: "sequences-comparing" "Comparing sequences"
1580 "Element equality testing:"
1581 { $subsection sequence= }
1582 { $subsection mismatch }
1583 { $subsection drop-prefix }
1584 "The " { $link <=> } " generic word performs lexicographic comparison when applied to sequences." ;
1585
1586 ARTICLE: "sequences-f" "The f object as a sequence"
1587 "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." ;
1588
1589 ARTICLE: "sequences" "Sequence operations"
1590 "A " { $emphasis "sequence" } " is a finite, linearly-ordered collection of elements. Words for working with sequences are in the " { $vocab-link "sequences" } " vocabulary."
1591 $nl
1592 "Sequences implement a protocol:"
1593 { $subsection "sequence-protocol" }
1594 { $subsection "sequences-f" }
1595 { $subsection "sequences-integers" }
1596 "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" } "."
1597 { $subsection "sequences-access" }
1598 { $subsection "sequences-combinators" }
1599 { $subsection "sequences-add-remove" }
1600 { $subsection "sequences-appending" }
1601 { $subsection "sequences-slices" }
1602 { $subsection "sequences-reshape" }
1603 { $subsection "sequences-tests" }
1604 { $subsection "sequences-search" }
1605 { $subsection "sequences-comparing" }
1606 { $subsection "sequences-split" }
1607 { $subsection "grouping" }
1608 { $subsection "sequences-destructive" }
1609 { $subsection "sequences-stacks" }
1610 { $subsection "sequences-sorting" }
1611 { $subsection "binary-search" }
1612 { $subsection "sets" }
1613 { $subsection "sequences-trimming" }
1614 "For inner loops:"
1615 { $subsection "sequences-unsafe" } ;
1616
1617 ABOUT: "sequences"