]> gitweb.factorcode.org Git - factor.git/blob - core/sequences/sequences-docs.factor
sequences: adding cartesian-find.
[factor.git] / core / sequences / sequences-docs.factor
1 USING: assocs arrays generic.single help.markup help.syntax kernel
2 layouts math math.order quotations sequences.private vectors ;
3 IN: sequences
4
5 HELP: sequence
6 { $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:"
7     { $code "INSTANCE: my-sequence sequence" }
8 } ;
9
10 HELP: length
11 { $values { "seq" sequence } { "n" "a non-negative integer" } }
12 { $contract "Outputs the length of the sequence. All sequences support this operation." } ;
13
14 HELP: set-length
15 { $values { "n" "a non-negative integer" } { "seq" "a resizable sequence" } }
16 { $contract "Resizes a sequence. The initial contents of the new area is undefined." }
17 { $errors "Throws a " { $link no-method } " error if the sequence is not resizable, and a " { $link bounds-error } " if the new length is negative." }
18 { $side-effects "seq" } ;
19
20 HELP: lengthen
21 { $values { "n" "a non-negative integer" } { "seq" "a resizable sequence" } }
22 { $contract "Ensures the sequence has a length of at least " { $snippet "n" } " elements. This word differs from " { $link set-length } " in two respects:"
23     { $list
24         { "This word does not shrink the sequence if " { $snippet "n" } " is less than its length." }
25         { "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." }
26     }
27 } ;
28
29 HELP: nth
30 { $values { "n" "a non-negative integer" } { "seq" sequence } { "elt" "the element at the " { $snippet "n" } "th index" } }
31 { $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." }
32 { $errors "Throws a " { $link bounds-error } " if the index is negative, or greater than or equal to the length of the sequence." } ;
33
34 HELP: set-nth
35 { $values { "elt" object } { "n" "a non-negative integer" } { "seq" "a mutable sequence" } }
36 { $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." }
37 { $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."
38 $nl
39 "Throws an error if the sequence cannot hold elements of the given type." }
40 { $side-effects "seq" } ;
41
42 HELP: nths
43 { $values
44      { "indices" sequence } { "seq" sequence }
45      { "seq'" sequence } }
46 { $description "Outputs a sequence of elements from the input sequence indexed by the indices." }
47 { $examples
48     { $example "USING: prettyprint sequences ;"
49                "{ 0 2 } { \"a\" \"b\" \"c\" } nths ."
50                "{ \"a\" \"c\" }"
51     }
52 } ;
53
54 HELP: immutable
55 { $values { "seq" sequence } }
56 { $description "Throws an " { $link immutable } " error." }
57 { $error-description "Thrown if an attempt is made to modify an immutable sequence." } ;
58
59 HELP: new-sequence
60 { $values { "len" "a non-negative integer" } { "seq" sequence } { "newseq" "a mutable sequence" } }
61 { $contract "Outputs a mutable sequence of length " { $snippet "len" } " which can hold the elements of " { $snippet "seq" } ". The initial contents of the sequence are undefined." } ;
62
63 HELP: new-resizable
64 { $values { "len" "a non-negative integer" } { "seq" sequence } { "newseq" "a resizable mutable sequence" } }
65 { $contract "Outputs a resizable mutable sequence with an initial capacity of " { $snippet "len" } " elements and zero length, which can hold the elements of " { $snippet "seq" } "." }
66 { $examples
67     { $example "USING: prettyprint sequences ;" "300 V{ } new-resizable ." "V{ }" }
68     { $example "USING: prettyprint sequences ;" "300 SBUF\" \" new-resizable ." "SBUF\" \"" }
69 } ;
70
71 HELP: like
72 { $values { "seq" sequence } { "exemplar" sequence } { "newseq" "a new sequence" } }
73 { $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."
74 $nl
75 "The default implementation does nothing." }
76 { $notes "Unlike " { $link clone-like } ", the output sequence might share storage with the input sequence." } ;
77
78 HELP: empty?
79 { $values { "seq" sequence } { "?" boolean } }
80 { $description "Tests if the sequence has zero length." } ;
81
82 HELP: if-empty
83 { $values { "seq" sequence } { "quot1" quotation } { "quot2" quotation } }
84 { $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." }
85 { $examples
86     { $example
87         "USING: kernel prettyprint sequences ;"
88         "{ 1 2 3 } [ \"empty sequence\" ] [ sum ] if-empty ."
89         "6"
90     }
91 } ;
92
93 HELP: when-empty
94 { $values
95      { "seq" sequence } { "quot" "the first quotation of an " { $link if-empty } } }
96 { $description "Makes an implicit check if the sequence is empty. An empty sequence is dropped and the " { $snippet "quot" } " is called." }
97 { $examples "This word is equivalent to " { $link if-empty } " with an empty second quotation:"
98     { $example
99     "USING: sequences prettyprint ;"
100     "{ } [ { 4 5 6 } ] [ ] if-empty ."
101     "{ 4 5 6 }"
102     }
103     { $example
104     "USING: sequences prettyprint ;"
105     "{ } [ { 4 5 6 } ] when-empty ."
106     "{ 4 5 6 }"
107     }
108 } ;
109
110 HELP: unless-empty
111 { $values
112      { "seq" sequence } { "quot" "the second quotation of an " { $link if-empty } } }
113 { $description "Makes an implicit check if the sequence is empty. An empty sequence is dropped. Otherwise, the " { $snippet "quot" } " is called on the sequence." }
114 { $examples "This word is equivalent to " { $link if-empty } " with an empty first quotation:"
115     { $example
116     "USING: sequences prettyprint ;"
117     "{ 4 5 6 } [ ] [ sum . ] if-empty"
118     "15"
119     }
120     { $example
121     "USING: sequences prettyprint ;"
122     "{ 4 5 6 } [ sum . ] unless-empty"
123     "15"
124     }
125 } ;
126
127 HELP: delete-all
128 { $values { "seq" "a resizable sequence" } }
129 { $description "Resizes the sequence to zero length, removing all elements. Not all sequences are resizable." }
130 { $errors "Throws an error if the sequence is not resizable." }
131 { $side-effects "seq" } ;
132
133 HELP: resize
134 { $values { "n" "a non-negative integer" } { "seq" sequence } { "newseq" "a new sequence" } }
135 { $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." }
136 { $notes "This generic word is only implemented for strings and arrays." } ;
137
138 HELP: first
139 { $values { "seq" sequence } { "first" "the first element of the sequence" } }
140 { $description "Outputs the first element of the sequence." }
141 { $errors "Throws an error if the sequence is empty." } ;
142
143 HELP: second
144 { $values { "seq" sequence } { "second" "the second element of the sequence" } }
145 { $description "Outputs the second element of the sequence." }
146 { $errors "Throws an error if the sequence contains less than two elements." } ;
147
148 HELP: third
149 { $values { "seq" sequence } { "third" "the third element of the sequence" } }
150 { $description "Outputs the third element of the sequence." }
151 { $errors "Throws an error if the sequence contains less than three elements." } ;
152
153 HELP: fourth
154 { $values { "seq" sequence } { "fourth" "the fourth element of the sequence" } }
155 { $description "Outputs the fourth element of the sequence." }
156 { $errors "Throws an error if the sequence contains less than four elements." } ;
157
158 HELP: push
159 { $values { "elt" object } { "seq" "a resizable mutable sequence" } }
160 { $description "Adds an element at the end of the sequence. The sequence length is adjusted accordingly." }
161 { $errors "Throws an error if " { $snippet "seq" } " is not resizable, or if the type of " { $snippet "elt" } " is not permitted in " { $snippet "seq" } "." }
162 { $side-effects "seq" } ;
163
164 HELP: bounds-check?
165 { $values { "n" integer } { "seq" sequence } { "?" boolean } }
166 { $description "Tests if the index is within the bounds of the sequence." } ;
167
168 HELP: bounds-error
169 { $values { "n" integer } { "seq" sequence } }
170 { $description "Throws a " { $link bounds-error } "." }
171 { $error-description "Thrown by " { $link nth } ", " { $link set-nth } " and " { $link set-length } " if the given index lies beyond the bounds of the sequence." } ;
172
173 HELP: bounds-check
174 { $values { "n" integer } { "seq" sequence } }
175 { $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." } ;
176
177 HELP: ?nth
178 { $values { "n" integer } { "seq" sequence } { "elt/f" { $maybe object } } }
179 { $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 } "." } ;
180
181 HELP: ?set-nth
182 { $values { "elt" object } { "n" integer } { "seq" sequence } }
183 { $description "A forgiving version of " { $link set-nth } ". If the index is out of bounds, does nothing." } ;
184
185 HELP: ?first
186 { $values { "seq" sequence } { "elt/f" { $maybe object } } }
187 { $description "A forgiving version of " { $link first } ". If the sequence is empty, or if the sequence is " { $link f } ", simply outputs " { $link f } "." }
188 { $examples
189     "On an empty sequence:"
190     { $example "USING: sequences prettyprint ;"
191                "{ } ?first ."
192                "f"
193     }
194     "Works like first on sequences with elements:"
195     { $example "USING: sequences prettyprint ;"
196                "{ 1 2 3 } ?first ."
197                "1"
198     }
199 } ;
200
201
202 HELP: ?second
203 { $values { "seq" sequence } { "elt/f" { $maybe object } } }
204 { $description "A forgiving version of " { $link second } ". If the sequence has less than two elements, or if the sequence is " { $link f } ", simply outputs " { $link f } "." } ;
205
206 HELP: ?last
207 { $values { "seq" sequence } { "elt/f" { $maybe object } } }
208 { $description "A forgiving version of " { $link last } ". If the sequence is empty, or if the sequence is " { $link f } ", simply outputs " { $link f } "." } ;
209
210 HELP: nth-unsafe
211 { $values { "n" integer } { "seq" sequence } { "elt" object } }
212 { $contract "Unsafe variant of " { $link nth } " that does not perform bounds checks." } ;
213
214 HELP: set-nth-unsafe
215 { $values { "elt" object } { "n" integer } { "seq" sequence } }
216 { $contract "Unsafe variant of " { $link set-nth } " that does not perform bounds checks." } ;
217
218 HELP: exchange-unsafe
219 { $values { "m" "a non-negative integer" } { "n" "a non-negative integer" } { "seq" "a mutable sequence" } }
220 { $description "Unsafe variant of " { $link exchange } " that does not perform bounds checks." } ;
221
222 HELP: first-unsafe
223 { $values { "seq" sequence } { "first" "the first element" } }
224 { $contract "Unsafe variant of " { $link first } " that does not perform bounds checks." } ;
225
226 HELP: first2-unsafe
227 { $values { "seq" sequence } { "first" "the first element" } { "second" "the second element" } }
228 { $contract "Unsafe variant of " { $link first2 } " that does not perform bounds checks." } ;
229
230 HELP: first3-unsafe
231 { $values { "seq" sequence } { "first" "the first element" } { "second" "the second element" } { "third" "the third element" } }
232 { $contract "Unsafe variant of " { $link first3 } " that does not perform bounds checks." } ;
233
234 HELP: first4-unsafe
235 { $values { "seq" sequence } { "first" "the first element" } { "second" "the second element" } { "third" "the third element" } { "fourth" "the fourth element" } }
236 { $contract "Unsafe variant of " { $link first4 } " that does not perform bounds checks." } ;
237
238 HELP: 1sequence
239 { $values { "obj" object } { "exemplar" sequence } { "seq" sequence } }
240 { $description "Creates a one-element sequence of the same type as " { $snippet "exemplar" } "." } ;
241
242 HELP: 2sequence
243 { $values { "obj1" object } { "obj2" object } { "exemplar" sequence } { "seq" sequence } }
244 { $description "Creates a two-element sequence of the same type as " { $snippet "exemplar" } "." } ;
245
246 HELP: 3sequence
247 { $values { "obj1" object } { "obj2" object } { "obj3" object } { "exemplar" sequence } { "seq" sequence } }
248 { $description "Creates a three-element sequence of the same type as " { $snippet "exemplar" } "." } ;
249
250 HELP: 4sequence
251 { $values { "obj1" object } { "obj2" object } { "obj3" object } { "obj4" object } { "exemplar" sequence } { "seq" sequence } }
252 { $description "Creates a four-element sequence of the same type as " { $snippet "exemplar" } "." } ;
253
254 HELP: first2
255 { $values { "seq" sequence } { "first" "the first element" } { "second" "the second element" } }
256 { $description "Pushes the first two elements of a sequence." }
257 { $errors "Throws an error if the sequence has less than two elements." } ;
258
259 HELP: first3
260 { $values { "seq" sequence } { "first" "the first element" } { "second" "the second element" } { "third" "the third element" } }
261 { $description "Pushes the first three elements of a sequence." }
262 { $errors "Throws an error if the sequence has less than three elements." } ;
263
264 HELP: first4
265 { $values { "seq" sequence } { "first" "the first element" } { "second" "the second element" } { "third" "the third element" } { "fourth" "the fourth element" } }
266 { $description "Pushes the first four elements of a sequence." }
267 { $errors "Throws an error if the sequence has less than four elements." } ;
268
269 HELP: array-capacity
270 { $class-description "A predicate class whose instances are fixnums of valid array sizes for the current architecture. The minimum value is zero and the maximum value is " { $link max-array-capacity } "." }
271 { $description "Low-level array length accessor." }
272 { $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." }
273 { $see-also integer-array-capacity } ;
274
275 HELP: integer-array-capacity
276 { $class-description "A predicate class whose instances are integer of valid array sizes for the current architecture. The minimum value is zero and the maximum value is " { $link max-array-capacity } "." }
277 { $description "Low-level array length accessor." }
278 { $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." }
279 { $see-also array-capacity } ;
280
281 HELP: array-nth
282 { $values { "n" "a non-negative fixnum" } { "array" array } { "elt" object } }
283 { $description "Low-level array element accessor." }
284 { $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." } ;
285
286 HELP: set-array-nth
287 { $values { "elt" object } { "n" "a non-negative fixnum" } { "array" array } }
288 { $description "Low-level array element mutator." }
289 { $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." } ;
290
291 HELP: collect
292 { $values { "n" "a non-negative integer" } { "quot" { $quotation ( ... n -- ... value ) } } { "into" "a sequence of length at least " { $snippet "n" } } }
293 { $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." }
294 { $examples
295   { $example
296     "USING: kernel math.parser prettyprint sequences sequences.private ;"
297     "10 [ number>string ] 10 f new-sequence [ collect ] keep ."
298     "{ \"0\" \"1\" \"2\" \"3\" \"4\" \"5\" \"6\" \"7\" \"8\" \"9\" }"
299   }
300 } ;
301
302 HELP: each
303 { $values { "seq" sequence } { "quot" { $quotation ( ... x -- ... ) } } }
304 { $description "Applies the quotation to each element of the sequence in order." } ;
305
306 HELP: reduce
307 { $values { "seq" sequence } { "identity" object } { "quot" { $quotation ( ... prev elt -- ... next ) } } { "result" "the final result" } }
308 { $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." }
309 { $examples
310     { $example "USING: math prettyprint sequences ;" "{ 1 5 3 } 0 [ + ] reduce ." "9" }
311 } ;
312
313 HELP: reduce-index
314 { $values
315      { "seq" sequence } { "identity" object } { "quot" { $quotation ( ... prev elt index -- ... next ) } } { "result" object } }
316 { $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." }
317 { $examples { $example "USING: sequences prettyprint math ;"
318     "{ 10 50 90 } 0 [ + + ] reduce-index ."
319     "153"
320 } } ;
321
322 HELP: accumulate-as
323 { $values { "seq" sequence } { "identity" object } { "quot" { $quotation ( ... prev elt -- ... next ) } } { "exemplar" sequence } { "final" "the final result" } { "newseq" "a new sequence" } }
324 { $description "Combines successive elements of the sequence using a binary operation, and outputs a sequence of the same type as " { $snippet "exemplar" } " containing intermediate results, together with the final result."
325 $nl
326 "The first element of the output sequence is " { $snippet "identity" } ". Then, on the first iteration, the two inputs to the quotation are " { $snippet "identity" } " and the first element of the input sequence. On successive iterations, the first input is the result of the previous iteration, and the second input is the next element of the input sequence."
327 $nl
328 "When given the empty sequence, outputs a new empty sequence together with the " { $snippet "identity" } "." }
329 { $notes "May be named " { $snippet "scan" } " or " { $snippet "prefix sum" } " in other languages." } ;
330
331 HELP: accumulate
332 { $values { "seq" sequence } { "identity" object } { "quot" { $quotation ( ... prev elt -- ... next ) } } { "final" "the final result" } { "newseq" "a new sequence" } }
333 { $description "Combines successive elements of the sequence using a binary operation, and outputs a sequence of intermediate results, together with the final result."
334 $nl
335 "The first element of the output sequence is " { $snippet "identity" } ". Then, on the first iteration, the two inputs to the quotation are " { $snippet "identity" } " and the first element of the input sequence. On successive iterations, the first input is the result of the previous iteration, and the second input is the next element of the input sequence."
336 $nl
337 "When given the empty sequence, outputs a new empty sequence together with the " { $snippet "identity" } "." }
338 { $examples
339     { $example "USING: math prettyprint sequences ;" "{ 2 2 2 2 2 } 0 [ + ] accumulate . ." "{ 0 2 4 6 8 }\n10" }
340 }
341 { $notes "May be named " { $snippet "scan" } " or " { $snippet "prefix sum" } " in other languages." } ;
342
343 HELP: accumulate!
344 { $values { "seq" "a mutable sequence" } { "identity" object } { "quot" { $quotation ( ... prev elt -- ... next ) } } { "final" "the final result" } }
345 { $description "Combines successive elements of the sequence using a binary operation, and outputs the original sequence of intermediate results, together with the final result."
346 $nl
347 "The first element of the new sequence is " { $snippet "identity" } ". Then, on the first iteration, the two inputs to the quotation are " { $snippet "identity" } ", and the first element of the old sequence. On successive iterations, the first input is the result of the previous iteration, and the second input is the corresponding element of the old sequence."
348 $nl
349 "When given the empty sequence, outputs the same empty sequence together with the " { $snippet "identity" } "." }
350 { $errors "Throws an error if the sequence is immutable, or the sequence cannot hold elements of the type output by " { $snippet "quot" } "." }
351 { $side-effects "seq" }
352 { $examples
353     { $example "USING: math prettyprint sequences ;" "{ 2 2 2 2 2 } 0 [ + ] accumulate! . ." "{ 0 2 4 6 8 }\n10" }
354 }
355 { $notes "May be named " { $snippet "scan" } " or " { $snippet "prefix sum" } " in other languages." } ;
356
357 HELP: accumulate*-as
358 { $values { "seq" sequence } { "identity" object } { "quot" { $quotation ( ... prev elt -- ... next ) } } { "exemplar" sequence } { "newseq" "a new sequence" } }
359 { $description "Combines successive elements of the sequence using a binary operation, and outputs a sequence of the same type as " { $snippet "exemplar" } " containing all results."
360 $nl
361 "On the first iteration, the two inputs to the quotation are " { $snippet "identity" } " and the first element of the input sequence. On successive iterations, the first input is the result of the previous iteration, and the second input is the next element of the input sequence."
362 $nl
363 "When given the empty sequence, outputs a new empty sequence" }
364 { $notes "May be named " { $snippet "scan" } " or " { $snippet "prefix sum" } " in other languages." } ;
365
366 HELP: accumulate*
367 { $values { "seq" sequence } { "identity" object } { "quot" { $quotation ( ... prev elt -- ... next ) } } { "newseq" sequence } }
368 { $description "Combines successive elements of the sequence using a binary operation, and outputs a sequence of all results."
369 $nl
370 "On the first iteration, the two inputs to the quotation are " { $snippet "identity" } " and the first element of the input sequence. On successive iterations, the first input is the result of the previous iteration, and the second input is the next element of the input sequence."
371 $nl
372 "When given the empty sequence, outputs a new empty sequence." }
373 { $examples
374     { $example "USING: math prettyprint sequences ;" "{ 2 2 2 2 2 } 0 [ + ] accumulate* ." "{ 2 4 6 8 10 }" }
375 }
376 { $notes "May be named " { $snippet "scan" } " or " { $snippet "prefix sum" } " in other languages." } ;
377
378 HELP: accumulate*!
379 { $values { "seq" sequence } { "identity" object } { "quot" { $quotation ( ... prev elt -- ... next ) } } }
380 { $description "Combines successive elements of the sequence using a binary operation, and outputs the original sequence of all results."
381 $nl
382 "On the first iteration, the two inputs to the quotation are " { $snippet "identity" } " and the first element of the input sequence. On successive iterations, the first input is the result of the previous iteration, and the second input is the next element of the input sequence."
383 $nl
384 "When given the empty sequence, outputs the same empty sequence." }
385 { $errors "Throws an error if the sequence is immutable, or the sequence cannot hold elements of the type output by " { $snippet "quot" } "." }
386 { $side-effects "seq" }
387 { $examples
388     { $example "USING: math prettyprint sequences ;" "{ 2 2 2 2 2 } 0 [ + ] accumulate*! ." "{ 2 4 6 8 10 }" }
389 }
390 { $notes "May be named " { $snippet "scan" } " or " { $snippet "prefix sum" } " in other languages." } ;
391
392 { accumulate accumulate! accumulate-as accumulate* accumulate*! accumulate*-as } related-words
393
394 HELP: map
395 { $values { "seq" sequence } { "quot" { $quotation ( ... elt -- ... newelt ) } } { "newseq" "a new sequence" } }
396 { $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." } ;
397
398 HELP: map-as
399 { $values { "seq" sequence } { "quot" { $quotation ( ... elt -- ... newelt ) } } { "exemplar" sequence } { "newseq" "a new sequence" } }
400 { $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" } "." }
401 { $examples
402     "The following example converts a string into an array of one-element strings:"
403     { $example "USING: prettyprint strings sequences ;" "\"Hello\" [ 1string ] { } map-as ." "{ \"H\" \"e\" \"l\" \"l\" \"o\" }" }
404     "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."
405 } ;
406
407 HELP: each-index
408 { $values
409      { "seq" sequence } { "quot" { $quotation ( ... elt index -- ... ) } } }
410 { $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." }
411 { $examples { $example "USING: arrays sequences prettyprint ;"
412 "{ 10 20 30 } [ 2array . ] each-index"
413 "{ 10 0 }\n{ 20 1 }\n{ 30 2 }"
414 } } ;
415
416 HELP: map-index
417 { $values
418   { "seq" sequence } { "quot" { $quotation ( ... elt index -- ... newelt ) } } { "newseq" sequence } }
419 { $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." }
420 { $examples
421     { $example "USING: arrays sequences prettyprint ;"
422         "{ 10 20 30 } [ 2array ] map-index ."
423         "{ { 10 0 } { 20 1 } { 30 2 } }"
424     }
425 } ;
426
427 HELP: map-index-as
428 { $values
429   { "seq" sequence } { "quot" { $quotation ( ... elt index -- ... newelt ) } } { "exemplar" sequence } { "newseq" sequence } }
430 { $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 " { $snippet "exemplar" } " sequence." }
431 { $examples
432     { $example "USING: arrays sequences prettyprint ;"
433         "{ 10 20 30 } [ 2array ] V{ } map-index-as ."
434         "V{ { 10 0 } { 20 1 } { 30 2 } }"
435     }
436 } ;
437
438 { map map! map-as map-index map-index-as } related-words
439
440 HELP: change-nth
441 { $values { "i" "a non-negative integer" } { "seq" "a mutable sequence" } { "quot" { $quotation ( ..a elt -- ..b newelt ) } } }
442 { $description "Applies the quotation to the " { $snippet "i" } "th element of the sequence, storing the result back into the sequence." }
443 { $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" } "." }
444 { $side-effects "seq" } ;
445
446 HELP: map!
447 { $values { "seq" "a mutable sequence" } { "quot" { $quotation ( ... elt -- ... newelt ) } } }
448 { $description "Applies the quotation to each element yielding a new element, storing the new elements back in the original sequence. Returns the original sequence." }
449 { $errors "Throws an error if the sequence is immutable, or the sequence cannot hold elements of the type output by " { $snippet "quot" } "." }
450 { $side-effects "seq" } ;
451
452 HELP: min-length
453 { $values { "seq1" sequence } { "seq2" sequence } { "n" "a non-negative integer" } }
454 { $description "Outputs the minimum of the lengths of the two sequences." } ;
455
456 HELP: max-length
457 { $values { "seq1" sequence } { "seq2" sequence } { "n" "a non-negative integer" } }
458 { $description "Outputs the maximum of the lengths of the two sequences." } ;
459
460 HELP: 2each
461 { $values { "seq1" sequence } { "seq2" sequence } { "quot" { $quotation ( ... elt1 elt2 -- ... ) } } }
462 { $description "Applies the quotation to pairs of elements from " { $snippet "seq1" } " and " { $snippet "seq2" } "." } ;
463
464 HELP: 3each
465 { $values { "seq1" sequence } { "seq2" sequence } { "seq3" sequence } { "quot" { $quotation ( ... elt1 elt2 elt3 -- ... ) } } }
466 { $description "Applies the quotation to triples of elements from " { $snippet "seq1" } ", " { $snippet "seq2" } " and " { $snippet "seq3" } "." } ;
467
468 HELP: 2reduce
469 { $values { "seq1" sequence }
470           { "seq2" sequence }
471           { "identity" object }
472           { "quot" { $quotation ( ... prev elt1 elt2 -- ... next ) } }
473           { "result" "the final result" } }
474 { $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" } "." } ;
475
476 HELP: 2map
477 { $values { "seq1" sequence } { "seq2" sequence } { "quot" { $quotation ( ... elt1 elt2 -- ... newelt ) } } { "newseq" "a new sequence" } }
478 { $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" } "." } ;
479
480 HELP: 3map
481 { $values { "seq1" sequence } { "seq2" sequence } { "seq3" sequence } { "quot" { $quotation ( ... elt1 elt2 elt3 -- ... newelt ) } } { "newseq" "a new sequence" } }
482 { $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" } "." } ;
483
484 HELP: 2map-as
485 { $values { "seq1" sequence } { "seq2" sequence } { "quot" { $quotation ( ... elt1 elt2 -- ... newelt ) } } { "exemplar" sequence } { "newseq" "a new sequence" } }
486 { $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" } "." } ;
487
488 HELP: 3map-as
489 { $values { "seq1" sequence } { "seq2" sequence } { "seq3" sequence } { "quot" { $quotation ( ... elt1 elt2 elt3 -- ... newelt ) } } { "exemplar" sequence } { "newseq" "a new sequence" } }
490 { $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" } "." } ;
491
492 HELP: 2all?
493 { $values { "seq1" sequence } { "seq2" sequence } { "quot" { $quotation ( ... elt1 elt2 -- ... ? ) } } { "?" boolean } }
494 { $description "Tests the predicate pairwise against elements of " { $snippet "seq1" } " and " { $snippet "seq2" } ". If the sequences have different lengths, then only the smallest sequences items are compared with the other." }
495 { $examples
496   { $example
497     "USING: math prettyprint sequences ;"
498     "{ 1 2 3 4 } { 2 4 6 8 } [ <= ] 2all? ."
499     "t"
500   }
501 } ;
502
503 HELP: find
504 { $values { "seq" sequence }
505           { "quot" { $quotation ( ... elt -- ... ? ) } }
506           { "i" "the index of the first match, or " { $link f } }
507           { "elt" "the first matching element, or " { $link f } } }
508 { $description "A simpler variant of " { $link find-from } " where the starting index is 0." } ;
509
510 HELP: find-from
511 { $values { "n" "a starting index" }
512           { "seq" sequence }
513           { "quot" { $quotation ( ... elt -- ... ? ) } }
514           { "i" { $maybe "the index of the first match" } }
515           { "elt" { $maybe "the first matching element" } } }
516 { $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 " { $link f } " and " { $link f } " as the element." } ;
517
518 HELP: find-last
519 { $values { "seq" sequence } { "quot" { $quotation ( ... elt -- ... ? ) } } { "i" { $maybe "the index of the first match" } } { "elt" { $maybe "the first matching element" } } }
520 { $description "A simpler variant of " { $link find-last-from } " where the starting index is one less than the length of the sequence." } ;
521
522 HELP: find-last-from
523 { $values { "n" "a starting index" } { "seq" sequence } { "quot" { $quotation ( ... elt -- ... ? ) } } { "i" { $maybe "the index of the first match" } } { "elt" { $maybe "the first matching element" } } }
524 { $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 " { $link f } " and " { $link f } " as the element." } ;
525
526 HELP: find-index
527 { $values { "seq" sequence }
528           { "quot" { $quotation ( ... elt i -- ... ? ) } }
529           { "i" { $maybe "the index of the first match" } }
530           { "elt" { $maybe "the first matching element" } } }
531 { $description "A variant of " { $link find } " where the quotation takes both an element and its index." } ;
532
533 HELP: find-index-from
534 { $values { "n" "a starting index" }
535           { "seq" sequence }
536           { "quot" { $quotation ( ... elt i -- ... ? ) } }
537           { "i" { $maybe "the index of the first match" } }
538           { "elt" { $maybe "the first matching element" } } }
539 { $description "A variant of " { $link find-from } " where the quotation takes both an element and its index." } ;
540
541 HELP: map-find
542 { $values { "seq" sequence } { "quot" { $quotation ( ... elt -- ... result/f ) } } { "result" "the first non-false result of the quotation" } { "elt" { $maybe "the first matching element" } } }
543 { $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." } ;
544
545 HELP: any?
546 { $values { "seq" sequence } { "quot" { $quotation ( ... elt -- ... ? ) } } { "?" boolean } }
547 { $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 } "." } ;
548
549 HELP: none?
550 { $values { "seq" sequence } { "quot" { $quotation ( ... elt -- ... ? ) } } { "?" boolean } }
551 { $description "Tests if the sequence does not contain any 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 t } "." } ;
552
553 HELP: all?
554 { $values { "seq" sequence } { "quot" { $quotation ( ... elt -- ... ? ) } } { "?" boolean } }
555 { $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 } "." } ;
556
557 HELP: push-if
558 { $values { "elt" object } { "quot" { $quotation ( ..a elt -- ..b ? ) } } { "accum" "a resizable mutable sequence" } }
559 { $description "Adds the element at the end of the sequence if the quotation yields a true value." }
560 { $notes "This word is a factor of " { $link filter } "." } ;
561
562 HELP: filter
563 { $values { "seq" sequence } { "quot" { $quotation ( ... elt -- ... ? ) } } { "subseq" "a new sequence" } }
564 { $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." } ;
565
566 HELP: filter-as
567 { $values { "seq" sequence } { "quot" { $quotation ( ... elt -- ... ? ) } } { "exemplar" sequence } { "subseq" "a new sequence" } }
568 { $description "Applies the quotation to each element in turn, and outputs a new sequence of the same type as " { $snippet "exemplar" } " containing the elements of the original sequence for which the quotation output a true value." } ;
569
570 HELP: filter!
571 { $values { "seq" "a resizable mutable sequence" } { "quot" { $quotation ( ... elt -- ... ? ) } } }
572 { $description "Applies the quotation to each element in turn, and removes elements for which the quotation outputs a false value." }
573 { $side-effects "seq" } ;
574
575 HELP: reject
576 { $values { "seq" sequence } { "quot" { $quotation ( ... elt -- ... ? ) } } { "subseq" "a new sequence" } }
577 { $description "Applies the quotation to each element in turn, and outputs a new sequence removing the elements of the original sequence for which the quotation outputs a true value." } ;
578
579 HELP: reject-as
580 { $values { "seq" sequence } { "quot" { $quotation ( ... elt -- ... ? ) } } { "exemplar" sequence } { "subseq" "a new sequence" } }
581 { $description "Applies the quotation to each element in turn, and outputs a new sequence of the same type as " { $snippet "exemplar" } " remove the elements of the original sequence for which the quotation output a true value." } ;
582
583 HELP: reject!
584 { $values { "seq" "a resizable mutable sequence" } { "quot" { $quotation ( ... elt -- ... ? ) } } }
585 { $description "Applies the quotation to each element in turn, and removes elements for which the quotation outputs a true value." }
586 { $side-effects "seq" } ;
587
588 HELP: interleave
589 { $values { "seq" sequence } { "between" quotation } { "quot" { $quotation ( ... elt -- ... ) } } }
590 { $description "Applies " { $snippet "quot" } " to each element in turn, also invoking " { $snippet "between" } " in-between each pair of elements." }
591 { $examples { $example "USING: io sequences ;" "{ \"a\" \"b\" \"c\" } [ \"X\" write ] [ write ] interleave" "aXbXc" } } ;
592
593 HELP: index
594 { $values { "obj" object } { "seq" sequence } { "n" "an index" } }
595 { $description "Outputs the index of the first element in the sequence equal to " { $snippet "obj" } ". If no element is found, outputs " { $link f } "." } ;
596
597 HELP: index-from
598 { $values { "obj" object } { "i" "a start index" } { "seq" sequence } { "n" "an index" } }
599 { $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 } "." } ;
600
601 HELP: last-index
602 { $values { "obj" object } { "seq" sequence } { "n" "an index" } }
603 { $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 } "." } ;
604
605 HELP: last-index-from
606 { $values { "obj" object } { "i" "a start index" } { "seq" sequence } { "n" "an index" } }
607 { $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 } "." } ;
608
609 HELP: member?
610 { $values { "elt" object } { "seq" sequence } { "?" boolean } }
611 { $description "Tests if the sequence contains an element equal to the object." }
612 { $examples
613     "Is a letter in a string:"
614     { $example
615         "USING: sequences prettyprint ;"
616         "CHAR: a \"abc\" member? ."
617         "t"
618     } $nl
619     "Is a number in a sequence:"
620     { $example
621         "USING: sequences prettyprint ;"
622         "4 { 1 2 3 } member? ."
623         "f"
624     }
625 }
626 { $notes "This word uses equality comparison (" { $link = } ")." } ;
627
628 HELP: member-eq?
629 { $values { "elt" object } { "seq" sequence } { "?" boolean } }
630 { $description "Tests if the sequence contains the object." }
631 { $notes "This word uses identity comparison (" { $link eq? } ")." } ;
632
633 HELP: remove
634 { $values { "elt" object } { "seq" sequence } { "newseq" "a new sequence" } }
635 { $description "Outputs a new sequence containing all elements of the input sequence except for the given element." }
636 { $notes "This word uses equality comparison (" { $link = } ")." } ;
637
638 HELP: remove-eq
639 { $values { "elt" object } { "seq" sequence } { "newseq" "a new sequence" } }
640 { $description "Outputs a new sequence containing all elements of the input sequence except those equal to the given element." }
641 { $notes "This word uses identity comparison (" { $link eq? } ")." } ;
642
643 HELP: remove-nth
644 { $values
645      { "n" integer } { "seq" sequence }
646      { "seq'" sequence } }
647 { $description "Creates a new sequence without the element at index " { $snippet "n" } "." }
648 { $examples "Notice that the original sequence is left intact:" { $example "USING: sequences prettyprint kernel ;"
649     "{ 1 2 3 } 1 over remove-nth . ."
650     "{ 1 3 }\n{ 1 2 3 }"
651 } } ;
652
653 HELP: move
654 { $values { "to" "an index in " { $snippet "seq" } } { "from" "an index in " { $snippet "seq" } } { "seq" "a mutable sequence" } }
655 { $description "Sets the element with index " { $snippet "m" } " to the element with index " { $snippet "n" } "." }
656 { $side-effects "seq" } ;
657
658 HELP: remove!
659 { $values { "elt" object } { "seq" "a resizable mutable sequence" } }
660 { $description "Removes all elements equal to " { $snippet "elt" } " from " { $snippet "seq" } " and returns " { $snippet "seq" } "." }
661 { $notes "This word uses equality comparison (" { $link = } ")." }
662 { $side-effects "seq" } ;
663
664 HELP: remove-eq!
665 { $values { "elt" object } { "seq" "a resizable mutable sequence" } }
666 { $description "Outputs a new sequence containing all elements of the input sequence except the given element." }
667 { $notes "This word uses identity comparison (" { $link eq? } ")." }
668 { $side-effects "seq" } ;
669
670 HELP: remove-nth!
671 { $values { "n" "a non-negative integer" } { "seq" "a resizable mutable sequence" } }
672 { $description "Removes the " { $snippet "n" } "th element from the sequence, shifting all other elements down and reducing its length by one." }
673 { $side-effects "seq" } ;
674
675 HELP: delete-slice
676 { $values { "from" "a non-negative integer" } { "to" "a non-negative integer" } { "seq" "a resizable mutable sequence" } }
677 { $description "Removes a range of elements beginning at index " { $snippet "from" } " and ending before index " { $snippet "to" } "." }
678 { $side-effects "seq" } ;
679
680 HELP: replace-slice
681 { $values { "new" sequence } { "from" "a non-negative integer" } { "to" "a non-negative integer" } { "seq" sequence } { "seq'" sequence } }
682 { $description "Replaces a range of elements beginning at index " { $snippet "from" } " and ending before index " { $snippet "to" } " with a new sequence." }
683 { $errors "Throws an error if " { $snippet "new" } " contains elements whose types are not permissible in " { $snippet "seq" } "." } ;
684
685 { push push-either push-if pop pop* prefix suffix suffix! } related-words
686
687 HELP: suffix
688 { $values { "seq" sequence } { "elt" object } { "newseq" sequence } }
689 { $description "Outputs a new sequence obtained by adding " { $snippet "elt" } " at the end of " { $snippet "seq" } "." }
690 { $errors "Throws an error if the type of " { $snippet "elt" } " is not permitted in sequences of the same class as " { $snippet "seq1" } "." }
691 { $examples
692     { $example "USING: prettyprint sequences ;" "{ 1 2 3 } 4 suffix ." "{ 1 2 3 4 }" }
693 } ;
694
695 HELP: suffix!
696 { $values { "seq" sequence } { "elt" object } }
697 { $description "Modifiers a sequence in-place by adding " { $snippet "elt" } " to the end of " { $snippet "seq" } ". Outputs " { $snippet "seq" } "." }
698 { $errors "Throws an error if the type of " { $snippet "elt" } " is not permitted in sequences of the same class as " { $snippet "seq" } "." }
699 { $examples
700     { $example "USING: prettyprint sequences ;" "V{ 1 2 3 } 4 suffix! ." "V{ 1 2 3 4 }" }
701 } ;
702
703 HELP: append!
704 { $values { "seq1" sequence } { "seq2" sequence } }
705 { $description "Modifiers " { $snippet "seq1" } " in-place by adding the elements from " { $snippet "seq2" } " to the end and outputs " { $snippet "seq1" } "." }
706 { $examples
707     { $example "USING: prettyprint sequences ;" "V{ 1 2 3 } { 4 5 6 } append! ." "V{ 1 2 3 4 5 6 }" }
708 } ;
709
710 HELP: prefix
711 { $values { "seq" sequence } { "elt" object } { "newseq" sequence } }
712 { $description "Outputs a new sequence obtained by adding " { $snippet "elt" } " at the beginning of " { $snippet "seq" } "." }
713 { $errors "Throws an error if the type of " { $snippet "elt" } " is not permitted in sequences of the same class as " { $snippet "seq1" } "." }
714 { $examples
715 { $example "USING: prettyprint sequences ;" "{ 1 2 3 } 0 prefix ." "{ 0 1 2 3 }" }
716 } ;
717
718 HELP: sum-lengths
719 { $values { "seq" { $sequence sequence } } { "n" integer } }
720 { $description "Outputs the sum of the lengths of all sequences in " { $snippet "seq" } "." } ;
721
722 HELP: concat
723 { $values { "seq" sequence } { "newseq" sequence } }
724 { $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" } "." }
725 { $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" } "." } ;
726
727 HELP: concat-as
728 { $values { "seq" sequence } { "exemplar" sequence } { "newseq" sequence } }
729 { $description "Concatenates a sequence of sequences together into one sequence with the same type as " { $snippet "exemplar" } "." }
730 { $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" } "." } ;
731
732 HELP: join
733 { $values { "seq" sequence } { "glue" sequence } { "newseq" sequence } }
734 { $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" } "." }
735 { $examples
736     "Join a list of strings:"
737     { $example "USING: sequences prettyprint ;"
738         "{ \"cat\" \"dog\" \"ant\" } \" \" join ."
739         "\"cat dog ant\""
740     }
741 }
742 { $notes "If the " { $snippet "glue" } " sequence is empty, this word calls " { $link concat-as } "." }
743 { $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" } "." } ;
744
745 HELP: join-as
746 { $values { "seq" sequence } { "glue" sequence } { "exemplar" sequence } { "newseq" sequence } }
747 { $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" } "." }
748 { $notes "If the " { $snippet "glue" } " sequence is empty, this word calls " { $link concat-as } "." }
749 { $examples
750     "Join a list of strings as a string buffer:"
751     { $example "USING: sequences prettyprint ;"
752         "{ \"a\" \"b\" \"c\" } \"1\" SBUF\" \"join-as ."
753         "SBUF\" a1b1c\""
754     }
755 }
756 { $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" } "." } ;
757
758 { join join-as concat concat-as } related-words
759
760 HELP: last
761 { $values { "seq" sequence } { "elt" object } }
762 { $description "Outputs the last element of a sequence." }
763 { $errors "Throws an error if the sequence is empty." } ;
764
765 HELP: pop*
766 { $values { "seq" "a resizable mutable sequence" } }
767 { $description "Removes the last element and shortens the sequence." }
768 { $side-effects "seq" }
769 { $errors "Throws an error if the sequence is empty." } ;
770
771 HELP: pop
772 { $values { "seq" "a resizable mutable sequence" } { "elt" object } }
773 { $description "Outputs the last element after removing it and shortening the sequence." }
774 { $side-effects "seq" }
775 { $errors "Throws an error if the sequence is empty." } ;
776
777 HELP: mismatch
778 { $values { "seq1" sequence } { "seq2" sequence } { "i" "an index" } }
779 { $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." } ;
780
781 HELP: flip
782 { $values { "matrix" "a sequence of equal-length sequences" } { "newmatrix" "a sequence of equal-length sequences" } }
783 { $description "Transposes the matrix; that is, rows become columns and columns become rows." }
784 { $examples { $example "USING: prettyprint sequences ;" "{ { 1 2 3 } { 4 5 6 } } flip ." "{ { 1 4 } { 2 5 } { 3 6 } }" } } ;
785
786 HELP: exchange
787 { $values { "m" "a non-negative integer" } { "n" "a non-negative integer" } { "seq" "a mutable sequence" } }
788 { $description "Exchanges the " { $snippet "m" } "th and " { $snippet "n" } "th elements of " { $snippet "seq" } "." } ;
789
790 HELP: reverse!
791 { $values { "seq" "a mutable sequence" } }
792 { $description "Reverses a sequence in-place and outputs that sequence." }
793 { $side-effects "seq" } ;
794
795 HELP: padding
796 { $values { "seq" sequence } { "n" "a non-negative integer" } { "elt" object } { "quot" { $quotation ( ... seq1 seq2 -- ... newseq ) } } { "newseq" "a new sequence" } }
797 { $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." } ;
798
799 HELP: pad-head
800 { $values { "seq" sequence } { "n" "a non-negative integer" } { "elt" object } { "padded" "a new sequence" } }
801 { $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" } "." }
802 { $examples { $example "USING: io sequences ;" "{ \"ab\" \"quux\" } [ 5 CHAR: - pad-head print ] each" "---ab\n-quux" } } ;
803
804 HELP: pad-tail
805 { $values { "seq" sequence } { "n" "a non-negative integer" } { "elt" object } { "padded" "a new sequence" } }
806 { $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" } "." }
807 { $examples { $example "USING: io sequences ;" "{ \"ab\" \"quux\" } [ 5 CHAR: - pad-tail print ] each" "ab---\nquux-" } } ;
808
809 HELP: sequence=
810 { $values { "seq1" sequence } { "seq2" sequence } { "?" boolean } }
811 { $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." } ;
812
813 HELP: reversed
814 { $class-description "A virtual sequence which presents a reversed view of an underlying sequence. New instances can be created by calling " { $link <reversed> } "." } ;
815
816 HELP: reverse
817 { $values { "seq" sequence } { "newseq" "a new sequence" } }
818 { $description "Outputs a new sequence having the same elements as " { $snippet "seq" } " but in reverse order." } ;
819
820 { reverse <reversed> reverse! } related-words
821
822 HELP: <reversed>
823 { $values { "seq" sequence } { "reversed" "a new sequence" } }
824 { $description "Creates an instance of the " { $link reversed } " class." }
825 { $see-also "virtual-sequences" } ;
826
827 HELP: slice-error
828 { $values { "str" "a reason" } }
829 { $description "Throws a " { $link slice-error } "." }
830 { $error-description "Thrown by " { $link <slice> } " if one of the following invalid conditions holds:"
831     { $list
832         "The start index is negative"
833         "The end index is greater than the length of the sequence"
834         "The start index is greater than the end index"
835     }
836 } ;
837
838 HELP: slice
839 { $class-description "A virtual sequence which presents a subrange of the elements of an underlying sequence. New instances can be created by calling " { $link <slice> } ". Convenience words are also provided for creating slices where one endpoint is the start or end of the sequence; see " { $link "sequences-slices" } " for a list."
840 $nl
841 "Slices are mutable if the underlying sequence is mutable, and mutating a slice changes the underlying sequence. However, slices cannot be resized after creation." } ;
842
843 HELP: check-slice
844 { $values { "from" "a non-negative integer" } { "to" "a non-negative integer" } { "seq" sequence } }
845 { $description "Ensures that " { $snippet "m" } " is less than or equal to " { $snippet "m" } ", and that both indices are within bounds for " { $snippet "seq" } "." }
846 { $errors "Throws a " { $link slice-error } " if the preconditions are not met." } ;
847
848 HELP: collapse-slice
849 { $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 } }
850 { $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." }
851 ;
852
853 HELP: <slice>
854 { $values { "from" "a non-negative integer" } { "to" "a non-negative integer" } { "seq" sequence } { "slice" slice } }
855 { $description "Outputs a new virtual sequence sharing storage with the subrange of elements in " { $snippet "seq" } " with indices starting from and including " { $snippet "from" } ", and up to but not including " { $snippet "to" } "." }
856 { $errors "Throws an error if " { $snippet "from" } " or " { $snippet "to" } " are out of bounds." }
857 { $notes "Taking the slice of a slice outputs a slice of the underlying sequence, instead of a slice of a slice. This means that you cannot assume that the " { $snippet "from" } " and " { $snippet "to" } " slots of the resulting slice will be equal to the values you passed to " { $link <slice> } "." } ;
858
859 { <slice> subseq } related-words
860
861 HELP: repetition
862 { $class-description "A virtual sequence consisting of " { $snippet "elt" } " repeated " { $snippet "len" } " times. Repetitions are created by calling " { $link <repetition> } "." } ;
863
864 HELP: <repetition>
865 { $values { "len" "a non-negative integer" } { "elt" object } { "repetition" repetition } }
866 { $description "Creates a new " { $link repetition } "." }
867 { $examples
868     { $example "USING: arrays prettyprint sequences ;" "10 \"X\" <repetition> >array ." "{ \"X\" \"X\" \"X\" \"X\" \"X\" \"X\" \"X\" \"X\" \"X\" \"X\" }" }
869     { $example "USING: prettyprint sequences ;" "10 \"X\" <repetition> concat ." "\"XXXXXXXXXX\"" }
870 } ;
871 HELP: copy
872 { $values { "src" sequence } { "i" "an index in " { $snippet "dst" } } { "dst" "a mutable sequence" } }
873 { $description "Copies all elements of " { $snippet "src" } " to " { $snippet "dst" } ", with destination indices starting from " { $snippet "i" } ". Grows " { $snippet "dst" } " first if necessary." }
874 { $side-effects "dst" }
875 { $errors "An error is thrown if " { $snippet "dst" } " is not resizable, and not large enough to hold the copied elements." } ;
876
877 HELP: push-all
878 { $values { "src" sequence } { "dst" "a resizable mutable sequence" } }
879 { $description "Appends " { $snippet "src" } " to the end of " { $snippet "dst" } "." }
880 { $side-effects "dst" }
881 { $errors "Throws an error if " { $snippet "src" } " contains elements not permitted in " { $snippet "dst" } "." } ;
882
883 HELP: append
884 { $values { "seq1" sequence } { "seq2" sequence } { "newseq" sequence } }
885 { $description "Outputs a new sequence of the same type as " { $snippet "seq1" } " consisting of the elements of " { $snippet "seq1" } " followed by " { $snippet "seq2" } "." }
886 { $errors "Throws an error if " { $snippet "seq2" } " contains elements not permitted in sequences of the same class as " { $snippet "seq1" } "." }
887 { $examples
888     { $example "USING: prettyprint sequences ;"
889         "{ 1 2 } B{ 3 4 } append ."
890         "{ 1 2 3 4 }"
891     }
892     { $example "USING: prettyprint sequences strings ;"
893         "\"go\" \"ing\" append ."
894         "\"going\""
895     }
896 } ;
897
898 HELP: append-as
899 { $values { "seq1" sequence } { "seq2" sequence } { "exemplar" sequence } { "newseq" sequence } }
900 { $description "Outputs a new sequence of the same type as " { $snippet "exemplar" } " consisting of the elements of " { $snippet "seq1" } " followed by " { $snippet "seq2" } "." }
901 { $errors "Throws an error if " { $snippet "seq1" } " or " { $snippet "seq2" } " contain elements not permitted in sequences of the same class as " { $snippet "exemplar" } "." }
902 { $examples
903     { $example "USING: prettyprint sequences ;"
904         "{ 1 2 } B{ 3 4 } B{ } append-as ."
905         "B{ 1 2 3 4 }"
906     }
907     { $example "USING: prettyprint sequences strings ;"
908         "\"go\" \"ing\" SBUF\" \" append-as ."
909         "SBUF\" going\""
910     }
911 } ;
912
913 { append append-as append! 3append 3append-as push-all } related-words
914
915 HELP: prepend
916 { $values { "seq1" sequence } { "seq2" sequence } { "newseq" sequence } }
917 { $description "Outputs a new sequence of the same type as " { $snippet "seq1" } " consisting of the elements of " { $snippet "seq2" } " followed by " { $snippet "seq1" } "." }
918 { $errors "Throws an error if " { $snippet "seq2" } " contains elements not permitted in sequences of the same class as " { $snippet "seq1" } "." }
919 { $examples { $example "USING: prettyprint sequences ;"
920         "{ 1 2 } B{ 3 4 } prepend ."
921         "{ 3 4 1 2 }"
922     }
923     { $example "USING: prettyprint sequences strings ;"
924         "\"go\" \"car\" prepend ."
925         "\"cargo\""
926     }
927 } ;
928
929 HELP: prepend-as
930 { $values { "seq1" sequence } { "seq2" sequence } { "exemplar" sequence } { "newseq" sequence } }
931 { $description "Outputs a new sequence of the same type as " { $snippet "exemplar" } " consisting of the elements of " { $snippet "seq2" } " followed by " { $snippet "seq1" } "." }
932 { $errors "Throws an error if " { $snippet "seq1" } " or " { $snippet "seq2" } " contain elements not permitted in sequences of the same class as " { $snippet "exemplar" } "." }
933 { $examples
934     { $example "USING: prettyprint sequences ;"
935         "{ 3 4 } B{ 1 2 } B{ } prepend-as ."
936         "B{ 1 2 3 4 }"
937     }
938     { $example "USING: prettyprint sequences strings ;"
939         "\"ing\" \"go\" SBUF\" \" prepend-as ."
940         "SBUF\" going\""
941     }
942 } ;
943
944 { prepend prepend-as } related-words
945
946 HELP: 3append
947 { $values { "seq1" sequence } { "seq2" sequence } { "seq3" sequence } { "newseq" sequence } }
948 { $description "Outputs a new sequence consisting of the elements of " { $snippet "seq1" } ", " { $snippet "seq2" } " and " { $snippet "seq3" } " in turn." }
949 { $errors "Throws an error if " { $snippet "seq2" } " or " { $snippet "seq3" } " contain elements not permitted in sequences of the same class as " { $snippet "seq1" } "." }
950 { $examples
951     { $example "USING: prettyprint sequences ;"
952         "\"a\" \"b\" \"c\" 3append ."
953         "\"abc\""
954     }
955 } ;
956
957 HELP: 3append-as
958 { $values { "seq1" sequence } { "seq2" sequence } { "seq3" sequence } { "exemplar" sequence } { "newseq" sequence } }
959 { $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" } "." }
960 { $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" } "." }
961 { $examples
962     { $example "USING: prettyprint sequences ;"
963         "\"a\" \"b\" \"c\" SBUF\" \" 3append-as ."
964         "SBUF\" abc\""
965     }
966 } ;
967
968 HELP: surround
969 { $values { "seq1" sequence } { "seq2" sequence } { "seq3" sequence } { "newseq" sequence } }
970 { $description "Outputs a new sequence with " { $snippet "seq1" } " inserted between " { $snippet "seq2" } " and " { $snippet "seq3" } "." }
971 { $examples
972     { $example "USING: sequences prettyprint ;"
973                "\"sssssh\" \"(\" \")\" surround ."
974                "\"(sssssh)\""
975     }
976 } ;
977
978 HELP: glue
979 { $values { "seq1" sequence } { "seq2" sequence } { "seq3" sequence } { "newseq" sequence } }
980 { $description "Outputs a new sequence with " { $snippet "seq3" } " inserted between " { $snippet "seq1" } " and " { $snippet "seq2" } "." }
981 { $examples
982     { $example "USING: sequences prettyprint ;"
983                "\"a\" \"b\" \",\" glue ."
984                "\"a,b\""
985     }
986 } ;
987
988 HELP: subseq
989 { $values { "from" "a non-negative integer" } { "to" "a non-negative integer" } { "seq" sequence } { "subseq" "a new sequence" } }
990 { $description "Outputs a new sequence consisting of all elements starting from and including " { $snippet "from" } ", and up to but not including " { $snippet "to" } "." }
991 { $errors "Throws an error if " { $snippet "from" } " or " { $snippet "to" } " is out of bounds." } ;
992
993 HELP: subseq-as
994 { $values { "from" "a non-negative integer" } { "to" "a non-negative integer" } { "seq" sequence } { "exemplar" sequence } { "subseq" "a new sequence" } }
995 { $description "Outputs a new sequence consisting of all elements starting from and including " { $snippet "from" } ", and up to but not including " { $snippet "to" } " of type " { $snippet "exemplar" } "." }
996 { $errors "Throws an error if " { $snippet "from" } " or " { $snippet "to" } " is out of bounds." } ;
997
998 HELP: clone-like
999 { $values { "seq" sequence } { "exemplar" sequence } { "newseq" "a new sequence" } }
1000 { $description "Outputs a newly-allocated sequence with the same elements as " { $snippet "seq" } " but of the same type as " { $snippet "exemplar" } "." }
1001 { $notes "Unlike " { $link like } ", this word always creates a new sequence which never shares storage with the original." } ;
1002
1003 HELP: head-slice
1004 { $values { "seq" sequence } { "n" "a non-negative integer" } { "slice" "a slice" } }
1005 { $description "Outputs a virtual sequence sharing storage with the first " { $snippet "n" } " elements of the input sequence." }
1006 { $errors "Throws an error if the index is out of bounds." } ;
1007
1008 HELP: tail-slice
1009 { $values { "seq" sequence } { "n" "a non-negative integer" } { "slice" "a slice" } }
1010 { $description "Outputs a virtual sequence sharing storage with all elements from the " { $snippet "n" } "th index until the end of the input sequence." }
1011 { $errors "Throws an error if the index is out of bounds." } ;
1012
1013 HELP: but-last-slice
1014 { $values { "seq" sequence } { "slice" "a slice" } }
1015 { $description "Outputs a virtual sequence sharing storage with all but the last element of the input sequence." }
1016 { $errors "Throws an error on an empty sequence." } ;
1017
1018 HELP: rest-slice
1019 { $values { "seq" sequence } { "slice" "a slice" } }
1020 { $description "Outputs a virtual sequence sharing storage with all elements from the 1st index until the end of the input sequence." }
1021 { $notes "Equivalent to " { $snippet "1 tail" } }
1022 { $errors "Throws an error on an empty sequence." } ;
1023
1024 HELP: head-slice*
1025 { $values { "seq" sequence } { "n" "a non-negative integer" } { "slice" "a slice" } }
1026 { $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." }
1027 { $errors "Throws an error if the index is out of bounds." } ;
1028
1029 { head-slice head-slice* } related-words
1030
1031 HELP: tail-slice*
1032 { $values { "seq" sequence } { "n" "a non-negative integer" } { "slice" "a slice" } }
1033 { $description "Outputs a virtual sequence sharing storage with the last " { $snippet "n" } " elements of the input sequence." }
1034 { $errors "Throws an error if the index is out of bounds." } ;
1035
1036 { tail-slice tail-slice* } related-words
1037
1038 HELP: head
1039 { $values { "seq" sequence } { "n" "a non-negative integer" } { "headseq" "a new sequence" } }
1040 { $description "Outputs a new sequence consisting of the first " { $snippet "n" } " elements of the input sequence." }
1041 { $examples
1042     { $example "USING: sequences prettyprint ;"
1043         "{ 1 2 3 4 5 6 7 } 2 head ."
1044         "{ 1 2 }"
1045     }
1046     "When a sequence may not have enough elements:"
1047     { $example "USING: sequences prettyprint ;"
1048         "{ 1 2 } 5 short head ."
1049         "{ 1 2 }"
1050     }
1051 }
1052 { $errors "Throws an error if the index is out of bounds." } ;
1053
1054 HELP: tail
1055 { $values { "seq" sequence } { "n" "a non-negative integer" } { "tailseq" "a new sequence" } }
1056 { $description "Outputs a new sequence consisting of the input sequence with the first " { $snippet "n" } " items removed." }
1057 { $examples
1058     { $example "USING: sequences prettyprint ;"
1059         "{ 1 2 3 4 5 6 7 } 2 tail ."
1060         "{ 3 4 5 6 7 }"
1061     }
1062     "When a sequence may not have enough elements:"
1063     { $example "USING: sequences prettyprint ;"
1064         "{ 1 2 } 5 short tail ."
1065         "{ }"
1066     }
1067 }
1068 { $errors "Throws an error if the index is out of bounds." } ;
1069
1070 HELP: but-last
1071 { $values { "seq" sequence } { "headseq" "a new sequence" } }
1072 { $description "Outputs a new sequence consisting of the input sequence with the last item removed." }
1073 { $errors "Throws an error on an empty sequence." } ;
1074
1075 HELP: rest
1076 { $values { "seq" sequence } { "tailseq" "a new sequence" } }
1077 { $description "Outputs a new sequence consisting of the input sequence with the first item removed." }
1078 { $errors "Throws an error on an empty sequence." } ;
1079
1080 HELP: head*
1081 { $values { "seq" sequence } { "n" "a non-negative integer" } { "headseq" "a new sequence" } }
1082 { $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 removes the last " { $snippet "n" } " elements." }
1083 { $examples
1084     { $example "USING: sequences prettyprint ;"
1085         "{ 1 2 3 4 5 6 7 } 2 head* ."
1086         "{ 1 2 3 4 5 }"
1087     }
1088     "When a sequence may not have enough elements:"
1089     { $example "USING: sequences prettyprint ;"
1090         "{ 1 2 } 5 short head* ."
1091         "{ }"
1092     }
1093 }
1094 { $errors "Throws an error if the index is out of bounds." } ;
1095
1096 HELP: tail*
1097 { $values { "seq" sequence } { "n" "a non-negative integer" } { "tailseq" "a new sequence" } }
1098 { $description "Outputs a new sequence consisting of the last " { $snippet "n" } " elements of the input sequence." }
1099 { $examples
1100     { $example "USING: sequences prettyprint ;"
1101         "{ 1 2 3 4 5 6 7 } 2 tail* ."
1102         "{ 6 7 }"
1103     }
1104     "When a sequence may not have enough elements:"
1105     { $example "USING: sequences prettyprint ;"
1106         "{ 1 2 } 5 short tail* ."
1107         "{ 1 2 }"
1108     }
1109 }
1110 { $errors "Throws an error if the index is out of bounds." } ;
1111
1112 { tail tail* tail-slice tail-slice* } related-words
1113 { head head* head-slice head-slice* } related-words
1114 { cut cut* cut-slice } related-words
1115 { unclip unclip-slice unclip-last unclip-last-slice } related-words
1116 { first last but-last but-last-slice rest rest-slice } related-words
1117
1118 HELP: shorter?
1119 { $values { "seq1" sequence } { "seq2" sequence } { "?" boolean } }
1120 { $description "Tests if the length of " { $snippet "seq1" } " is smaller than the length of " { $snippet "seq2" } "." } ;
1121
1122 HELP: head?
1123 { $values { "seq" sequence } { "begin" sequence } { "?" boolean } }
1124 { $description "Tests if " { $snippet "seq" } " starts with " { $snippet "begin" } ". If " { $snippet "begin" } " is longer than " { $snippet "seq" } ", this word outputs " { $link f } "." }
1125 { $examples
1126   { $example
1127     "USING: prettyprint sequences ;"
1128     "{ \"accept\" \"adept\" \"advance\" \"advice\" \"affect\" } [ \"ad\" head? ] filter ."
1129     "{ \"adept\" \"advance\" \"advice\" }"
1130   }
1131 } ;
1132
1133 HELP: tail?
1134 { $values { "seq" sequence } { "end" sequence } { "?" boolean } }
1135 { $description "Tests if " { $snippet "seq" } " ends with " { $snippet "end" } ". If " { $snippet "end" } " is longer than " { $snippet "seq" } ", this word outputs " { $link f } "." } ;
1136
1137 { remove remove-nth remove-eq remove-eq! remove! remove-nth! } related-words
1138
1139 HELP: cut-slice
1140 { $values { "seq" sequence } { "n" "a non-negative integer" } { "before-slice" sequence } { "after-slice" "a slice" } }
1141 { $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." }
1142 { $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." } ;
1143
1144 HELP: cut
1145 { $values { "seq" sequence } { "n" "a non-negative integer" } { "before" sequence } { "after" sequence } }
1146 { $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" } "." }
1147 { $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." } ;
1148
1149 HELP: cut*
1150 { $values { "seq" sequence } { "n" "a non-negative integer" } { "before" sequence } { "after" sequence } }
1151 { $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" } "." } ;
1152
1153 HELP: subseq-start-from
1154 { $values { "subseq" sequence } { "seq" sequence } { "n" "a start index" } { "i" "a start index" } }
1155 { $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 } "." } ;
1156
1157 HELP: subseq-start
1158 { $values { "subseq" sequence } { "seq" sequence } { "i" "a start index" } }
1159 { $description "Outputs the start index of the first contiguous subsequence equal to " { $snippet "subseq" } ", or " { $link f } " if no matching subsequence is found." } ;
1160
1161 HELP: subseq?
1162 { $values { "subseq" sequence } { "seq" sequence } { "?" boolean } }
1163 { $description "Tests if " { $snippet "seq" } " contains the elements of " { $snippet "subseq" } " as a contiguous subsequence." } ;
1164
1165 HELP: drop-prefix
1166 { $values { "seq1" sequence } { "seq2" sequence } { "slice1" "a slice" } { "slice2" "a slice" } }
1167 { $description "Outputs a pair of virtual sequences with the common prefix of " { $snippet "seq1" } " and " { $snippet "seq2" } " removed." } ;
1168
1169 HELP: unclip
1170 { $values { "seq" sequence } { "rest" sequence } { "first" object } }
1171 { $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." }
1172 { $examples
1173     { $example "USING: prettyprint sequences ;" "{ 1 2 3 } unclip suffix ." "{ 2 3 1 }" }
1174 } ;
1175
1176 HELP: unclip-slice
1177 { $values { "seq" sequence } { "rest-slice" slice } { "first" object } }
1178 { $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." }
1179 { $examples { $example "USING: math.order prettyprint sequences ;" "{ 3 -1 -10 5 7 } unclip-slice [ min ] reduce ." "-10" } } ;
1180
1181 HELP: unclip-last
1182 { $values { "seq" sequence } { "butlast" sequence } { "last" object } }
1183 { $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." }
1184 { $examples
1185     { $example "USING: prettyprint sequences ;" "{ 1 2 3 } unclip-last prefix ." "{ 3 1 2 }" }
1186 } ;
1187
1188 HELP: unclip-last-slice
1189 { $values { "seq" sequence } { "butlast-slice" slice } { "last" object } }
1190 { $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." } ;
1191
1192 HELP: sum
1193 { $values { "seq" { $sequence number } } { "n" number } }
1194 { $description "Outputs the sum of all elements of " { $snippet "seq" } ". Outputs zero given an empty sequence." } ;
1195
1196 HELP: product
1197 { $values { "seq" { $sequence number } } { "n" number } }
1198 { $description "Outputs the product of all elements of " { $snippet "seq" } ". Outputs one given an empty sequence." } ;
1199
1200 HELP: infimum
1201 { $values { "seq" sequence } { "elt" object } }
1202 { $description "Outputs the least element of " { $snippet "seq" } "." }
1203 { $errors "Throws an error if the sequence is empty." } ;
1204
1205 HELP: supremum
1206 { $values { "seq" sequence } { "elt" object } }
1207 { $description "Outputs the greatest element of " { $snippet "seq" } "." }
1208 { $errors "Throws an error if the sequence is empty." } ;
1209
1210 { min max supremum infimum } related-words
1211
1212 HELP: shortest
1213 { $values { "seqs" sequence } { "elt" object } }
1214 { $description "Outputs the shortest sequence from " { $snippet "seqs" } "." } ;
1215
1216 HELP: longest
1217 { $values { "seqs" sequence } { "elt" object } }
1218 { $description "Outputs the longest sequence from " { $snippet "seqs" } "." } ;
1219
1220 { shortest longest } related-words
1221
1222 HELP: produce
1223 { $values { "pred" { $quotation ( ..a -- ..b ? ) } } { "quot" { $quotation ( ..b -- ..a obj ) } } { "seq" sequence } }
1224 { $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." }
1225 { $examples
1226     "The following example divides a number by two until we reach zero, and accumulates intermediate results:"
1227     { $example "USING: kernel math prettyprint sequences ;" "1337 [ dup 0 > ] [ 2/ dup ] produce nip ." "{ 668 334 167 83 41 20 10 5 2 1 0 }" }
1228     "The following example collects random numbers as long as they are greater than 1:"
1229     { $unchecked-example "USING: kernel prettyprint random sequences ;" "[ 10 random dup 1 > ] [ ] produce nip ." "{ 8 2 2 9 }" }
1230 } ;
1231
1232 HELP: produce-as
1233 { $values { "pred" { $quotation ( ..a -- ..b ? ) } } { "quot" { $quotation ( ..b -- ..a obj ) } } { "exemplar" sequence } { "seq" sequence } }
1234 { $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." }
1235 { $examples "See " { $link produce } " for examples." } ;
1236
1237 HELP: map-sum
1238 { $values { "seq" sequence } { "quot" quotation } { "n" number } }
1239 { $description "Like " { $snippet "map sum" } ", but without creating an intermediate sequence." }
1240 { $examples
1241     { $example
1242         "USING: math math.ranges sequences prettyprint ;"
1243         "100 [1,b] [ sq ] map-sum ."
1244         "338350"
1245     }
1246 } ;
1247
1248 HELP: count
1249 { $values { "seq" sequence } { "quot" quotation } { "n" integer } }
1250 { $description "Efficiently returns the number of elements that the predicate quotation matches." }
1251 { $examples
1252     { $example
1253         "USING: math math.ranges sequences prettyprint ;"
1254         "100 [1,b] [ even? ] count ."
1255         "50"
1256     }
1257 } ;
1258
1259 HELP: selector
1260 { $values
1261      { "quot" { $quotation ( ... elt -- ... ? ) } }
1262      { "selector" { $quotation ( ... elt -- ... ) } } { "accum" vector } }
1263 { $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 collector if the test yields true. The collector is left on the stack for convenience." }
1264 { $examples
1265     { $example "! Find all the even numbers:" "USING: prettyprint sequences math kernel ;"
1266                "10 <iota> [ even? ] selector [ each ] dip ."
1267                "V{ 0 2 4 6 8 }"
1268     }
1269 }
1270 { $notes "Used to implement the " { $link filter } " word. Compare this word with " { $link collector } ", which is an unfiltering version." } ;
1271
1272 HELP: trim-head
1273 { $values
1274      { "seq" sequence } { "quot" quotation }
1275      { "newseq" sequence } }
1276 { $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." }
1277 { $examples
1278     { $example "USING: prettyprint math sequences ;"
1279                "{ 0 0 1 2 3 0 0 } [ zero? ] trim-head ."
1280                "{ 1 2 3 0 0 }"
1281     }
1282 } ;
1283
1284 HELP: trim-head-slice
1285 { $values
1286      { "seq" sequence } { "quot" quotation }
1287      { "slice" slice } }
1288 { $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." }
1289 { $examples
1290     { $example "USING: prettyprint math sequences ;"
1291                "{ 0 0 1 2 3 0 0 } [ zero? ] trim-head-slice ."
1292                "T{ slice { from 2 } { to 7 } { seq { 0 0 1 2 3 0 0 } } }"
1293     }
1294 } ;
1295
1296 HELP: trim-tail
1297 { $values
1298      { "seq" sequence } { "quot" quotation }
1299      { "newseq" sequence } }
1300 { $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." }
1301 { $examples
1302     { $example "USING: prettyprint math sequences ;"
1303                "{ 0 0 1 2 3 0 0 } [ zero? ] trim-tail ."
1304                "{ 0 0 1 2 3 }"
1305     }
1306 } ;
1307
1308 HELP: trim-tail-slice
1309 { $values
1310      { "seq" sequence } { "quot" quotation }
1311      { "slice" slice } }
1312 { $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." }
1313 { $examples
1314     { $example "USING: prettyprint math sequences ;"
1315                "{ 0 0 1 2 3 0 0 } [ zero? ] trim-tail-slice ."
1316                "T{ slice { to 5 } { seq { 0 0 1 2 3 0 0 } } }"
1317     }
1318 } ;
1319
1320 HELP: trim
1321 { $values
1322      { "seq" sequence } { "quot" quotation }
1323      { "newseq" sequence } }
1324 { $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." }
1325 { $examples
1326     { $example "USING: prettyprint math sequences ;"
1327                "{ 0 0 1 2 3 0 0 } [ zero? ] trim ."
1328                "{ 1 2 3 }"
1329     }
1330 } ;
1331
1332 HELP: trim-slice
1333 { $values
1334      { "seq" sequence } { "quot" quotation }
1335      { "slice" slice } }
1336 { $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." }
1337 { $examples
1338     { $example "USING: prettyprint math sequences ;"
1339                "{ 0 0 1 2 3 0 0 } [ zero? ] trim-slice ."
1340                "T{ slice { from 2 } { to 5 } { seq { 0 0 1 2 3 0 0 } } }"
1341     }
1342 } ;
1343
1344 { trim trim-slice trim-head trim-head-slice trim-tail trim-tail-slice } related-words
1345
1346 HELP: sift
1347 { $values
1348      { "seq" sequence }
1349      { "newseq" sequence } }
1350  { $description "Outputs a new sequence with all instances of " { $link f } " removed." }
1351  { $examples
1352     { $example "USING: prettyprint sequences ;"
1353         "{ \"a\" 3 { } f } sift ."
1354         "{ \"a\" 3 { } }"
1355     }
1356 } ;
1357
1358 HELP: harvest
1359 { $values
1360      { "seq" sequence }
1361      { "newseq" sequence } }
1362 { $description "Outputs a new sequence with all empty sequences removed." }
1363 { $examples
1364     { $example "USING: prettyprint sequences ;"
1365                "{ { } { 2 3 } { 5 } { } } harvest ."
1366                "{ { 2 3 } { 5 } }"
1367     }
1368 } ;
1369
1370 { filter filter-as filter! reject reject-as reject! sift harvest } related-words
1371
1372 HELP: set-first
1373 { $values
1374      { "first" object } { "seq" sequence } }
1375 { $description "Sets the first element of a sequence." }
1376 { $examples
1377     { $example "USING: prettyprint kernel sequences ;"
1378         "{ 1 2 3 4 } 5 over set-first ."
1379         "{ 5 2 3 4 }"
1380     }
1381 } ;
1382
1383 HELP: set-second
1384 { $values
1385      { "second" object } { "seq" sequence } }
1386 { $description "Sets the second element of a sequence." }
1387 { $examples
1388     { $example "USING: prettyprint kernel sequences ;"
1389         "{ 1 2 3 4 } 5 over set-second ."
1390         "{ 1 5 3 4 }"
1391     }
1392 } ;
1393
1394 HELP: set-third
1395 { $values
1396      { "third" object } { "seq" sequence } }
1397 { $description "Sets the third element of a sequence." }
1398 { $examples
1399     { $example "USING: prettyprint kernel sequences ;"
1400         "{ 1 2 3 4 } 5 over set-third ."
1401         "{ 1 2 5 4 }"
1402     }
1403 } ;
1404
1405 HELP: set-fourth
1406 { $values
1407      { "fourth" object } { "seq" sequence } }
1408 { $description "Sets the fourth element of a sequence." }
1409 { $examples
1410     { $example "USING: prettyprint kernel sequences ;"
1411         "{ 1 2 3 4 } 5 over set-fourth ."
1412         "{ 1 2 3 5 }"
1413     }
1414 } ;
1415
1416 { set-first set-second set-third set-fourth } related-words
1417
1418 HELP: replicate
1419 { $values
1420      { "len" integer } { "quot" { $quotation ( ... -- ... newelt ) } }
1421      { "newseq" sequence } }
1422      { $description "Calls the quotation " { $snippet "len" } " times, collecting results into a new array." }
1423 { $examples
1424     { $unchecked-example "USING: kernel prettyprint random sequences ;"
1425         "5 [ 100 random ] replicate ."
1426         "{ 52 10 45 81 30 }"
1427     }
1428 } ;
1429
1430 HELP: replicate-as
1431 { $values
1432      { "len" integer } { "quot" { $quotation ( ... -- ... newelt ) } } { "exemplar" sequence }
1433      { "newseq" sequence } }
1434  { $description "Calls the quotation " { $snippet "len" } " times, collecting results into a new sequence of the same type as the exemplar sequence." }
1435 { $examples
1436     { $unchecked-example "USING: prettyprint kernel sequences ;"
1437         "5 [ 100 random ] B{ } replicate-as ."
1438         "B{ 44 8 2 33 18 }"
1439     }
1440 } ;
1441
1442 { replicate replicate-as } related-words
1443
1444 HELP: partition
1445 { $values
1446      { "seq" sequence } { "quot" quotation }
1447      { "trueseq" sequence } { "falseseq" sequence } }
1448      { $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" } "." }
1449 { $examples
1450     { $example "USING: prettyprint kernel math sequences ;"
1451         "{ 1 2 3 4 5 } [ even? ] partition [ . ] bi@"
1452         "{ 2 4 }\n{ 1 3 5 }"
1453     }
1454 } ;
1455
1456 HELP: virtual-exemplar
1457 { $values
1458      { "seq" sequence }
1459      { "seq'" sequence } }
1460 { $description "Part of the virtual sequence protocol, this word is used to return an exemplar of the underlying storage. This is used in words like " { $link new-sequence } "." } ;
1461
1462 HELP: virtual@
1463 { $values
1464      { "n" integer } { "seq" sequence }
1465      { "n'" integer } { "seq'" sequence } }
1466 { $description "Part of the sequence protocol, this word translates the input index " { $snippet "n" } " into an index and the underlying storage this index points into." } ;
1467
1468 HELP: 2map-reduce
1469 { $values
1470      { "seq1" sequence } { "seq2" sequence } { "map-quot" { $quotation ( ..a elt1 elt2 -- ..b intermediate ) } } { "reduce-quot" { $quotation ( ..b prev intermediate -- ..a next ) } }
1471      { "result" object } }
1472  { $description "Calls " { $snippet "map-quot" } " on each pair of elements from " { $snippet "seq1" } " and " { $snippet "seq2" } " and combines the results using " { $snippet "reduce-quot" } " in the same manner as " { $link reduce } ", except that there is no identity element, and the sequence must have a length of at least 1." }
1473 { $errors "Throws an error if the sequence is empty." }
1474 { $examples { $example "USING: sequences prettyprint math ;"
1475     "{ 10 30 50 } { 200 400 600 } [ + ] [ + ] 2map-reduce ."
1476     "1290"
1477 } } ;
1478
1479 HELP: 2selector
1480 { $values
1481      { "quot" quotation }
1482      { "selector" quotation } { "accum1" vector } { "accum2" vector } }
1483 { $description "Creates two new vectors to accumulate values based on a predicate. The first vector accumulates values for which the predicate yields true; the second for false." } ;
1484
1485 HELP: collector
1486 { $values
1487      { "quot" quotation }
1488      { "quot'" quotation } { "vec" vector } }
1489 { $description "Creates a new quotation that pushes its result to a vector and outputs that vector on the stack." }
1490 { $examples { $example "USING: sequences prettyprint kernel math ;"
1491     "{ 1 2 } [ 30 + ] collector [ each ] dip ."
1492     "V{ 31 32 }"
1493 } } ;
1494
1495 HELP: binary-reduce
1496 { $values
1497      { "seq" sequence } { "start" integer } { "quot" { $quotation ( elt1 elt2 -- newelt ) } }
1498      { "value" object } }
1499 { $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 } "." }
1500 { $examples "Computing factorial:"
1501     { $example "USING: prettyprint sequences math ;"
1502     "40 <iota> rest-slice 1 [ * ] binary-reduce ."
1503     "20397882081197443358640281739902897356800000000" }
1504 } ;
1505
1506 HELP: follow
1507 { $values
1508      { "obj" object } { "quot" { $quotation ( ... prev -- ... result/f ) } }
1509      { "seq" sequence } }
1510 { $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 recursively. Objects yielded by the quotation are added to the output sequence until the quotation yields " { $link f } ", at which point the recursion terminates." }
1511 { $examples "Get random numbers until zero is reached:"
1512     { $unchecked-example
1513     "USING: random sequences prettyprint math ;"
1514     "100 [ random [ f ] when-zero ] follow ."
1515     "{ 100 86 34 32 24 11 7 2 }"
1516 } } ;
1517
1518 HELP: halves
1519 { $values
1520      { "seq" sequence }
1521      { "first-slice" slice } { "second-slice" slice } }
1522 { $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." }
1523 { $examples { $example "USING: arrays sequences prettyprint kernel ;"
1524     "{ 1 2 3 4 5 } halves [ >array . ] bi@"
1525     "{ 1 2 }\n{ 3 4 5 }"
1526 } } ;
1527
1528 HELP: indices
1529 { $values
1530      { "obj" object } { "seq" sequence }
1531      { "indices" sequence } }
1532 { $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." }
1533 { $examples { $example "USING: sequences prettyprint ;"
1534     "2 { 2 4 2 6 2 8 2 10 } indices ."
1535     "V{ 0 2 4 6 }"
1536 } } ;
1537
1538 HELP: insert-nth
1539 { $values
1540      { "elt" object } { "n" integer } { "seq" sequence }
1541      { "seq'" sequence } }
1542 { $description "Creates a new sequence where the " { $snippet "n" } "th index is set to the input object." }
1543 { $examples { $example "USING: prettyprint sequences ;"
1544     "40 3 { 10 20 30 50 } insert-nth ."
1545     "{ 10 20 30 40 50 }"
1546 } } ;
1547
1548 HELP: map-reduce
1549 { $values
1550      { "seq" sequence } { "map-quot" { $quotation ( ..a elt -- ..b intermediate ) } } { "reduce-quot" { $quotation ( ..b prev intermediate -- ..a next ) } }
1551      { "result" object } }
1552 { $description "Calls " { $snippet "map-quot" } " on each element and combines the results using " { $snippet "reduce-quot" } " in the same manner as " { $link reduce } ", except that there is no identity element, and the sequence must have a length of at least 1." }
1553 { $errors "Throws an error if the sequence is empty." }
1554 { $examples { $example "USING: sequences prettyprint math ;"
1555     "{ 1 3 5 } [ sq ] [ + ] map-reduce ."
1556     "35"
1557 } } ;
1558
1559 HELP: new-like
1560 { $values
1561      { "len" integer } { "exemplar" "an exemplar sequence" } { "quot" quotation }
1562      { "seq" sequence } }
1563 { $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." } ;
1564
1565 HELP: push-either
1566 { $values
1567      { "elt" object } { "quot" quotation } { "accum1" vector } { "accum2" vector } }
1568 { $description "Pushes the input object onto one of the accumulators; the first if the quotation yields true, the second if false." } ;
1569
1570 HELP: sequence-hashcode
1571 { $values
1572      { "n" integer } { "seq" sequence }
1573      { "x" integer } }
1574 { $description "Iterates over a sequence, computes a hashcode with " { $link hashcode* } " for each element, and combines them using " { $link sequence-hashcode-step } "." } ;
1575
1576 HELP: sequence-hashcode-step
1577 { $values
1578      { "oldhash" integer } { "newpart" integer }
1579      { "newhash" integer } }
1580 { $description "An implementation word that computes a running hashcode of a sequence using some bit-twiddling. The resulting hashcode is always a fixnum." } ;
1581
1582 HELP: short
1583 { $values
1584      { "seq" sequence } { "n" integer } { "n'" integer } }
1585 { $description "Returns the input sequence and its length or " { $snippet "n" } ", whichever is less." }
1586 { $examples { $example "USING: sequences kernel prettyprint ;"
1587     "\"abcd\" 3 short [ . ] bi@"
1588     "\"abcd\"\n3"
1589 } } ;
1590
1591 HELP: shorten
1592 { $values
1593      { "n" integer } { "seq" sequence } }
1594 { $description "Shortens a " { $link "growable" } " sequence to be " { $snippet "n" } " elements long." }
1595 { $examples { $example "USING: sequences prettyprint kernel ;"
1596     "V{ 1 2 3 4 5 } 3 over shorten ."
1597     "V{ 1 2 3 }"
1598 } } ;
1599
1600 HELP: <iota>
1601 { $values { "n" integer } { "iota" iota } }
1602 { $description "Creates an immutable virtual sequence containing the integers from 0 to " { $snippet "n-1" } "." }
1603 { $examples
1604   { $example
1605     "USING: math sequences prettyprint ;"
1606     "3 <iota> [ sq ] map ."
1607     "{ 0 1 4 }"
1608   }
1609 } ;
1610
1611 HELP: assert-sequence=
1612 { $values
1613     { "a" sequence } { "b" sequence }
1614 }
1615 { $description "Throws an error if all the elements of two sequences, taken pairwise, are not equal." }
1616 { $notes "The sequences need not be of the same type." }
1617 { $examples
1618   { $code
1619     "USING: prettyprint sequences ;"
1620     "{ 1 2 3 } V{ 1 2 3 } assert-sequence="
1621   }
1622 } ;
1623
1624 HELP: cartesian-find
1625 { $values { "seq1" sequence } { "seq2" sequence } { "quot" { $quotation ( ... elt1 elt2 -- ... ? ) } } { "elt1" object } { "elt2" object } }
1626 { $description "Applies the quotation to every possible pairing of elements from the two sequences, returning the first two elements where the quotation returns a true value." } ;
1627
1628 HELP: cartesian-each
1629 { $values { "seq1" sequence } { "seq2" sequence } { "quot" { $quotation ( ... elt1 elt2 -- ... ) } } }
1630 { $description "Applies the quotation to every possible pairing of elements from the two sequences." } ;
1631
1632 HELP: cartesian-map
1633 { $values { "seq1" sequence } { "seq2" sequence } { "quot" { $quotation ( ... elt1 elt2 -- ... newelt ) } } { "newseq" "a new sequence of sequences" } }
1634 { $description "Applies the quotation to every possible pairing of elements from the two sequences, collecting results into a new sequence of sequences." } ;
1635
1636 HELP: cartesian-product
1637 { $values { "seq1" sequence } { "seq2" sequence } { "newseq" "a new sequence of sequences of pairs" } }
1638 { $description "Outputs a sequence of all possible pairings of elements from the two sequences." }
1639 { $examples
1640     { $example
1641         "USING: prettyprint sequences ;"
1642         "{ 1 2 } { 3 4 } cartesian-product ."
1643         "{ { { 1 3 } { 1 4 } } { { 2 3 } { 2 4 } } }"
1644     }
1645 } ;
1646
1647 ARTICLE: "sequences-unsafe" "Unsafe sequence operations"
1648 "The " { $link nth-unsafe } " and " { $link set-nth-unsafe } " sequence protocol bypasses bounds checks for increased performance."
1649 $nl
1650 "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."
1651 $nl
1652 "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."
1653 $nl
1654 "The justification for this is that the VM should not crash if a resizable sequence is resized during the execution of an iteration combinator."
1655 $nl
1656 "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 } "." ;
1657
1658 ARTICLE: "sequence-protocol" "Sequence protocol"
1659 "All sequences must be instances of a mixin class:"
1660 { $subsections sequence sequence? }
1661 "All sequences must know their length:"
1662 { $subsections length }
1663 "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:"
1664 { $subsections nth nth-unsafe }
1665 "Note that sequences are always indexed starting from zero."
1666 $nl
1667 "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:"
1668 { $subsections set-nth set-nth-unsafe }
1669 "If your sequence is immutable, then you must implement either " { $link set-nth } " or " { $link set-nth-unsafe } " to simply call " { $link immutable } " to signal an error."
1670 $nl
1671 "The following two generic words are optional, as not all sequences are resizable:"
1672 { $subsections set-length lengthen }
1673 "An optional generic word for creating sequences of the same class as a given sequence:"
1674 { $subsections like }
1675 "Optional generic words for optimization purposes:"
1676 { $subsections new-sequence new-resizable }
1677 { $see-also "sequences-unsafe" } ;
1678
1679 ARTICLE: "virtual-sequences-protocol" "Virtual sequence protocol"
1680 "Virtual sequences must know their length:"
1681 { $subsections length }
1682 "An exemplar of the underlying storage:"
1683 { $subsections virtual-exemplar }
1684 "The index and the underlying storage where the value is located:"
1685 { $subsections virtual@ } ;
1686
1687 ARTICLE: "virtual-sequences" "Virtual sequences"
1688 "A virtual sequence is an implementation of the " { $link "sequence-protocol" } " which does not store its own elements, and instead computes them, either from scratch or by retrieving them from another sequence."
1689 $nl
1690 "Implementations include the following:"
1691 { $subsections reversed slice }
1692 "Virtual sequences can be implemented with the " { $link "virtual-sequences-protocol" } ", by translating an index in the virtual sequence into an index in another sequence."
1693 { $see-also "sequences-integers" } ;
1694
1695 ARTICLE: "sequences-integers" "Counted loops"
1696 "A virtual sequence is defined for iterating over integers from zero."
1697 { $subsection <iota> }
1698 "For example, calling " { $link <iota> } " on the integer 3 produces a sequence containing the elements 0, 1, and 2. This is very useful for performing counted loops using words such as " { $link each } ":"
1699 { $example "USING: sequences prettyprint ; 3 <iota> [ . ] each" "0\n1\n2" }
1700 "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 } "."
1701 $nl
1702 "Combinators that produce new sequences, such as " { $link map } ", will output an array if the input is an instance of " { $link <iota> } "."
1703 $nl
1704 "More elaborate counted loops can be performed with " { $link "math.ranges" } "." ;
1705
1706 ARTICLE: "sequences-if" "Control flow with sequences"
1707 "To reduce the boilerplate of checking if a sequence is empty, several combinators are provided."
1708 $nl
1709 "Checking if a sequence is empty:"
1710 { $subsections if-empty when-empty unless-empty } ;
1711
1712 ARTICLE: "sequences-access" "Accessing sequence elements"
1713 "Element access by index, without raising exceptions:"
1714 { $subsections ?nth }
1715 "Concise way of extracting one of the first four elements:"
1716 { $subsections first second third fourth ?first ?second }
1717 "Extracting the last element:"
1718 { $subsections last ?last }
1719 "Unpacking sequences:"
1720 { $subsections first2 first3 first4 }
1721 { $see-also nth } ;
1722
1723 ARTICLE: "sequences-add-remove" "Adding and removing sequence elements"
1724 "Adding elements:"
1725 { $subsections prefix suffix insert-nth }
1726 "Removing elements:"
1727 { $subsections remove remove-eq remove-nth } ;
1728
1729 ARTICLE: "sequences-reshape" "Reshaping sequences"
1730 "A " { $emphasis "repetition" } " is a virtual sequence consisting of a single element repeated multiple times:"
1731 { $subsections repetition <repetition> }
1732 "Reversing a sequence:"
1733 { $subsections reverse }
1734 "A " { $emphasis "reversal" } " presents a reversed view of an underlying sequence:"
1735 { $subsections reversed <reversed> }
1736 "Transposing a matrix:"
1737 { $subsections flip } ;
1738
1739 ARTICLE: "sequences-appending" "Appending sequences"
1740 "Basic append operations:"
1741 { $subsections
1742     append
1743     append-as
1744     prepend
1745     3append
1746     3append-as
1747     surround
1748     glue
1749 }
1750 "Collapse a sequence unto itself:"
1751 { $subsections concat join }
1752 "A pair of words useful for aligning strings:"
1753 { $subsections pad-head pad-tail } ;
1754
1755 ARTICLE: "sequences-slices" "Subsequences and slices"
1756 "There are two ways to extract a subrange of elements from a sequence. The first approach creates a new sequence of the same type as the input, which does not share storage with the underlying sequence. This takes time proportional to the number of elements being extracted. The second approach creates a " { $emphasis "slice" } ", which is a virtual sequence (see " { $link "virtual-sequences" } ") sharing storage with the original sequence. Slices are constructed in constant time."
1757 $nl
1758 "Some general guidelines for choosing between the two approaches:"
1759 { $list
1760   "If you are using mutable state, the choice has to be made one way or another because of semantics; mutating a slice will change the underlying sequence."
1761   { "Using a slice can improve algorithmic complexity. For example, if each iteration of a loop decomposes a sequence using " { $link first } " and " { $link rest } ", then the loop will run in quadratic time, relative to the length of the sequence. Using " { $link rest-slice } " changes the loop to run in linear time, since " { $link rest-slice } " does not copy any elements. Taking a slice of a slice will “collapse” the slice so to avoid the double indirection, so it is safe to use slices in recursive code." }
1762   "Accessing elements from a concrete sequence (such as a string or an array) is often faster than accessing elements from a slice, because slice access entails additional indirection. However, in some cases, if the slice is immediately consumed by an iteration combinator, the compiler can eliminate the slice allocation and indirection altogether."
1763   "If the slice outlives the original sequence, the original sequence will still remain in memory, since the slice will reference it. This can increase memory consumption unnecessarily."
1764 }
1765 { $heading "Subsequence operations" }
1766 "Extracting a subsequence:"
1767 { $subsections
1768     subseq
1769     subseq-as
1770     head
1771     tail
1772     head*
1773     tail*
1774 }
1775 "Removing the first or last element:"
1776 { $subsections rest but-last }
1777 "Taking a sequence apart into a head and a tail:"
1778 { $subsections
1779     unclip
1780     unclip-last
1781     cut
1782     cut*
1783 }
1784 { $heading "Slice operations" }
1785 "The slice data type:"
1786 { $subsections slice slice? }
1787 "Extracting a slice:"
1788 { $subsections
1789     <slice>
1790     head-slice
1791     tail-slice
1792     head-slice*
1793     tail-slice*
1794 }
1795 "Removing the first or last element:"
1796 { $subsections rest-slice but-last-slice }
1797 "Taking a sequence apart into a head and a tail:"
1798 { $subsections unclip-slice unclip-last-slice cut-slice }
1799 "Replacing slices with new elements:"
1800 { $subsections replace-slice } ;
1801
1802 ARTICLE: "sequences-combinators" "Sequence combinators"
1803 "Iteration:"
1804 { $subsections
1805     each
1806     each-index
1807     reduce
1808     interleave
1809 }
1810 "Mapping:"
1811 { $subsections
1812     map
1813     map-as
1814     map-index
1815     map-index-as
1816     map-reduce
1817     accumulate
1818     accumulate-as
1819     accumulate*
1820     accumulate*-as
1821 }
1822 "Filtering:"
1823 { $subsections
1824     filter
1825     filter-as
1826     partition
1827 }
1828 "Generating:"
1829 { $subsections
1830     replicate
1831     replicate-as
1832     produce
1833     produce-as
1834 }
1835 "Testing if a sequence contains elements satisfying a predicate:"
1836 { $subsections
1837     any?
1838     all?
1839 }
1840 { $heading "Related Articles" }
1841 { $subsections
1842     "sequence-2combinators"
1843     "sequence-3combinators"
1844 } ;
1845
1846 ARTICLE: "sequence-2combinators" "Pair-wise sequence combinators"
1847 "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."
1848 { $subsections
1849     2each
1850     2reduce
1851     2map
1852     2map-as
1853     2map-reduce
1854     2all?
1855 } ;
1856
1857 ARTICLE: "sequence-3combinators" "Triple-wise sequence combinators"
1858 "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."
1859 { $subsections 3each 3map 3map-as } ;
1860
1861 ARTICLE: "sequences-tests" "Testing sequences"
1862 "Testing for an empty sequence:"
1863 { $subsections empty? }
1864 "Testing indices:"
1865 { $subsections bounds-check? }
1866 "Testing if a sequence contains an object:"
1867 { $subsections member? member-eq? }
1868 "Testing if a sequence contains a subsequence:"
1869 { $subsections head? tail? subseq? } ;
1870
1871 ARTICLE: "sequences-search" "Searching sequences"
1872 "Finding the index of an element:"
1873 { $subsections
1874     index
1875     index-from
1876     last-index
1877     last-index-from
1878 }
1879 "Finding the start of a subsequence:"
1880 { $subsections subseq-start subseq-start-from }
1881 "Finding the index of an element satisfying a predicate:"
1882 { $subsections
1883     find
1884     find-from
1885     find-last
1886     find-last-from
1887     map-find
1888 } ;
1889
1890 ARTICLE: "sequences-trimming" "Trimming sequences"
1891 "Trimming words:"
1892 { $subsections trim trim-head trim-tail }
1893 "Potentially more efficient trim:"
1894 { $subsections trim-slice trim-head-slice trim-tail-slice } ;
1895
1896 ARTICLE: "sequences-destructive-discussion" "When to use destructive operations"
1897 "Constructive (non-destructive) operations should be preferred where possible because code without side-effects is usually more reusable and easier to reason about. There are two main reasons to use destructive operations:"
1898 { $list
1899     "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."
1900     { "As an optimization. Some code written to use constructive operations suffers from worse performance. An example is a loop which adds an element to a sequence on each iteration. Either " { $link suffix } " or " { $link suffix! } " could be used; however, the former copies the entire sequence each time, which would cause the loop to run in quadratic time." }
1901 }
1902 "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." ;
1903
1904 ARTICLE: "sequences-destructive" "Destructive sequence operations"
1905 "Many operations have destructive variants that side effect an input sequence, instead of creating a new sequence:"
1906 { $table
1907     { "Constructive" "Destructive" }
1908     { { $link suffix } { $link suffix! } }
1909     { { $link remove } { $link remove! } }
1910     { { $link remove-eq } { $link remove-eq! } }
1911     { { $link remove-nth } { $link remove-nth! } }
1912     { { $link reverse } { $link reverse! } }
1913     { { $link append } { $link append! } }
1914     { { $link map } { $link map! } }
1915     { { $link accumulate } { $link accumulate! } }
1916     { { $link accumulate* } { $link accumulate*! } }
1917     { { $link filter } { $link filter! } }
1918 }
1919 "Changing elements:"
1920 { $subsections map! accumulate! accumulate*! change-nth }
1921 "Deleting elements:"
1922 { $subsections
1923     remove!
1924     remove-eq!
1925     remove-nth!
1926     delete-slice
1927     delete-all
1928     filter!
1929 }
1930 "Adding elements:"
1931 { $subsections
1932     suffix!
1933     append!
1934 }
1935 "Other destructive words:"
1936 { $subsections
1937     reverse!
1938     move
1939     exchange
1940     copy
1941 }
1942 { $heading "Related Articles" }
1943 { $subsections
1944     "sequences-destructive-discussion"
1945     "sequences-stacks"
1946 }
1947 { $see-also set-nth push push-all pop pop* } ;
1948
1949 ARTICLE: "sequences-stacks" "Treating sequences as stacks"
1950 "The classical stack operations, modifying a sequence in place:"
1951 { $subsections push push-all pop pop* }
1952 { $see-also empty? } ;
1953
1954 ARTICLE: "sequences-comparing" "Comparing sequences"
1955 "Element equality testing:"
1956 { $subsections
1957     sequence=
1958     mismatch
1959     drop-prefix
1960     assert-sequence=
1961 }
1962 "The " { $link <=> } " generic word performs lexicographic comparison when applied to sequences." ;
1963
1964 ARTICLE: "sequences-f" "The f object as a sequence"
1965 "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." ;
1966
1967 ARTICLE: "sequences-combinator-implementation" "Implementing sequence combinators"
1968 "Creating a new sequence unconditionally:"
1969 { $subsections
1970     collector
1971     collector-as
1972     collector-for-as
1973 }
1974 "Creating a new sequence conditionally:"
1975 { $subsections
1976     selector
1977     selector-as
1978     2selector
1979 } ;
1980
1981 ARTICLE: "sequences-cartesian" "Cartesian product operations"
1982 "The cartesian product of two sequences is a sequence of all pairs where the first element of each pair is from the first sequence, and the second element of each pair is from the second sequence. The number of elements in the cartesian product is the product of the lengths of the two sequences."
1983 $nl
1984 "Combinators which pair every element of the first sequence with every element of the second:"
1985 { $subsections
1986     cartesian-each
1987     cartesian-map
1988     cartesian-find
1989 }
1990 "Computing the cartesian product of two sequences:"
1991 { $subsections
1992     cartesian-product
1993 } ;
1994
1995 ARTICLE: "sequences" "Sequence operations"
1996 "A " { $emphasis "sequence" } " is a finite, linearly-ordered collection of elements. Words for working with sequences are in the " { $vocab-link "sequences" } " vocabulary."
1997 $nl
1998 "Sequences implement a protocol:"
1999 { $subsections
2000     "sequence-protocol"
2001     "sequences-f"
2002 }
2003 "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" } "."
2004 { $subsections
2005     "sequences-access"
2006     "sequences-combinators"
2007     "sequences-add-remove"
2008     "sequences-appending"
2009     "sequences-slices"
2010     "sequences-reshape"
2011     "sequences-tests"
2012     "sequences-search"
2013     "sequences-comparing"
2014     "sequences-split"
2015     "grouping"
2016     "sequences-destructive"
2017     "sequences-stacks"
2018     "sequences-sorting"
2019     "binary-search"
2020     "sets"
2021     "sequences-trimming"
2022     "sequences-cartesian"
2023     "sequences.deep"
2024 }
2025 "Using sequences for looping:"
2026 { $subsections
2027     "sequences-integers"
2028     "math.ranges"
2029 }
2030 "Using sequences for control flow:"
2031 { $subsections "sequences-if" }
2032 "For inner loops:"
2033 { $subsections "sequences-unsafe" }
2034 "Implementing sequence combinators:"
2035 { $subsections "sequences-combinator-implementation" } ;
2036
2037 ABOUT: "sequences"