]> gitweb.factorcode.org Git - factor.git/blob - core/sequences/sequences-docs.factor
Resolved merge.
[factor.git] / core / sequences / sequences-docs.factor
1 USING: arrays help.markup help.syntax math
2 sequences.private vectors strings kernel math.order layouts
3 quotations generic.single ;
4 IN: sequences
5
6 HELP: sequence
7 { $class-description "A mixin class whose instances are sequences. Custom implementations of the sequence protocol should be declared as instances of this mixin for all sequence functionality to work correctly:"
8     { $code "INSTANCE: my-sequence sequence" }
9 } ;
10
11 HELP: length
12 { $values { "seq" sequence } { "n" "a non-negative integer" } }
13 { $contract "Outputs the length of the sequence. All sequences support this operation." } ;
14
15 HELP: set-length
16 { $values { "n" "a non-negative integer" } { "seq" "a resizable sequence" } }
17 { $contract "Resizes a sequence. The initial contents of the new area is undefined." }
18 { $errors "Throws a " { $link no-method  } " error if the sequence is not resizable, and a " { $link bounds-error } " if the new length is negative." }
19 { $side-effects "seq" } ;
20
21 HELP: lengthen
22 { $values { "n" "a non-negative integer" } { "seq" "a resizable sequence" } }
23 { $contract "Ensures the sequence has a length of at least " { $snippet "n" } " elements. This word differs from " { $link set-length } " in two respects:"
24     { $list
25         { "This word does not shrink the sequence if " { $snippet "n" } " is less than its length." }
26         { "The word doubles the underlying storage of " { $snippet "seq" } ", whereas " { $link set-length } " is permitted to set it to equal " { $snippet "n" } ". This ensures that repeated calls to this word with constant increments of " { $snippet "n" } " do not result in a quadratic amount of copying, so that for example " { $link push-all } " can run efficiently when used in a loop." }
27     }
28 } ;
29
30 HELP: nth
31 { $values { "n" "a non-negative integer" } { "seq" sequence } { "elt" "the element at the " { $snippet "n" } "th index" } }
32 { $contract "Outputs the " { $snippet "n" } "th element of the sequence. Elements are numbered from zero, so the last element has an index one less than the length of the sequence. All sequences support this operation." }
33 { $errors "Throws a " { $link bounds-error } " if the index is negative, or greater than or equal to the length of the sequence." } ;
34
35 HELP: set-nth
36 { $values { "elt" object } { "n" "a non-negative integer" } { "seq" "a mutable sequence" } }
37 { $contract "Sets the " { $snippet "n" } "th element of the sequence. Storing beyond the end of a resizable sequence such as a vector or string buffer grows the sequence." }
38 { $errors "Throws an error if the index is negative, or if the sequence is not resizable and the index is greater than or equal to the length of the sequence."
39 $nl
40 "Throws an error if the sequence cannot hold elements of the given type." }
41 { $side-effects "seq" } ;
42
43 HELP: nths
44 { $values
45      { "indices" sequence } { "seq" sequence }
46      { "seq'" sequence } }
47 { $description "Ouptuts a sequence of elements from the input sequence indexed by the indices." }
48 { $examples 
49     { $example "USING: prettyprint sequences ;"
50                "{ 0 2 } { \"a\" \"b\" \"c\" } nths ."
51                "{ \"a\" \"c\" }"
52     }
53 } ;
54
55 HELP: immutable
56 { $values { "seq" sequence } }
57 { $description "Throws an " { $link immutable } " error." }
58 { $error-description "Thrown if an attempt is made to modify an immutable sequence." } ;
59
60 HELP: new-sequence
61 { $values { "len" "a non-negative integer" } { "seq" sequence } { "newseq" "a mutable sequence" } }
62 { $contract "Outputs a mutable sequence of length " { $snippet "n" } " which can hold the elements of " { $snippet "seq" } ". The initial contents of the sequence are undefined." } ;
63
64 HELP: new-resizable
65 { $values { "len" "a non-negative integer" } { "seq" sequence } { "newseq" "a resizable mutable sequence" } }
66 { $contract "Outputs a resizable mutable sequence with an initial capacity of " { $snippet "n" } " elements and zero length, which can hold the elements of " { $snippet "seq" } "." }
67 { $examples
68     { $example "USING: prettyprint sequences ;" "300 V{ } new-resizable ." "V{ }" }
69     { $example "USING: prettyprint sequences ;" "300 SBUF\" \" new-resizable ." "SBUF\" \"" }
70 } ;
71
72 HELP: like
73 { $values { "seq" sequence } { "exemplar" sequence } { "newseq" "a new sequence" } }
74 { $contract "Outputs a sequence with the same elements as " { $snippet "seq" } ", but " { $emphasis "like" } " the template sequence, in the sense that it either has the same class as the template sequence, or if the template sequence is a virtual sequence, the same class as the template sequence's underlying sequence."
75 $nl
76 "The default implementation does nothing." }
77 { $notes "Unlike " { $link clone-like } ", the output sequence might share storage with the input sequence." } ;
78
79 HELP: empty?
80 { $values { "seq" sequence } { "?" "a boolean" } }
81 { $description "Tests if the sequence has zero length." } ;
82
83 HELP: if-empty
84 { $values { "seq" sequence } { "quot1" quotation } { "quot2" quotation } }
85 { $description "Makes an implicit check if the sequence is empty. An empty sequence is dropped and " { $snippet "quot1" } " is called. Otherwise, if the sequence has any elements, " { $snippet "quot2" } " is called on it." }
86 { $example
87     "USING: kernel prettyprint sequences ;"
88     "{ 1 2 3 } [ \"empty sequence\" ] [ sum ] if-empty ."
89     "6"
90 } ;
91
92 HELP: when-empty
93 { $values
94      { "seq" sequence } { "quot" "the first quotation of an " { $link if-empty } } }
95 { $description "Makes an implicit check if the sequence is empty. An empty sequence is dropped and the " { $snippet "quot" } " is called." }
96 { $examples "This word is equivalent to " { $link if-empty } " with an empty second quotation:"
97     { $example
98     "USING: sequences prettyprint ;"
99     "{ } [ { 4 5 6 } ] [ ] if-empty ."
100     "{ 4 5 6 }"
101     }
102     { $example
103     "USING: sequences prettyprint ;"
104     "{ } [ { 4 5 6 } ] when-empty ."
105     "{ 4 5 6 }"
106     }
107 } ;
108
109 HELP: unless-empty
110 { $values
111      { "seq" sequence } { "quot" "the second quotation of an " { $link if-empty } } }
112 { $description "Makes an implicit check if the sequence is empty. An empty sequence is dropped. Otherwise, the " { $snippet "quot" } " is called on the sequence." }
113 { $examples "This word is equivalent to " { $link if-empty } " with an empty first quotation:"
114     { $example
115     "USING: sequences prettyprint ;"
116     "{ 4 5 6 } [ ] [ sum ] if-empty ."
117     "15"
118     }
119     { $example
120     "USING: sequences prettyprint ;"
121     "{ 4 5 6 } [ sum ] unless-empty ."
122     "15"
123     }
124 } ;
125
126 { 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. User code must use " { $link nth } " instead." } ;
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. User code must use " { $link set-nth } " instead." } ;
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 } { "newseq" sequence } }
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: map-find
401 { $values { "seq" sequence } { "quot" { $quotation "( elt -- result/f )" } } { "result" "the first non-false result of the quotation" } { "elt" "the first matching element, or " { $link f } } }
402 { $description "Applies the quotation to each element of the sequence, until the quotation outputs a true value. If the quotation ever yields a result which is not " { $link f } ", then the value is output, along with the element of the sequence which yielded this." } ;
403
404 HELP: any?
405 { $values { "seq" sequence } { "quot" { $quotation "( elt -- ? )" } } { "?" "a boolean" } }
406 { $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 } "." } ;
407
408 HELP: all?
409 { $values { "seq" sequence } { "quot" { $quotation "( elt -- ? )" } } { "?" "a boolean" } }
410 { $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 } "." } ;
411
412 HELP: push-if
413 { $values { "elt" object } { "quot" { $quotation "( elt -- ? )" } } { "accum" "a resizable mutable sequence" } }
414 { $description "Adds the element at the end of the sequence if the quotation yields a true value." } 
415 { $notes "This word is a factor of " { $link filter } "." } ;
416
417 HELP: filter
418 { $values { "seq" sequence } { "quot" { $quotation "( elt -- ? )" } } { "subseq" "a new sequence" } }
419 { $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." } ;
420
421 HELP: filter-here
422 { $values { "seq" "a resizable mutable sequence" } { "quot" { $quotation "( elt -- ? )" } } }
423 { $description "Applies the quotation to each element in turn, and removes elements for which the quotation outputs a false value." }
424 { $side-effects "seq" } ;
425
426 HELP: interleave
427 { $values { "seq" sequence } { "between" "a quotation" } { "quot" { $quotation "( elt -- )" } } }
428 { $description "Applies " { $snippet "quot" } " to each element in turn, also invoking " { $snippet "between" } " in-between each pair of elements." }
429 { $example "USING: io sequences ;" "{ \"a\" \"b\" \"c\" } [ \"X\" write ] [ write ] interleave" "aXbXc" } ;
430
431 HELP: index
432 { $values { "obj" object } { "seq" sequence } { "n" "an index" } }
433 { $description "Outputs the index of the first element in the sequence equal to " { $snippet "obj" } ". If no element is found, outputs " { $link f } "." } ;
434
435 HELP: index-from
436 { $values { "obj" object } { "i" "a start index" } { "seq" sequence } { "n" "an index" } }
437 { $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 } "." } ;
438
439 HELP: last-index
440 { $values { "obj" object } { "seq" sequence } { "n" "an index" } }
441 { $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 } "." } ;
442
443 HELP: last-index-from
444 { $values { "obj" object } { "i" "a start index" } { "seq" sequence } { "n" "an index" } }
445 { $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 } "." } ;
446
447 HELP: member?
448 { $values { "elt" object } { "seq" sequence } { "?" "a boolean" } }
449 { $description "Tests if the sequence contains an element equal to the object." }
450 { $notes "This word uses equality comparison (" { $link = } ")." } ;
451
452 HELP: memq?
453 { $values { "elt" object } { "seq" sequence } { "?" "a boolean" } }
454 { $description "Tests if the sequence contains the object." }
455 { $notes "This word uses identity comparison (" { $link eq? } ")." } ;
456
457 HELP: remove
458 { $values { "elt" object } { "seq" sequence } { "newseq" "a new sequence" } }
459 { $description "Outputs a new sequence containing all elements of the input sequence except for given element." }
460 { $notes "This word uses equality comparison (" { $link = } ")." } ;
461
462 HELP: remq
463 { $values { "elt" object } { "seq" sequence } { "newseq" "a new sequence" } }
464 { $description "Outputs a new sequence containing all elements of the input sequence except those equal to the given element." }
465 { $notes "This word uses identity comparison (" { $link eq? } ")." } ;
466
467 HELP: remove-nth
468 { $values
469      { "n" integer } { "seq" sequence }
470      { "seq'" sequence } }
471 { $description "Creates a new sequence without the element at index " { $snippet "n" } "." }
472 { $examples "Notice that the original sequence is left intact:" { $example "USING: sequences prettyprint kernel ;"
473     "{ 1 2 3 } 1 over remove-nth . ."
474     "{ 1 3 }\n{ 1 2 3 }"
475 } } ;
476
477 HELP: move
478 { $values { "from" "an index in " { $snippet "seq" } } { "to" "an index in " { $snippet "seq" } } { "seq" "a mutable sequence" } }
479 { $description "Sets the element with index " { $snippet "m" } " to the element with index " { $snippet "n" } "." }
480 { $side-effects "seq" } ;
481
482 HELP: delete
483 { $values { "elt" object } { "seq" "a resizable mutable sequence" } }
484 { $description "Removes all elements equal to " { $snippet "elt" } " from " { $snippet "seq" } "." }
485 { $notes "This word uses equality comparison (" { $link = } ")." }
486 { $side-effects "seq" } ;
487
488 HELP: delq
489 { $values { "elt" object } { "seq" "a resizable mutable sequence" } }
490 { $description "Outputs a new sequence containing all elements of the input sequence except the given element." }
491 { $notes "This word uses identity comparison (" { $link eq? } ")." }
492 { $side-effects "seq" } ;
493
494 HELP: delete-nth
495 { $values { "n" "a non-negative integer" } { "seq" "a resizable mutable sequence" } }
496 { $description "Removes the " { $snippet "n" } "th element from the sequence, shifting all other elements down and reducing its length by one." }
497 { $side-effects "seq" } ;
498
499 HELP: delete-slice
500 { $values { "from" "a non-negative integer" } { "to" "a non-negative integer" } { "seq" "a resizable mutable sequence" } }
501 { $description "Removes a range of elements beginning at index " { $snippet "from" } " and ending before index " { $snippet "to" } "." }
502 { $side-effects "seq" } ;
503
504 HELP: replace-slice
505 { $values { "new" sequence } { "seq" sequence } { "from" "a non-negative integer" } { "to" "a non-negative integer" } { "seq'" sequence } }
506 { $description "Replaces a range of elements beginning at index " { $snippet "from" } " and ending before index " { $snippet "to" } " with a new sequence." }
507 { $errors "Throws an error if " { $snippet "new" } " contains elements whose types are not permissible in " { $snippet "seq" } "." } ;
508
509 { push prefix suffix } related-words
510
511 HELP: suffix
512 { $values { "seq" sequence } { "elt" object } { "newseq" sequence } }
513 { $description "Outputs a new sequence obtained by adding " { $snippet "elt" } " at the end of " { $snippet "seq" } "." }
514 { $errors "Throws an error if the type of " { $snippet "elt" } " is not permitted in sequences of the same class as " { $snippet "seq1" } "." }
515 { $examples
516     { $example "USING: prettyprint sequences ;" "{ 1 2 3 } 4 suffix ." "{ 1 2 3 4 }" }
517 } ;
518
519 HELP: prefix
520 { $values { "seq" sequence } { "elt" object } { "newseq" sequence } }
521 { $description "Outputs a new sequence obtained by adding " { $snippet "elt" } " at the beginning of " { $snippet "seq" } "." }
522 { $errors "Throws an error if the type of " { $snippet "elt" } " is not permitted in sequences of the same class as " { $snippet "seq1" } "." } 
523 { $examples
524 { $example "USING: prettyprint sequences ;" "{ 1 2 3 } 0 prefix ." "{ 0 1 2 3 }" }
525 } ;
526
527 HELP: sum-lengths
528 { $values { "seq" "a sequence of sequences" } { "n" integer } }
529 { $description "Outputs the sum of the lengths of all sequences in " { $snippet "seq" } "." } ;
530
531 HELP: concat
532 { $values { "seq" sequence } { "newseq" sequence } }
533 { $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" } "." }
534 { $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" } "." } ;
535
536 HELP: concat-as
537 { $values { "seq" sequence } { "exemplar" sequence } { "newseq" sequence } }
538 { $description "Concatenates a sequence of sequences together into one sequence with the same type as " { $snippet "exemplar" } "." }
539 { $errors "Throws an error if one of the sequences in " { $snippet "seq" } " contains elements not permitted in sequences of the same class as " { $snippet "exemplar" } "." } ;
540
541 HELP: join
542 { $values { "seq" sequence } { "glue" sequence } { "newseq" sequence } }
543 { $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" } "." }
544 { $notes "If the " { $snippet "glue" } " sequence is empty, this word calls " { $link concat-as } "." }
545 { $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" } "." } ;
546
547 { join concat concat-as } related-words
548
549 HELP: last
550 { $values { "seq" sequence } { "elt" object } }
551 { $description "Outputs the last element of a sequence." }
552 { $errors "Throws an error if the sequence is empty." } ;
553
554 { pop pop* } related-words
555
556 HELP: pop*
557 { $values { "seq" "a resizable mutable sequence" } }
558 { $description "Removes the last element and shortens the sequence." }
559 { $side-effects "seq" }
560 { $errors "Throws an error if the sequence is empty." } ;
561
562 HELP: pop
563 { $values { "seq" "a resizable mutable sequence" } { "elt" object } }
564 { $description "Outputs the last element after removing it and shortening the sequence." }
565 { $side-effects "seq" }
566 { $errors "Throws an error if the sequence is empty." } ;
567
568 HELP: mismatch
569 { $values { "seq1" sequence } { "seq2" sequence } { "i" "an index" } }
570 { $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." } ;
571
572 HELP: flip
573 { $values { "matrix" "a sequence of equal-length sequences" } { "newmatrix" "a sequence of equal-length sequences" } }
574 { $description "Transposes the matrix; that is, rows become columns and columns become rows." }
575 { $examples { $example "USING: prettyprint sequences ;" "{ { 1 2 3 } { 4 5 6 } } flip ." "{ { 1 4 } { 2 5 } { 3 6 } }" } } ;
576
577 HELP: exchange
578 { $values { "m" "a non-negative integer" } { "n" "a non-negative integer" } { "seq" "a mutable sequence" } }
579 { $description "Exchanges the " { $snippet "m" } "th and " { $snippet "n" } "th elements of " { $snippet "seq" } "." } ;
580
581 HELP: reverse-here
582 { $values { "seq" "a mutable sequence" } }
583 { $description "Reverses a sequence in-place." }
584 { $side-effects "seq" } ;
585
586 HELP: padding
587 { $values { "seq" sequence } { "n" "a non-negative integer" } { "elt" object } { "quot" { $quotation "( seq1 seq2 -- newseq )" } } { "newseq" "a new sequence" } }
588 { $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." } ;
589
590 HELP: pad-head
591 { $values { "seq" sequence } { "n" "a non-negative integer" } { "elt" object } { "padded" "a new sequence" } }
592 { $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" } "." }
593 { $examples { $example "USING: io sequences ;" "{ \"ab\" \"quux\" } [ 5 CHAR: - pad-head print ] each" "---ab\n-quux" } } ;
594
595 HELP: pad-tail
596 { $values { "seq" sequence } { "n" "a non-negative integer" } { "elt" object } { "padded" "a new sequence" } }
597 { $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" } "." }
598 { $examples { $example "USING: io sequences ;" "{ \"ab\" \"quux\" } [ 5 CHAR: - pad-tail print ] each" "ab---\nquux-" } } ;
599
600 HELP: sequence=
601 { $values { "seq1" sequence } { "seq2" sequence } { "?" "a boolean" } }
602 { $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." } ;
603
604 HELP: reversed
605 { $class-description "A virtual sequence which presents a reversed view of an underlying sequence. New instances can be created by calling " { $link <reversed> } "." } ;
606
607 HELP: reverse
608 { $values { "seq" sequence } { "newseq" "a new sequence" } }
609 { $description "Outputs a new sequence having the same elements as " { $snippet "seq" } " but in reverse order." } ;
610
611 { reverse <reversed> reverse-here } related-words
612
613 HELP: <reversed>
614 { $values { "seq" sequence } { "reversed" "a new sequence" } }
615 { $description "Creates an instance of the " { $link reversed } " class." }
616 { $see-also "virtual-sequences" } ;
617
618 HELP: slice-error
619 { $values { "str" "a reason" } }
620 { $description "Throws a " { $link slice-error } "." }
621 { $error-description "Thrown by " { $link <slice> } " if one of the following invalid conditions holds:"
622     { $list
623         "The start index is negative"
624         "The end index is greater than the length of the sequence"
625         "The start index is greater than the end index"
626     }
627 } ;
628
629 HELP: slice
630 { $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> } "."
631 $nl
632 "Slices are mutable if the underlying sequence is mutable, and mutating a slice changes the underlying sequence. However, slices cannot be resized after creation." } ;
633
634 HELP: check-slice
635 { $values { "from" "a non-negative integer" } { "to" "a non-negative integer" } { "seq" sequence } }
636 { $description "Ensures that " { $snippet "m" } " is less than or equal to " { $snippet "m" } ", and that both indices are within bounds for " { $snippet "seq" } "." }
637 { $errors "Throws a " { $link slice-error } " if the preconditions are not met." } ;
638
639 HELP: collapse-slice
640 { $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 } }
641 { $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." }
642 ;
643
644 HELP: <flat-slice>
645 { $values { "seq" sequence } { "slice" slice } }
646 { $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" } "." }
647 { $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." } ;
648
649 HELP: <slice>
650 { $values { "from" "a non-negative integer" } { "to" "a non-negative integer" } { "seq" sequence } { "slice" slice } }
651 { $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" } "." }
652 { $errors "Throws an error if " { $snippet "m" } " or " { $snippet "n" } " is out of bounds." }
653 { $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." } ;
654
655 { <slice> subseq } related-words
656
657 HELP: repetition
658 { $class-description "A virtual sequence consisting of " { $snippet "elt" } " repeated " { $snippet "len" } " times. Repetitions are created by calling " { $link <repetition> } "." } ;
659
660 HELP: <repetition>
661 { $values { "len" "a non-negative integer" } { "elt" object } { "repetition" repetition } }
662 { $description "Creates a new " { $link repetition } "." }
663 { $examples
664     { $example "USING: arrays prettyprint sequences ;" "10 \"X\" <repetition> >array ." "{ \"X\" \"X\" \"X\" \"X\" \"X\" \"X\" \"X\" \"X\" \"X\" \"X\" }" }
665     { $example "USING: prettyprint sequences ;" "10 \"X\" <repetition> concat ." "\"XXXXXXXXXX\"" }
666 } ;
667 HELP: copy
668 { $values { "src" sequence } { "i" "an index in " { $snippet "dest" } } { "dst" "a mutable sequence" } }
669 { $description "Copies all elements of " { $snippet "src" } " to " { $snippet "dest" } ", with destination indices starting from " { $snippet "i" } ". Grows " { $snippet "to" } " first if necessary." }
670 { $side-effects "dest" }
671 { $errors "An error is thrown if " { $snippet "to" } " is not resizable, and not large enough to hold the copied elements." } ;
672
673 HELP: push-all
674 { $values { "src" sequence } { "dest" "a resizable mutable sequence" } }
675 { $description "Appends " { $snippet "src" } " to the end of " { $snippet "dest" } "." }
676 { $side-effects "dest" }
677 { $errors "Throws an error if " { $snippet "src" } " contains elements not permitted in " { $snippet "dest" } "." } ;
678
679 HELP: append
680 { $values { "seq1" sequence } { "seq2" sequence } { "newseq" sequence } }
681 { $description "Outputs a new sequence of the same type as " { $snippet "seq1" } " consisting of the elements of " { $snippet "seq1" } " followed by " { $snippet "seq2" } "." }
682 { $errors "Throws an error if " { $snippet "seq2" } " contains elements not permitted in sequences of the same class as " { $snippet "seq1" } "." }
683 { $examples 
684     { $example "USING: prettyprint sequences ;"
685         "{ 1 2 } B{ 3 4 } append ."
686         "{ 1 2 3 4 }"
687     }
688     { $example "USING: prettyprint sequences strings ;"
689         "\"go\" \"ing\" append ."
690         "\"going\""
691     }
692 } ;
693
694 HELP: append-as
695 { $values { "seq1" sequence } { "seq2" sequence } { "exemplar" sequence } { "newseq" sequence } }
696 { $description "Outputs a new sequence of the same type as " { $snippet "exemplar" } " consisting of the elements of " { $snippet "seq1" } " followed by " { $snippet "seq2" } "." }
697 { $errors "Throws an error if " { $snippet "seq1" } " or " { $snippet "seq2" } " contain elements not permitted in sequences of the same class as " { $snippet "exemplar" } "." }
698 { $examples 
699     { $example "USING: prettyprint sequences ;"
700         "{ 1 2 } B{ 3 4 } B{ } append-as ."
701         "B{ 1 2 3 4 }"
702     }
703     { $example "USING: prettyprint sequences strings ;"
704         "\"go\" \"ing\" SBUF\" \" append-as ."
705         "SBUF\" going\""
706     }
707 } ;
708
709 { append append-as } related-words
710
711 HELP: prepend
712 { $values { "seq1" sequence } { "seq2" sequence } { "newseq" sequence } }
713 { $description "Outputs a new sequence of the same type as " { $snippet "seq2" } " consisting of the elements of " { $snippet "seq2" } " followed by " { $snippet "seq1" } "." }
714 { $errors "Throws an error if " { $snippet "seq1" } " contains elements not permitted in sequences of the same class as " { $snippet "seq2" } "." }
715 { $examples { $example "USING: prettyprint sequences ;"
716         "{ 1 2 } B{ 3 4 } prepend ."
717         "B{ 3 4 1 2 }"
718     }
719     { $example "USING: prettyprint sequences strings ;"
720         "\"go\" \"car\" prepend ."
721         "\"cargo\""
722     }
723 } ;
724
725 HELP: 3append
726 { $values { "seq1" sequence } { "seq2" sequence } { "seq3" sequence } { "newseq" sequence } }
727 { $description "Outputs a new sequence consisting of the elements of " { $snippet "seq1" } ", " { $snippet "seq2" } " and " { $snippet "seq3" } " in turn." }
728 { $errors "Throws an error if " { $snippet "seq2" } " or " { $snippet "seq3" } " contain elements not permitted in sequences of the same class as " { $snippet "seq1" } "." }
729 { $examples
730     { $example "USING: prettyprint sequences ;"
731         "\"a\" \"b\" \"c\" 3append ."
732         "\"abc\""
733     }
734 } ;
735
736 HELP: 3append-as
737 { $values { "seq1" sequence } { "seq2" sequence } { "seq3" sequence } { "exemplar" sequence } { "newseq" sequence } }
738 { $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" } "." }
739 { $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" } "." }
740 { $examples
741     { $example "USING: prettyprint sequences ;"
742         "\"a\" \"b\" \"c\" SBUF\" \" 3append-as ."
743         "SBUF\" abc\""
744     }
745 } ;
746
747 { 3append 3append-as } related-words
748
749 HELP: surround
750 { $values { "seq1" sequence } { "seq2" sequence } { "seq3" sequence } { "newseq" sequence } }
751 { $description "Outputs a new sequence with " { $snippet "seq1" } " inserted between " { $snippet "seq2" } " and " { $snippet "seq3" } "." }
752 { $examples
753     { $example "USING: sequences prettyprint ;"
754                "\"sssssh\" \"(\" \")\" surround ."
755                "\"(sssssh)\""
756     }
757 } ;
758
759 HELP: glue
760 { $values { "seq1" sequence } { "seq2" sequence } { "seq3" sequence } { "newseq" sequence } }
761 { $description "Outputs a new sequence with " { $snippet "seq3" } " inserted between " { $snippet "seq1" } " and " { $snippet "seq2" } "." }
762 { $examples
763     { $example "USING: sequences prettyprint ;"
764                "\"a\" \"b\" \",\" glue ."
765                "\"a,b\""
766     }
767 } ;
768
769 HELP: subseq
770 { $values { "from" "a non-negative integer" } { "to" "a non-negative integer" } { "seq" sequence } { "subseq" "a new sequence" } }
771 { $description "Outputs a new sequence consisting of all elements starting from and including " { $snippet "from" } ", and up to but not including " { $snippet "to" } "." }
772 { $errors "Throws an error if " { $snippet "from" } " or " { $snippet "to" } " is out of bounds." } ;
773
774 HELP: clone-like
775 { $values { "seq" sequence } { "exemplar" sequence } { "newseq" "a new sequence" } }
776 { $description "Outputs a newly-allocated sequence with the same elements as " { $snippet "seq" } " but of the same type as " { $snippet "exemplar" } "." }
777 { $notes "Unlike " { $link like } ", this word always creates a new sequence which never shares storage with the original." } ;
778
779 HELP: head-slice
780 { $values { "seq" sequence } { "n" "a non-negative integer" } { "slice" "a slice" } }
781 { $description "Outputs a virtual sequence sharing storage with the first " { $snippet "n" } " elements of the input sequence." }
782 { $errors "Throws an error if the index is out of bounds." } ;
783
784 HELP: tail-slice
785 { $values { "seq" sequence } { "n" "a non-negative integer" } { "slice" "a slice" } }
786 { $description "Outputs a virtual sequence sharing storage with all elements from the " { $snippet "n" } "th index until the end of the input sequence." }
787 { $errors "Throws an error if the index is out of bounds." } ;
788
789 HELP: but-last-slice
790 { $values { "seq" sequence } { "slice" "a slice" } }
791 { $description "Outputs a virtual sequence sharing storage with all but the last element of the input sequence." }
792 { $errors "Throws an error on an empty sequence." } ;
793
794 HELP: rest-slice
795 { $values { "seq" sequence } { "slice" "a slice" } }
796 { $description "Outputs a virtual sequence sharing storage with all elements from the 1st index until the end of the input sequence." }
797 { $notes "Equivalent to " { $snippet "1 tail" } }
798 { $errors "Throws an error on an empty sequence." } ;
799
800 HELP: head-slice*
801 { $values { "seq" sequence } { "n" "a non-negative integer" } { "slice" "a slice" } }
802 { $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." }
803 { $errors "Throws an error if the index is out of bounds." } ;
804
805 HELP: tail-slice*
806 { $values { "seq" sequence } { "n" "a non-negative integer" } { "slice" "a slice" } }
807 { $description "Outputs a virtual sequence sharing storage with the last " { $snippet "n" } " elements of the input sequence." }
808 { $errors "Throws an error if the index is out of bounds." } ;
809
810 HELP: head
811 { $values { "seq" sequence } { "n" "a non-negative integer" } { "headseq" "a new sequence" } }
812 { $description "Outputs a new sequence consisting of the first " { $snippet "n" } " elements of the input sequence." }
813 { $errors "Throws an error if the index is out of bounds." } ;
814
815 HELP: tail
816 { $values { "seq" sequence } { "n" "a non-negative integer" } { "tailseq" "a new sequence" } }
817 { $description "Outputs a new sequence consisting of the input sequence with the first n items removed." }
818 { $errors "Throws an error if the index is out of bounds." } ;
819
820 HELP: but-last
821 { $values { "seq" sequence } { "headseq" "a new sequence" } }
822 { $description "Outputs a new sequence consisting of the input sequence with the last item removed." }
823 { $errors "Throws an error on an empty sequence." } ;
824
825 HELP: rest
826 { $values { "seq" sequence } { "tailseq" "a new sequence" } }
827 { $description "Outputs a new sequence consisting of the input sequence with the first item removed." }
828 { $errors "Throws an error on an empty sequence." } ;
829
830 HELP: head*
831 { $values { "seq" sequence } { "n" "a non-negative integer" } { "headseq" "a new sequence" } }
832 { $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." }
833 { $errors "Throws an error if the index is out of bounds." } ;
834
835 HELP: tail*
836 { $values { "seq" sequence } { "n" "a non-negative integer" } { "tailseq" "a new sequence" } }
837 { $description "Outputs a new sequence consisting of the last " { $snippet "n" } " elements of the input sequence." }
838 { $errors "Throws an error if the index is out of bounds." } ;
839
840 HELP: shorter?
841 { $values { "seq1" sequence } { "seq2" sequence } { "?" "a boolean" } }
842 { $description "Tets if the length of " { $snippet "seq1" } " is smaller than the length of " { $snippet "seq2" } "." } ;
843
844 HELP: head?
845 { $values { "seq" sequence } { "begin" sequence } { "?" "a boolean" } }
846 { $description "Tests if " { $snippet "seq" } " starts with " { $snippet "begin" } ". If " { $snippet "begin" } " is longer than " { $snippet "seq" } ", this word outputs " { $link f } "." } ;
847
848 HELP: tail?
849 { $values { "seq" sequence } { "end" sequence } { "?" "a boolean" } }
850 { $description "Tests if " { $snippet "seq" } " ends with " { $snippet "end" } ". If " { $snippet "end" } " is longer than " { $snippet "seq" } ", this word outputs " { $link f } "." } ;
851
852 { remove remove-nth remq delq delete delete-nth } related-words
853
854 HELP: cut-slice
855 { $values { "seq" sequence } { "n" "a non-negative integer" } { "before-slice" sequence } { "after-slice" "a slice" } }
856 { $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." }
857 { $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." } ;
858
859 HELP: cut
860 { $values { "seq" sequence } { "n" "a non-negative integer" } { "before" sequence } { "after" sequence } }
861 { $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" } "." }
862 { $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." } ;
863
864 HELP: cut*
865 { $values { "seq" sequence } { "n" "a non-negative integer" } { "before" sequence } { "after" sequence } }
866 { $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" } "." } ;
867
868 HELP: start*
869 { $values { "subseq" sequence } { "seq" sequence } { "n" "a start index" } { "i" "a start index" } }
870 { $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 } "." } ;
871
872 HELP: start
873 { $values { "subseq" sequence } { "seq" sequence } { "i" "a start index" } }
874 { $description "Outputs the start index of the first contiguous subsequence equal to " { $snippet "subseq" } ", or " { $link f } " if no matching subsequence is found." } ;
875
876 HELP: subseq?
877 { $values { "subseq" sequence } { "seq" sequence } { "?" "a boolean" } }
878 { $description "Tests if " { $snippet "seq" } " contains the elements of " { $snippet "subseq" } " as a contiguous subsequence." } ;
879
880 HELP: drop-prefix
881 { $values { "seq1" sequence } { "seq2" sequence } { "slice1" "a slice" } { "slice2" "a slice" } }
882 { $description "Outputs a pair of virtual sequences with the common prefix of " { $snippet "seq1" } " and " { $snippet "seq2" } " removed." } ;
883
884 HELP: unclip
885 { $values { "seq" sequence } { "rest" sequence } { "first" object } }
886 { $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." }
887 { $examples
888     { $example "USING: prettyprint sequences ;" "{ 1 2 3 } unclip suffix ." "{ 2 3 1 }" }
889 } ;
890
891 HELP: unclip-slice
892 { $values { "seq" sequence } { "rest-slice" slice } { "first" object } }
893 { $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." }
894 { $examples { $example "USING: math.order prettyprint sequences ;" "{ 3 -1 -10 5 7 } unclip-slice [ min ] reduce ." "-10" } } ;
895
896 HELP: unclip-last
897 { $values { "seq" sequence } { "butlast" sequence } { "last" object } }
898 { $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." }
899 { $examples
900     { $example "USING: prettyprint sequences ;" "{ 1 2 3 } unclip-last prefix ." "{ 3 1 2 }" }
901 } ;
902
903 HELP: unclip-last-slice
904 { $values { "seq" sequence } { "butlast-slice" slice } { "last" object } }
905 { $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." } ;
906
907 HELP: sum
908 { $values { "seq" "a sequence of numbers" } { "n" "a number" } }
909 { $description "Outputs the sum of all elements of " { $snippet "seq" } ". Outputs zero given an empty sequence." } ;
910
911 HELP: product
912 { $values { "seq" "a sequence of numbers" } { "n" "a number" } }
913 { $description "Outputs the product of all elements of " { $snippet "seq" } ". Outputs one given an empty sequence." } ;
914
915 HELP: infimum
916 { $values { "seq" "a sequence of real numbers" } { "n" "a number" } }
917 { $description "Outputs the least element of " { $snippet "seq" } "." }
918 { $errors "Throws an error if the sequence is empty." } ;
919
920 HELP: supremum
921 { $values { "seq" "a sequence of real numbers" } { "n" "a number" } }
922 { $description "Outputs the greatest element of " { $snippet "seq" } "." }
923 { $errors "Throws an error if the sequence is empty." } ;
924
925 HELP: produce
926 { $values { "pred" { $quotation "( -- ? )" } } { "quot" { $quotation "( -- obj )" } } { "seq" "a sequence" } }
927 { $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." }
928 { $examples
929     "The following example divides a number by two until we reach zero, and accumulates intermediate results:"
930     { $example "USING: kernel math prettyprint sequences ;" "1337 [ dup 0 > ] [ 2/ dup ] produce nip ." "{ 668 334 167 83 41 20 10 5 2 1 0 }" }
931     "The following example collects random numbers as long as they are greater than 1:"
932     { $unchecked-example "USING: kernel prettyprint random sequences ;" "[ 10 random dup 1 > ] [ ] produce nip ." "{ 8 2 2 9 }" }
933 } ;
934
935 HELP: produce-as
936 { $values { "pred" { $quotation "( -- ? )" } } { "quot" { $quotation "( -- obj )" } } { "exemplar" sequence } { "seq" "a sequence" } }
937 { $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." }
938 { $examples "See " { $link produce } " for examples." } ;
939
940 HELP: sigma
941 { $values { "seq" sequence } { "quot" quotation } { "n" number } }
942 { $description "Like map sum, but without creating an intermediate sequence." }
943 { $example
944     "! Find the sum of the squares [0,99]"
945     "USING: math math.ranges sequences prettyprint ;"
946     "100 [1,b] [ sq ] sigma ."
947     "338350"
948 } ;
949
950 HELP: count
951 { $values { "seq" sequence } { "quot" quotation } { "n" integer } }
952 { $description "Efficiently returns the number of elements that the predicate quotation matches." }
953 { $example
954     "USING: math math.ranges sequences prettyprint ;"
955     "100 [1,b] [ even? ] count ."
956     "50"
957 } ;
958
959 HELP: pusher
960 { $values
961      { "quot" "a predicate quotation" }
962      { "quot" quotation } { "accum" vector } }
963 { $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." }
964 { $example "! Find all the even numbers:" "USING: prettyprint sequences math kernel ;"
965            "10 [ even? ] pusher [ each ] dip ."
966            "V{ 0 2 4 6 8 }"
967 }
968 { $notes "Used to implement the " { $link filter } " word." } ;
969
970 HELP: trim-head
971 { $values
972      { "seq" sequence } { "quot" quotation }
973      { "newseq" sequence } }
974 { $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." }
975 { $example "" "USING: prettyprint math sequences ;"
976            "{ 0 0 1 2 3 0 0 } [ zero? ] trim-head ."
977            "{ 1 2 3 0 0 }"
978 } ;
979
980 HELP: trim-head-slice
981 { $values
982      { "seq" sequence } { "quot" quotation }
983      { "slice" slice } }
984 { $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" }
985 { $example "" "USING: prettyprint math sequences ;"
986            "{ 0 0 1 2 3 0 0 } [ zero? ] trim-head-slice ."
987            "T{ slice { from 2 } { to 7 } { seq { 0 0 1 2 3 0 0 } } }"
988 } ;
989
990 HELP: trim-tail
991 { $values
992      { "seq" sequence } { "quot" quotation }
993      { "newseq" sequence } }
994 { $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." }
995 { $example "" "USING: prettyprint math sequences ;"
996            "{ 0 0 1 2 3 0 0 } [ zero? ] trim-tail ."
997            "{ 0 0 1 2 3 }"
998 } ;
999
1000 HELP: trim-tail-slice
1001 { $values
1002      { "seq" sequence } { "quot" quotation }
1003      { "slice" slice } }
1004 { $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." }
1005 { $example "" "USING: prettyprint math sequences ;"
1006            "{ 0 0 1 2 3 0 0 } [ zero? ] trim-tail-slice ."
1007            "T{ slice { from 0 } { to 5 } { seq { 0 0 1 2 3 0 0 } } }"
1008 } ;
1009
1010 HELP: trim
1011 { $values
1012      { "seq" sequence } { "quot" quotation }
1013      { "newseq" sequence } }
1014 { $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." }
1015 { $example "" "USING: prettyprint math sequences ;"
1016            "{ 0 0 1 2 3 0 0 } [ zero? ] trim ."
1017            "{ 1 2 3 }"
1018 } ;
1019
1020 HELP: trim-slice
1021 { $values
1022      { "seq" sequence } { "quot" quotation }
1023      { "slice" slice } }
1024 { $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." }
1025 { $example "" "USING: prettyprint math sequences ;"
1026            "{ 0 0 1 2 3 0 0 } [ zero? ] trim-slice ."
1027            "T{ slice { from 2 } { to 5 } { seq { 0 0 1 2 3 0 0 } } }"
1028 } ;
1029
1030 { trim trim-slice trim-head trim-head-slice trim-tail trim-tail-slice } related-words
1031
1032 HELP: sift
1033 { $values
1034      { "seq" sequence }
1035      { "newseq" sequence } }
1036  { $description "Outputs a new sequence with all instance of " { $link f  } " removed." }
1037  { $examples 
1038     { $example "USING: prettyprint sequences ;"
1039         "{ \"a\" 3 { } f } sift ."
1040         "{ \"a\" 3 { } }"
1041     }
1042 } ;
1043
1044 HELP: harvest
1045 { $values
1046      { "seq" sequence }
1047      { "newseq" sequence } }
1048 { $description "Outputs a new sequence with all empty sequences removed." }
1049 { $examples 
1050     { $example "USING: prettyprint sequences ;"
1051                "{ { } { 2 3 } { 5 } { } } harvest ."
1052                "{ { 2 3 } { 5 } }"
1053     }
1054 } ;
1055
1056 { filter filter-here sift harvest } related-words
1057
1058 HELP: set-first
1059 { $values
1060      { "first" object } { "seq" sequence } }
1061 { $description "Sets the first element of a sequence." }
1062 { $examples 
1063     { $example "USING: prettyprint kernel sequences ;"
1064         "{ 1 2 3 4  } 5 over set-first ."
1065         "{ 5 2 3 4 }"
1066     }
1067 } ;
1068
1069 HELP: set-second
1070 { $values
1071      { "second" object } { "seq" sequence } }
1072 { $description "Sets the second element of a sequence." }
1073 { $examples 
1074     { $example "USING: prettyprint kernel sequences ;"
1075         "{ 1 2 3 4  } 5 over set-second ."
1076         "{ 1 5 3 4 }"
1077     }
1078 } ;
1079
1080 HELP: set-third
1081 { $values
1082      { "third" object } { "seq" sequence } }
1083 { $description "Sets the third element of a sequence." }
1084 { $examples 
1085     { $example "USING: prettyprint kernel sequences ;"
1086         "{ 1 2 3 4  } 5 over set-third ."
1087         "{ 1 2 5 4 }"
1088     }
1089 } ;
1090
1091 HELP: set-fourth
1092 { $values
1093      { "fourth" object } { "seq" sequence } }
1094 { $description "Sets the fourth element of a sequence." }
1095 { $examples 
1096     { $example "USING: prettyprint kernel sequences ;"
1097         "{ 1 2 3 4  } 5 over set-fourth ."
1098         "{ 1 2 3 5 }"
1099     }
1100 } ;
1101
1102 { set-first set-second set-third set-fourth } related-words
1103
1104 HELP: replicate
1105 { $values
1106      { "seq" sequence } { "quot" { $quotation "( -- elt )" } }
1107      { "newseq" sequence } }
1108 { $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." }
1109 { $examples 
1110     { $unchecked-example "USING: prettyprint kernel sequences ;"
1111         "5 [ 100 random ] replicate ."
1112         "{ 52 10 45 81 30 }"
1113     }
1114 } ;
1115
1116 HELP: replicate-as
1117 { $values
1118      { "seq" sequence } { "quot" quotation } { "exemplar" sequence }
1119      { "newseq" sequence } }
1120 { $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." }
1121 { $examples 
1122     { $unchecked-example "USING: prettyprint kernel sequences ;"
1123         "5 [ 100 random ] B{ } replicate-as ."
1124         "B{ 44 8 2 33 18 }"
1125     }
1126 } ;
1127 { replicate replicate-as } related-words
1128
1129 HELP: partition
1130 { $values
1131      { "seq" sequence } { "quot" quotation }
1132      { "trueseq" sequence } { "falseseq" sequence } }
1133      { $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" } "." }
1134 { $examples 
1135     { $example "USING: prettyprint kernel math sequences ;"
1136         "{ 1 2 3 4 5 } [ even? ] partition [ . ] bi@"
1137         "{ 2 4 }\n{ 1 3 5 }"
1138     }
1139 } ;
1140
1141 HELP: virtual-seq
1142 { $values
1143      { "seq" sequence }
1144      { "seq'" sequence } }
1145 { $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@ } "." } ;
1146
1147 HELP: virtual@
1148 { $values
1149      { "n" integer } { "seq" sequence }
1150      { "n'" integer } { "seq'" sequence } }
1151 { $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 } "." } ;
1152
1153 HELP: 2map-reduce
1154 { $values
1155      { "seq1" sequence } { "seq2" sequence } { "map-quot" quotation } { "reduce-quot" quotation }
1156      { "result" object } }
1157 { $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." }
1158 { $examples { $example "USING: sequences prettyprint math ;"
1159     "{ 10 30 50 } { 200 400 600 } [ + ] [ + ] 2map-reduce ."
1160     "1290"
1161 } } ;
1162
1163 HELP: 2pusher
1164 { $values
1165      { "quot" quotation }
1166      { "quot" quotation } { "accum1" vector } { "accum2" vector } }
1167 { $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." } ;
1168
1169 HELP: 2reverse-each
1170 { $values
1171      { "seq1" sequence } { "seq2" sequence } { "quot" quotation } }
1172 { $description "Reverse the sequences using the " { $link <reversed> } " word and calls " { $link 2each } " on the reversed sequences." }
1173 { $examples { $example "USING: sequences math prettyprint ;"
1174     "{ 10 20 30 } { 1 2 3 } [ + . ] 2reverse-each"
1175     "33\n22\n11"
1176 } } ;
1177
1178 HELP: 2unclip-slice
1179 { $values
1180      { "seq1" sequence } { "seq2" sequence }
1181      { "rest-slice1" sequence } { "rest-slice2" sequence } { "first1" object } { "first2" object } }
1182 { $description "Unclips the first element of each sequence and leaves two slice elements and the two unclipped objects on the stack." }
1183 { $examples { $example "USING: sequences prettyprint kernel arrays ;"
1184     "{ 1 2 } { 3 4 } 2unclip-slice 4array [ . ] each"
1185     "T{ slice { from 1 } { to 2 } { seq { 1 2 } } }\nT{ slice { from 1 } { to 2 } { seq { 3 4 } } }\n1\n3"
1186 } } ;
1187
1188 HELP: accumulator
1189 { $values
1190      { "quot" quotation }
1191      { "quot'" quotation } { "vec" vector } }
1192 { $description "Creates a new quotation that pushes its result to a vector and outputs that vector on the stack." }
1193 { $examples { $example "USING: sequences prettyprint kernel math ;"
1194     "{ 1 2 } [ 30 + ] accumulator [ each ] dip ."
1195     "V{ 31 32 }"
1196 } } ;
1197
1198 HELP: binary-reduce
1199 { $values
1200      { "seq" sequence } { "start" integer } { "quot" quotation }
1201      { "value" object } }
1202 { $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 } "." }
1203 { $examples "Computing factorial:"
1204     { $example "USING: prettyprint sequences math ;"
1205     "40 rest-slice 1 [ * ] binary-reduce ."
1206     "20397882081197443358640281739902897356800000000" }
1207 } ;
1208
1209 HELP: follow
1210 { $values
1211      { "obj" object } { "quot" quotation }
1212      { "seq" sequence } }
1213 { $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." }
1214 { $examples "Get random numbers until zero is reached:"
1215     { $unchecked-example
1216     "USING: random sequences prettyprint math ;"
1217     "100 [ random dup zero? [ drop f ] when ] follow ."
1218     "{ 100 86 34 32 24 11 7 2 }"
1219 } } ;
1220
1221 HELP: halves
1222 { $values
1223      { "seq" sequence }
1224      { "first-slice" slice } { "second-slice" slice } }
1225 { $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." }
1226 { $examples { $example "USING: arrays sequences prettyprint kernel ;"
1227     "{ 1 2 3 4 5 } halves [ >array . ] bi@"
1228     "{ 1 2 }\n{ 3 4 5 }"
1229 } } ;
1230
1231 HELP: indices
1232 { $values
1233      { "obj" object } { "seq" sequence }
1234      { "indices" sequence } }
1235 { $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." }
1236 { $examples { $example "USING: sequences prettyprint ;"
1237     "2 { 2 4 2 6 2 8 2 10 } indices ."
1238     "V{ 0 2 4 6 }"
1239 } } ;
1240
1241 HELP: insert-nth
1242 { $values
1243      { "elt" object } { "n" integer } { "seq" sequence }
1244      { "seq'" sequence } }
1245 { $description "Creates a new sequence where the " { $snippet "n" } "th index is set to the input object." }
1246 { $examples { $example "USING: prettyprint sequences ;"
1247     "40 3 { 10 20 30 50 } insert-nth ."
1248     "{ 10 20 30 40 50 }"
1249 } } ;
1250
1251 HELP: map-reduce
1252 { $values
1253      { "seq" sequence } { "map-quot" quotation } { "reduce-quot" quotation }
1254      { "result" object } }
1255 { $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." }
1256 { $examples { $example "USING: sequences prettyprint math ;"
1257     "{ 1 3 5 } [ sq ] [ + ] map-reduce ."
1258     "35"
1259 } } ;
1260
1261 HELP: new-like
1262 { $values
1263      { "len" integer } { "exemplar" "an exemplar sequence" } { "quot" quotation }
1264      { "seq" sequence } }
1265 { $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." } ;
1266
1267 HELP: push-either
1268 { $values
1269      { "elt" object } { "quot" quotation } { "accum1" vector } { "accum2" vector } }
1270 { $description "Pushes the input object onto one of the accumualators; the first if the quotation yields true, the second if false." } ;
1271
1272 HELP: sequence-hashcode
1273 { $values
1274      { "n" integer } { "seq" sequence }
1275      { "x" integer } }
1276 { $description "Iterates over a sequence, computes a hashcode with " { $link hashcode* } " for each element, and combines them using " { $link sequence-hashcode-step } "." } ;
1277
1278 HELP: sequence-hashcode-step
1279 { $values
1280      { "oldhash" integer } { "newpart" integer }
1281      { "newhash" integer } }
1282 { $description "An implementation word that computes a running hashcode of a sequence using some bit-twiddling. The resulting hashcode is always a fixnum." } ;
1283
1284 HELP: short
1285 { $values
1286      { "seq" sequence } { "n" integer }
1287      { "seq" sequence } { "n'" integer } }
1288 { $description "Returns the input sequence and its length or " { $snippet "n" } ", whichever is less." }
1289 { $examples { $example "USING: sequences kernel prettyprint ;"
1290     "\"abcd\" 3 short [ . ] bi@"
1291     "\"abcd\"\n3"
1292 } } ;
1293
1294 HELP: shorten
1295 { $values
1296      { "n" integer } { "seq" sequence } }
1297 { $description "Shortens a " { $link "growable" } " sequence to by " { $snippet "n" } " elements long." }
1298 { $examples { $example "USING: sequences prettyprint kernel ;"
1299     "V{ 1 2 3 4 5 } 3 over shorten ."
1300     "V{ 1 2 3 }"
1301 } } ;
1302
1303 HELP: iota
1304 { $values { "n" integer } { "iota" iota } }
1305 { $description "Creates an immutable virtual sequence containing the integers from 0 to " { $snippet "n-1" } "." }
1306 { $examples
1307   { $example
1308     "USING: math sequences prettyprint ;"
1309     "3 iota [ sq ] map ."
1310     "{ 0 1 4 }"
1311   }
1312 } ;
1313
1314 ARTICLE: "sequences-unsafe" "Unsafe sequence operations"
1315 "The " { $link nth-unsafe } " and " { $link set-nth-unsafe } " sequence protocol bypasses bounds checks for increased performance."
1316 $nl
1317 "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."
1318 $nl
1319 "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."
1320 $nl
1321 "The justification for this is that the VM should not crash if a resizable sequence is resized during the execution of an iteration combinator."
1322 $nl
1323 "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 } "." ;
1324
1325 ARTICLE: "sequence-protocol" "Sequence protocol"
1326 "All sequences must be instances of a mixin class:"
1327 { $subsection sequence }
1328 { $subsection sequence? }
1329 "All sequences must know their length:"
1330 { $subsection length }
1331 "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:"
1332 { $subsection nth }
1333 { $subsection nth-unsafe }
1334 "Note that sequences are always indexed starting from zero."
1335 $nl
1336 "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:"
1337 { $subsection set-nth }
1338 { $subsection set-nth-unsafe }
1339 "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:"
1340 { $subsection immutable }
1341 "The following two generic words are optional, as not all sequences are resizable:"
1342 { $subsection set-length }
1343 { $subsection lengthen }
1344 "An optional generic word for creating sequences of the same class as a given sequence:"
1345 { $subsection like }
1346 "Optional generic words for optimization purposes:"
1347 { $subsection new-sequence }
1348 { $subsection new-resizable }
1349 { $see-also "sequences-unsafe" } ;
1350
1351 ARTICLE: "virtual-sequences-protocol" "Virtual sequence protocol"
1352 "Virtual sequences must know their length:"
1353 { $subsection length }
1354 "The underlying sequence to look up a value in:"
1355 { $subsection virtual-seq }
1356 "The index of the value in the underlying sequence:"
1357 { $subsection virtual@ } ;
1358
1359 ARTICLE: "virtual-sequences" "Virtual sequences"
1360 "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" } "."
1361 { $subsection "virtual-sequences-protocol" } ;
1362
1363 ARTICLE: "sequences-integers" "Counted loops"
1364 "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."
1365 $nl
1366 "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:"
1367 { $example "3 [ . ] each" "0\n1\n2" }
1368 "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 } "."
1369 $nl
1370 "Combinators that produce new sequences, such as " { $link map } ", will output an array if the input is an integer."
1371 $nl
1372 "More elaborate counted loops can be performed with " { $link "math.ranges" } "." ;
1373
1374 ARTICLE: "sequences-access" "Accessing sequence elements"
1375 { $subsection ?nth }
1376 "Concise way of extracting one of the first four elements:"
1377 { $subsection first }
1378 { $subsection second }
1379 { $subsection third }
1380 { $subsection fourth }
1381 "Extracting the last element:"
1382 { $subsection last }
1383 "Unpacking sequences:"
1384 { $subsection first2 }
1385 { $subsection first3 }
1386 { $subsection first4 }
1387 { $see-also nth } ;
1388
1389 ARTICLE: "sequences-add-remove" "Adding and removing sequence elements"
1390 "Adding elements:"
1391 { $subsection prefix }
1392 { $subsection suffix }
1393 "Removing elements:"
1394 { $subsection remove }
1395 { $subsection remq }
1396 { $subsection remove-nth } ;
1397
1398 ARTICLE: "sequences-reshape" "Reshaping sequences"
1399 "A " { $emphasis "repetition" } " is a virtual sequence consisting of a single element repeated multiple times:"
1400 { $subsection repetition }
1401 { $subsection <repetition> }
1402 "Reversing a sequence:"
1403 { $subsection reverse }
1404 "A " { $emphasis "reversal" } " presents a reversed view of an underlying sequence:"
1405 { $subsection reversed }
1406 { $subsection <reversed> }
1407 "Transposing a matrix:"
1408 { $subsection flip } ;
1409
1410 ARTICLE: "sequences-appending" "Appending sequences"
1411 { $subsection append }
1412 { $subsection append-as }
1413 { $subsection prepend }
1414 { $subsection 3append }
1415 { $subsection 3append-as }
1416 { $subsection surround }
1417 { $subsection glue }
1418 { $subsection concat }
1419 { $subsection join }
1420 "A pair of words useful for aligning strings:"
1421 { $subsection pad-head }
1422 { $subsection pad-tail } ;
1423
1424 ARTICLE: "sequences-slices" "Subsequences and slices"
1425 "Extracting a subsequence:"
1426 { $subsection subseq }
1427 { $subsection head }
1428 { $subsection tail }
1429 { $subsection head* }
1430 { $subsection tail* }
1431 "Removing the first or last element:"
1432 { $subsection rest }
1433 { $subsection but-last }
1434 "Taking a sequence apart into a head and a tail:"
1435 { $subsection unclip }
1436 { $subsection unclip-last }
1437 { $subsection cut }
1438 { $subsection cut* }
1439 "A " { $emphasis "slice" } " is a virtual sequence which presents as view of a subsequence of an underlying sequence:"
1440 { $subsection slice }
1441 { $subsection slice? }
1442 "Extracting a slice:"
1443 { $subsection <slice> }
1444 { $subsection head-slice }
1445 { $subsection tail-slice }
1446 { $subsection head-slice* }
1447 { $subsection tail-slice* }
1448 "Removing the first or last element:"
1449 { $subsection rest-slice }
1450 { $subsection but-last-slice }
1451 "Taking a sequence apart into a head and a tail:"
1452 { $subsection unclip-slice }
1453 { $subsection unclip-last-slice }
1454 { $subsection cut-slice }
1455 "A utility for words which use slices as iterators:"
1456 { $subsection <flat-slice> }
1457 "Replacing slices with new elements:"
1458 { $subsection replace-slice } ;
1459
1460 ARTICLE: "sequences-combinators" "Sequence combinators"
1461 "Iteration:"
1462 { $subsection each }
1463 { $subsection each-index }
1464 { $subsection reduce }
1465 { $subsection interleave }
1466 { $subsection replicate }
1467 { $subsection replicate-as }
1468 "Mapping:"
1469 { $subsection map }
1470 { $subsection map-as }
1471 { $subsection map-index }
1472 { $subsection map-reduce }
1473 { $subsection accumulate }
1474 { $subsection produce }
1475 { $subsection produce-as }
1476 "Filtering:"
1477 { $subsection filter }
1478 { $subsection partition }
1479 "Testing if a sequence contains elements satisfying a predicate:"
1480 { $subsection any? }
1481 { $subsection all? }
1482 { $subsection "sequence-2combinators" }
1483 { $subsection "sequence-3combinators" } ;
1484
1485 ARTICLE: "sequence-2combinators" "Pair-wise sequence combinators"
1486 "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."
1487 { $subsection 2each }
1488 { $subsection 2reduce }
1489 { $subsection 2map }
1490 { $subsection 2map-as }
1491 { $subsection 2map-reduce }
1492 { $subsection 2all? } ;
1493
1494 ARTICLE: "sequence-3combinators" "Triple-wise sequence combinators"
1495 "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."
1496 { $subsection 3each }
1497 { $subsection 3map }
1498 { $subsection 3map-as } ;
1499
1500 ARTICLE: "sequences-tests" "Testing sequences"
1501 "Testing for an empty sequence:"
1502 { $subsection empty? }
1503 "Testing indices:"
1504 { $subsection bounds-check? }
1505 "Testing if a sequence contains an object:"
1506 { $subsection member? }
1507 { $subsection memq? }
1508 "Testing if a sequence contains a subsequence:"
1509 { $subsection head? }
1510 { $subsection tail? }
1511 { $subsection subseq? } ;
1512
1513 ARTICLE: "sequences-search" "Searching sequences"
1514 "Finding the index of an element:"
1515 { $subsection index }
1516 { $subsection index-from }
1517 { $subsection last-index }
1518 { $subsection last-index-from }
1519 "Finding the start of a subsequence:"
1520 { $subsection start }
1521 { $subsection start* }
1522 "Finding the index of an element satisfying a predicate:"
1523 { $subsection find }
1524 { $subsection find-from }
1525 { $subsection find-last }
1526 { $subsection find-last-from }
1527 { $subsection map-find } ;
1528
1529 ARTICLE: "sequences-trimming" "Trimming sequences"
1530 "Trimming words:"
1531 { $subsection trim }
1532 { $subsection trim-head }
1533 { $subsection trim-tail }
1534 "Potentially more efficient trim:"
1535 { $subsection trim-slice }
1536 { $subsection trim-head-slice }
1537 { $subsection trim-tail-slice } ;
1538
1539 ARTICLE: "sequences-destructive-discussion" "When to use destructive operations"
1540 "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:"
1541 { $list
1542     "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."
1543     { "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." }
1544 }
1545 "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." ;
1546
1547 ARTICLE: "sequences-destructive" "Destructive operations"
1548 "These words modify their input, instead of creating a new sequence."
1549 { $subsection "sequences-destructive-discussion" }
1550 "Changing elements:"
1551 { $subsection change-each }
1552 { $subsection change-nth }
1553 "Deleting elements:"
1554 { $subsection delete }
1555 { $subsection delq }
1556 { $subsection delete-nth }
1557 { $subsection delete-slice }
1558 { $subsection delete-all }
1559 { $subsection filter-here }
1560 "Other destructive words:"
1561 { $subsection reverse-here }
1562 { $subsection push-all }
1563 { $subsection move }
1564 { $subsection exchange }
1565 { $subsection copy }
1566 "Many operations have constructive and destructive variants:"
1567 { $table
1568     { "Constructive" "Destructive" }
1569     { { $link suffix } { $link push } }
1570     { { $link but-last } { $link pop* } }
1571     { { $link unclip-last } { $link pop } }
1572     { { $link remove } { $link delete } }
1573     { { $link remq } { $link delq } }
1574     { { $link remove-nth } { $link delete-nth } }
1575     { { $link reverse } { $link reverse-here } }
1576     { { $link append } { $link push-all } }
1577     { { $link map } { $link change-each } }
1578     { { $link filter } { $link filter-here } }
1579 }
1580 { $see-also set-nth push pop "sequences-stacks" } ;
1581
1582 ARTICLE: "sequences-stacks" "Treating sequences as stacks"
1583 "The classical stack operations, modifying a sequence in place:"
1584 { $subsection push }
1585 { $subsection pop }
1586 { $subsection pop* }
1587 { $see-also empty? } ;
1588
1589 ARTICLE: "sequences-comparing" "Comparing sequences"
1590 "Element equality testing:"
1591 { $subsection sequence= }
1592 { $subsection mismatch }
1593 { $subsection drop-prefix }
1594 "The " { $link <=> } " generic word performs lexicographic comparison when applied to sequences." ;
1595
1596 ARTICLE: "sequences-f" "The f object as a sequence"
1597 "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." ;
1598
1599 ARTICLE: "sequences" "Sequence operations"
1600 "A " { $emphasis "sequence" } " is a finite, linearly-ordered collection of elements. Words for working with sequences are in the " { $vocab-link "sequences" } " vocabulary."
1601 $nl
1602 "Sequences implement a protocol:"
1603 { $subsection "sequence-protocol" }
1604 { $subsection "sequences-f" }
1605 "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" } "."
1606 { $subsection "sequences-access" }
1607 { $subsection "sequences-combinators" }
1608 { $subsection "sequences-add-remove" }
1609 { $subsection "sequences-appending" }
1610 { $subsection "sequences-slices" }
1611 { $subsection "sequences-reshape" }
1612 { $subsection "sequences-tests" }
1613 { $subsection "sequences-search" }
1614 { $subsection "sequences-comparing" }
1615 { $subsection "sequences-split" }
1616 { $subsection "grouping" }
1617 { $subsection "sequences-destructive" }
1618 { $subsection "sequences-stacks" }
1619 { $subsection "sequences-sorting" }
1620 { $subsection "binary-search" }
1621 { $subsection "sets" }
1622 { $subsection "sequences-trimming" }
1623 { $subsection "sequences.deep" }
1624 "Using sequences for looping:"
1625 { $subsection "sequences-integers" }
1626 { $subsection "math.ranges" }
1627 "For inner loops:"
1628 { $subsection "sequences-unsafe" } ;
1629
1630 ABOUT: "sequences"