]> gitweb.factorcode.org Git - factor.git/blob - core/sequences/sequences-docs.factor
sequences-docs: fix a typo
[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 varient 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 varient 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: all?
550 { $values { "seq" sequence } { "quot" { $quotation ( ... elt -- ... ? ) } } { "?" boolean } }
551 { $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 } "." } ;
552
553 HELP: push-if
554 { $values { "elt" object } { "quot" { $quotation ( ..a elt -- ..b ? ) } } { "accum" "a resizable mutable sequence" } }
555 { $description "Adds the element at the end of the sequence if the quotation yields a true value." }
556 { $notes "This word is a factor of " { $link filter } "." } ;
557
558 HELP: filter
559 { $values { "seq" sequence } { "quot" { $quotation ( ... elt -- ... ? ) } } { "subseq" "a new sequence" } }
560 { $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." } ;
561
562 HELP: filter-as
563 { $values { "seq" sequence } { "quot" { $quotation ( ... elt -- ... ? ) } } { "exemplar" sequence } { "subseq" "a new sequence" } }
564 { $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." } ;
565
566 HELP: filter!
567 { $values { "seq" "a resizable mutable sequence" } { "quot" { $quotation ( ... elt -- ... ? ) } } }
568 { $description "Applies the quotation to each element in turn, and removes elements for which the quotation outputs a false value." }
569 { $side-effects "seq" } ;
570
571 HELP: reject
572 { $values { "seq" sequence } { "quot" { $quotation ( ... elt -- ... ? ) } } { "subseq" "a new sequence" } }
573 { $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." } ;
574
575 HELP: reject-as
576 { $values { "seq" sequence } { "quot" { $quotation ( ... elt -- ... ? ) } } { "exemplar" sequence } { "subseq" "a new sequence" } }
577 { $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." } ;
578
579 HELP: reject!
580 { $values { "seq" "a resizable mutable sequence" } { "quot" { $quotation ( ... elt -- ... ? ) } } }
581 { $description "Applies the quotation to each element in turn, and removes elements for which the quotation outputs a true value." }
582 { $side-effects "seq" } ;
583
584 HELP: interleave
585 { $values { "seq" sequence } { "between" quotation } { "quot" { $quotation ( ... elt -- ... ) } } }
586 { $description "Applies " { $snippet "quot" } " to each element in turn, also invoking " { $snippet "between" } " in-between each pair of elements." }
587 { $examples { $example "USING: io sequences ;" "{ \"a\" \"b\" \"c\" } [ \"X\" write ] [ write ] interleave" "aXbXc" } } ;
588
589 HELP: index
590 { $values { "obj" object } { "seq" sequence } { "n" "an index" } }
591 { $description "Outputs the index of the first element in the sequence equal to " { $snippet "obj" } ". If no element is found, outputs " { $link f } "." } ;
592
593 HELP: index-from
594 { $values { "obj" object } { "i" "a start index" } { "seq" sequence } { "n" "an index" } }
595 { $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 } "." } ;
596
597 HELP: last-index
598 { $values { "obj" object } { "seq" sequence } { "n" "an index" } }
599 { $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 } "." } ;
600
601 HELP: last-index-from
602 { $values { "obj" object } { "i" "a start index" } { "seq" sequence } { "n" "an index" } }
603 { $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 } "." } ;
604
605 HELP: member?
606 { $values { "elt" object } { "seq" sequence } { "?" boolean } }
607 { $description "Tests if the sequence contains an element equal to the object." }
608 { $examples
609     "Is a letter in a string:"
610     { $example
611         "USING: sequences prettyprint ;"
612         "CHAR: a \"abc\" member? ."
613         "t"
614     } $nl
615     "Is a number in a sequence:"
616     { $example
617         "USING: sequences prettyprint ;"
618         "4 { 1 2 3 } member? ."
619         "f"
620     }
621 }
622 { $notes "This word uses equality comparison (" { $link = } ")." } ;
623
624 HELP: member-eq?
625 { $values { "elt" object } { "seq" sequence } { "?" boolean } }
626 { $description "Tests if the sequence contains the object." }
627 { $notes "This word uses identity comparison (" { $link eq? } ")." } ;
628
629 HELP: remove
630 { $values { "elt" object } { "seq" sequence } { "newseq" "a new sequence" } }
631 { $description "Outputs a new sequence containing all elements of the input sequence except for given element." }
632 { $notes "This word uses equality comparison (" { $link = } ")." } ;
633
634 HELP: remove-eq
635 { $values { "elt" object } { "seq" sequence } { "newseq" "a new sequence" } }
636 { $description "Outputs a new sequence containing all elements of the input sequence except those equal to the given element." }
637 { $notes "This word uses identity comparison (" { $link eq? } ")." } ;
638
639 HELP: remove-nth
640 { $values
641      { "n" integer } { "seq" sequence }
642      { "seq'" sequence } }
643 { $description "Creates a new sequence without the element at index " { $snippet "n" } "." }
644 { $examples "Notice that the original sequence is left intact:" { $example "USING: sequences prettyprint kernel ;"
645     "{ 1 2 3 } 1 over remove-nth . ."
646     "{ 1 3 }\n{ 1 2 3 }"
647 } } ;
648
649 HELP: move
650 { $values { "to" "an index in " { $snippet "seq" } } { "from" "an index in " { $snippet "seq" } } { "seq" "a mutable sequence" } }
651 { $description "Sets the element with index " { $snippet "m" } " to the element with index " { $snippet "n" } "." }
652 { $side-effects "seq" } ;
653
654 HELP: remove!
655 { $values { "elt" object } { "seq" "a resizable mutable sequence" } }
656 { $description "Removes all elements equal to " { $snippet "elt" } " from " { $snippet "seq" } " and returns " { $snippet "seq" } "." }
657 { $notes "This word uses equality comparison (" { $link = } ")." }
658 { $side-effects "seq" } ;
659
660 HELP: remove-eq!
661 { $values { "elt" object } { "seq" "a resizable mutable sequence" } }
662 { $description "Outputs a new sequence containing all elements of the input sequence except the given element." }
663 { $notes "This word uses identity comparison (" { $link eq? } ")." }
664 { $side-effects "seq" } ;
665
666 HELP: remove-nth!
667 { $values { "n" "a non-negative integer" } { "seq" "a resizable mutable sequence" } }
668 { $description "Removes the " { $snippet "n" } "th element from the sequence, shifting all other elements down and reducing its length by one." }
669 { $side-effects "seq" } ;
670
671 HELP: delete-slice
672 { $values { "from" "a non-negative integer" } { "to" "a non-negative integer" } { "seq" "a resizable mutable sequence" } }
673 { $description "Removes a range of elements beginning at index " { $snippet "from" } " and ending before index " { $snippet "to" } "." }
674 { $side-effects "seq" } ;
675
676 HELP: replace-slice
677 { $values { "new" sequence } { "from" "a non-negative integer" } { "to" "a non-negative integer" } { "seq" sequence } { "seq'" sequence } }
678 { $description "Replaces a range of elements beginning at index " { $snippet "from" } " and ending before index " { $snippet "to" } " with a new sequence." }
679 { $errors "Throws an error if " { $snippet "new" } " contains elements whose types are not permissible in " { $snippet "seq" } "." } ;
680
681 { push push-either push-if pop pop* prefix suffix suffix! } related-words
682
683 HELP: suffix
684 { $values { "seq" sequence } { "elt" object } { "newseq" sequence } }
685 { $description "Outputs a new sequence obtained by adding " { $snippet "elt" } " at the end of " { $snippet "seq" } "." }
686 { $errors "Throws an error if the type of " { $snippet "elt" } " is not permitted in sequences of the same class as " { $snippet "seq1" } "." }
687 { $examples
688     { $example "USING: prettyprint sequences ;" "{ 1 2 3 } 4 suffix ." "{ 1 2 3 4 }" }
689 } ;
690
691 HELP: suffix!
692 { $values { "seq" sequence } { "elt" object } }
693 { $description "Modifiers a sequence in-place by adding " { $snippet "elt" } " to the end of " { $snippet "seq" } ". Outputs " { $snippet "seq" } "." }
694 { $errors "Throws an error if the type of " { $snippet "elt" } " is not permitted in sequences of the same class as " { $snippet "seq" } "." }
695 { $examples
696     { $example "USING: prettyprint sequences ;" "V{ 1 2 3 } 4 suffix! ." "V{ 1 2 3 4 }" }
697 } ;
698
699 HELP: append!
700 { $values { "seq1" sequence } { "seq2" sequence } }
701 { $description "Modifiers " { $snippet "seq1" } " in-place by adding the elements from " { $snippet "seq2" } " to the end and outputs " { $snippet "seq1" } "." }
702 { $examples
703     { $example "USING: prettyprint sequences ;" "V{ 1 2 3 } { 4 5 6 } append! ." "V{ 1 2 3 4 5 6 }" }
704 } ;
705
706 HELP: prefix
707 { $values { "seq" sequence } { "elt" object } { "newseq" sequence } }
708 { $description "Outputs a new sequence obtained by adding " { $snippet "elt" } " at the beginning of " { $snippet "seq" } "." }
709 { $errors "Throws an error if the type of " { $snippet "elt" } " is not permitted in sequences of the same class as " { $snippet "seq1" } "." }
710 { $examples
711 { $example "USING: prettyprint sequences ;" "{ 1 2 3 } 0 prefix ." "{ 0 1 2 3 }" }
712 } ;
713
714 HELP: sum-lengths
715 { $values { "seq" { $sequence sequence } } { "n" integer } }
716 { $description "Outputs the sum of the lengths of all sequences in " { $snippet "seq" } "." } ;
717
718 HELP: concat
719 { $values { "seq" sequence } { "newseq" sequence } }
720 { $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" } "." }
721 { $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" } "." } ;
722
723 HELP: concat-as
724 { $values { "seq" sequence } { "exemplar" sequence } { "newseq" sequence } }
725 { $description "Concatenates a sequence of sequences together into one sequence with the same type as " { $snippet "exemplar" } "." }
726 { $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" } "." } ;
727
728 HELP: join
729 { $values { "seq" sequence } { "glue" sequence } { "newseq" sequence } }
730 { $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" } "." }
731 { $examples
732     "Join a list of strings:"
733     { $example "USING: sequences prettyprint ;"
734         "{ \"cat\" \"dog\" \"ant\" } \" \" join ."
735         "\"cat dog ant\""
736     }
737 }
738 { $notes "If the " { $snippet "glue" } " sequence is empty, this word calls " { $link concat-as } "." }
739 { $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" } "." } ;
740
741 HELP: join-as
742 { $values { "seq" sequence } { "glue" sequence } { "exemplar" sequence } { "newseq" sequence } }
743 { $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" } "." }
744 { $notes "If the " { $snippet "glue" } " sequence is empty, this word calls " { $link concat-as } "." }
745 { $examples
746     "Join a list of strings as a string buffer:"
747     { $example "USING: sequences prettyprint ;"
748         "{ \"a\" \"b\" \"c\" } \"1\" SBUF\" \"join-as ."
749         "SBUF\" a1b1c\""
750     }
751 }
752 { $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" } "." } ;
753
754 { join join-as concat concat-as } related-words
755
756 HELP: last
757 { $values { "seq" sequence } { "elt" object } }
758 { $description "Outputs the last element of a sequence." }
759 { $errors "Throws an error if the sequence is empty." } ;
760
761 HELP: pop*
762 { $values { "seq" "a resizable mutable sequence" } }
763 { $description "Removes the last element and shortens the sequence." }
764 { $side-effects "seq" }
765 { $errors "Throws an error if the sequence is empty." } ;
766
767 HELP: pop
768 { $values { "seq" "a resizable mutable sequence" } { "elt" object } }
769 { $description "Outputs the last element after removing it and shortening the sequence." }
770 { $side-effects "seq" }
771 { $errors "Throws an error if the sequence is empty." } ;
772
773 HELP: mismatch
774 { $values { "seq1" sequence } { "seq2" sequence } { "i" "an index" } }
775 { $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." } ;
776
777 HELP: flip
778 { $values { "matrix" "a sequence of equal-length sequences" } { "newmatrix" "a sequence of equal-length sequences" } }
779 { $description "Transposes the matrix; that is, rows become columns and columns become rows." }
780 { $examples { $example "USING: prettyprint sequences ;" "{ { 1 2 3 } { 4 5 6 } } flip ." "{ { 1 4 } { 2 5 } { 3 6 } }" } } ;
781
782 HELP: exchange
783 { $values { "m" "a non-negative integer" } { "n" "a non-negative integer" } { "seq" "a mutable sequence" } }
784 { $description "Exchanges the " { $snippet "m" } "th and " { $snippet "n" } "th elements of " { $snippet "seq" } "." } ;
785
786 HELP: reverse!
787 { $values { "seq" "a mutable sequence" } }
788 { $description "Reverses a sequence in-place and outputs that sequence." }
789 { $side-effects "seq" } ;
790
791 HELP: padding
792 { $values { "seq" sequence } { "n" "a non-negative integer" } { "elt" object } { "quot" { $quotation ( ... seq1 seq2 -- ... newseq ) } } { "newseq" "a new sequence" } }
793 { $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." } ;
794
795 HELP: pad-head
796 { $values { "seq" sequence } { "n" "a non-negative integer" } { "elt" object } { "padded" "a new sequence" } }
797 { $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" } "." }
798 { $examples { $example "USING: io sequences ;" "{ \"ab\" \"quux\" } [ 5 CHAR: - pad-head print ] each" "---ab\n-quux" } } ;
799
800 HELP: pad-tail
801 { $values { "seq" sequence } { "n" "a non-negative integer" } { "elt" object } { "padded" "a new sequence" } }
802 { $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" } "." }
803 { $examples { $example "USING: io sequences ;" "{ \"ab\" \"quux\" } [ 5 CHAR: - pad-tail print ] each" "ab---\nquux-" } } ;
804
805 HELP: sequence=
806 { $values { "seq1" sequence } { "seq2" sequence } { "?" boolean } }
807 { $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." } ;
808
809 HELP: reversed
810 { $class-description "A virtual sequence which presents a reversed view of an underlying sequence. New instances can be created by calling " { $link <reversed> } "." } ;
811
812 HELP: reverse
813 { $values { "seq" sequence } { "newseq" "a new sequence" } }
814 { $description "Outputs a new sequence having the same elements as " { $snippet "seq" } " but in reverse order." } ;
815
816 { reverse <reversed> reverse! } related-words
817
818 HELP: <reversed>
819 { $values { "seq" sequence } { "reversed" "a new sequence" } }
820 { $description "Creates an instance of the " { $link reversed } " class." }
821 { $see-also "virtual-sequences" } ;
822
823 HELP: slice-error
824 { $values { "str" "a reason" } }
825 { $description "Throws a " { $link slice-error } "." }
826 { $error-description "Thrown by " { $link <slice> } " if one of the following invalid conditions holds:"
827     { $list
828         "The start index is negative"
829         "The end index is greater than the length of the sequence"
830         "The start index is greater than the end index"
831     }
832 } ;
833
834 HELP: slice
835 { $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."
836 $nl
837 "Slices are mutable if the underlying sequence is mutable, and mutating a slice changes the underlying sequence. However, slices cannot be resized after creation." } ;
838
839 HELP: check-slice
840 { $values { "from" "a non-negative integer" } { "to" "a non-negative integer" } { "seq" sequence } }
841 { $description "Ensures that " { $snippet "m" } " is less than or equal to " { $snippet "m" } ", and that both indices are within bounds for " { $snippet "seq" } "." }
842 { $errors "Throws a " { $link slice-error } " if the preconditions are not met." } ;
843
844 HELP: collapse-slice
845 { $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 } }
846 { $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." }
847 ;
848
849 HELP: <slice>
850 { $values { "from" "a non-negative integer" } { "to" "a non-negative integer" } { "seq" sequence } { "slice" slice } }
851 { $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" } "." }
852 { $errors "Throws an error if " { $snippet "from" } " or " { $snippet "to" } " are out of bounds." }
853 { $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> } "." } ;
854
855 { <slice> subseq } related-words
856
857 HELP: repetition
858 { $class-description "A virtual sequence consisting of " { $snippet "elt" } " repeated " { $snippet "len" } " times. Repetitions are created by calling " { $link <repetition> } "." } ;
859
860 HELP: <repetition>
861 { $values { "len" "a non-negative integer" } { "elt" object } { "repetition" repetition } }
862 { $description "Creates a new " { $link repetition } "." }
863 { $examples
864     { $example "USING: arrays prettyprint sequences ;" "10 \"X\" <repetition> >array ." "{ \"X\" \"X\" \"X\" \"X\" \"X\" \"X\" \"X\" \"X\" \"X\" \"X\" }" }
865     { $example "USING: prettyprint sequences ;" "10 \"X\" <repetition> concat ." "\"XXXXXXXXXX\"" }
866 } ;
867 HELP: copy
868 { $values { "src" sequence } { "i" "an index in " { $snippet "dst" } } { "dst" "a mutable sequence" } }
869 { $description "Copies all elements of " { $snippet "src" } " to " { $snippet "dst" } ", with destination indices starting from " { $snippet "i" } ". Grows " { $snippet "dst" } " first if necessary." }
870 { $side-effects "dst" }
871 { $errors "An error is thrown if " { $snippet "dst" } " is not resizable, and not large enough to hold the copied elements." } ;
872
873 HELP: push-all
874 { $values { "src" sequence } { "dst" "a resizable mutable sequence" } }
875 { $description "Appends " { $snippet "src" } " to the end of " { $snippet "dst" } "." }
876 { $side-effects "dst" }
877 { $errors "Throws an error if " { $snippet "src" } " contains elements not permitted in " { $snippet "dst" } "." } ;
878
879 HELP: append
880 { $values { "seq1" sequence } { "seq2" sequence } { "newseq" sequence } }
881 { $description "Outputs a new sequence of the same type as " { $snippet "seq1" } " consisting of the elements of " { $snippet "seq1" } " followed by " { $snippet "seq2" } "." }
882 { $errors "Throws an error if " { $snippet "seq2" } " contains elements not permitted in sequences of the same class as " { $snippet "seq1" } "." }
883 { $examples
884     { $example "USING: prettyprint sequences ;"
885         "{ 1 2 } B{ 3 4 } append ."
886         "{ 1 2 3 4 }"
887     }
888     { $example "USING: prettyprint sequences strings ;"
889         "\"go\" \"ing\" append ."
890         "\"going\""
891     }
892 } ;
893
894 HELP: append-as
895 { $values { "seq1" sequence } { "seq2" sequence } { "exemplar" sequence } { "newseq" sequence } }
896 { $description "Outputs a new sequence of the same type as " { $snippet "exemplar" } " consisting of the elements of " { $snippet "seq1" } " followed by " { $snippet "seq2" } "." }
897 { $errors "Throws an error if " { $snippet "seq1" } " or " { $snippet "seq2" } " contain elements not permitted in sequences of the same class as " { $snippet "exemplar" } "." }
898 { $examples
899     { $example "USING: prettyprint sequences ;"
900         "{ 1 2 } B{ 3 4 } B{ } append-as ."
901         "B{ 1 2 3 4 }"
902     }
903     { $example "USING: prettyprint sequences strings ;"
904         "\"go\" \"ing\" SBUF\" \" append-as ."
905         "SBUF\" going\""
906     }
907 } ;
908
909 { append append-as append! 3append 3append-as push-all } related-words
910
911 HELP: prepend
912 { $values { "seq1" sequence } { "seq2" sequence } { "newseq" sequence } }
913 { $description "Outputs a new sequence of the same type as " { $snippet "seq1" } " consisting of the elements of " { $snippet "seq2" } " followed by " { $snippet "seq1" } "." }
914 { $errors "Throws an error if " { $snippet "seq2" } " contains elements not permitted in sequences of the same class as " { $snippet "seq1" } "." }
915 { $examples { $example "USING: prettyprint sequences ;"
916         "{ 1 2 } B{ 3 4 } prepend ."
917         "{ 3 4 1 2 }"
918     }
919     { $example "USING: prettyprint sequences strings ;"
920         "\"go\" \"car\" prepend ."
921         "\"cargo\""
922     }
923 } ;
924
925 HELP: prepend-as
926 { $values { "seq1" sequence } { "seq2" sequence } { "exemplar" sequence } { "newseq" sequence } }
927 { $description "Outputs a new sequence of the same type as " { $snippet "exemplar" } " consisting of the elements of " { $snippet "seq2" } " followed by " { $snippet "seq1" } "." }
928 { $errors "Throws an error if " { $snippet "seq1" } " or " { $snippet "seq2" } " contain elements not permitted in sequences of the same class as " { $snippet "exemplar" } "." }
929 { $examples
930     { $example "USING: prettyprint sequences ;"
931         "{ 3 4 } B{ 1 2 } B{ } prepend-as ."
932         "B{ 1 2 3 4 }"
933     }
934     { $example "USING: prettyprint sequences strings ;"
935         "\"ing\" \"go\" SBUF\" \" prepend-as ."
936         "SBUF\" going\""
937     }
938 } ;
939
940 { prepend prepend-as } related-words
941
942 HELP: 3append
943 { $values { "seq1" sequence } { "seq2" sequence } { "seq3" sequence } { "newseq" sequence } }
944 { $description "Outputs a new sequence consisting of the elements of " { $snippet "seq1" } ", " { $snippet "seq2" } " and " { $snippet "seq3" } " in turn." }
945 { $errors "Throws an error if " { $snippet "seq2" } " or " { $snippet "seq3" } " contain elements not permitted in sequences of the same class as " { $snippet "seq1" } "." }
946 { $examples
947     { $example "USING: prettyprint sequences ;"
948         "\"a\" \"b\" \"c\" 3append ."
949         "\"abc\""
950     }
951 } ;
952
953 HELP: 3append-as
954 { $values { "seq1" sequence } { "seq2" sequence } { "seq3" sequence } { "exemplar" sequence } { "newseq" sequence } }
955 { $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" } "." }
956 { $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" } "." }
957 { $examples
958     { $example "USING: prettyprint sequences ;"
959         "\"a\" \"b\" \"c\" SBUF\" \" 3append-as ."
960         "SBUF\" abc\""
961     }
962 } ;
963
964 HELP: surround
965 { $values { "seq1" sequence } { "seq2" sequence } { "seq3" sequence } { "newseq" sequence } }
966 { $description "Outputs a new sequence with " { $snippet "seq1" } " inserted between " { $snippet "seq2" } " and " { $snippet "seq3" } "." }
967 { $examples
968     { $example "USING: sequences prettyprint ;"
969                "\"sssssh\" \"(\" \")\" surround ."
970                "\"(sssssh)\""
971     }
972 } ;
973
974 HELP: glue
975 { $values { "seq1" sequence } { "seq2" sequence } { "seq3" sequence } { "newseq" sequence } }
976 { $description "Outputs a new sequence with " { $snippet "seq3" } " inserted between " { $snippet "seq1" } " and " { $snippet "seq2" } "." }
977 { $examples
978     { $example "USING: sequences prettyprint ;"
979                "\"a\" \"b\" \",\" glue ."
980                "\"a,b\""
981     }
982 } ;
983
984 HELP: subseq
985 { $values { "from" "a non-negative integer" } { "to" "a non-negative integer" } { "seq" sequence } { "subseq" "a new sequence" } }
986 { $description "Outputs a new sequence consisting of all elements starting from and including " { $snippet "from" } ", and up to but not including " { $snippet "to" } "." }
987 { $errors "Throws an error if " { $snippet "from" } " or " { $snippet "to" } " is out of bounds." } ;
988
989 HELP: subseq-as
990 { $values { "from" "a non-negative integer" } { "to" "a non-negative integer" } { "seq" sequence } { "exemplar" sequence } { "subseq" "a new sequence" } }
991 { $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" } "." }
992 { $errors "Throws an error if " { $snippet "from" } " or " { $snippet "to" } " is out of bounds." } ;
993
994 HELP: clone-like
995 { $values { "seq" sequence } { "exemplar" sequence } { "newseq" "a new sequence" } }
996 { $description "Outputs a newly-allocated sequence with the same elements as " { $snippet "seq" } " but of the same type as " { $snippet "exemplar" } "." }
997 { $notes "Unlike " { $link like } ", this word always creates a new sequence which never shares storage with the original." } ;
998
999 HELP: head-slice
1000 { $values { "seq" sequence } { "n" "a non-negative integer" } { "slice" "a slice" } }
1001 { $description "Outputs a virtual sequence sharing storage with the first " { $snippet "n" } " elements of the input sequence." }
1002 { $errors "Throws an error if the index is out of bounds." } ;
1003
1004 HELP: tail-slice
1005 { $values { "seq" sequence } { "n" "a non-negative integer" } { "slice" "a slice" } }
1006 { $description "Outputs a virtual sequence sharing storage with all elements from the " { $snippet "n" } "th index until the end of the input sequence." }
1007 { $errors "Throws an error if the index is out of bounds." } ;
1008
1009 HELP: but-last-slice
1010 { $values { "seq" sequence } { "slice" "a slice" } }
1011 { $description "Outputs a virtual sequence sharing storage with all but the last element of the input sequence." }
1012 { $errors "Throws an error on an empty sequence." } ;
1013
1014 HELP: rest-slice
1015 { $values { "seq" sequence } { "slice" "a slice" } }
1016 { $description "Outputs a virtual sequence sharing storage with all elements from the 1st index until the end of the input sequence." }
1017 { $notes "Equivalent to " { $snippet "1 tail" } }
1018 { $errors "Throws an error on an empty sequence." } ;
1019
1020 HELP: head-slice*
1021 { $values { "seq" sequence } { "n" "a non-negative integer" } { "slice" "a slice" } }
1022 { $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." }
1023 { $errors "Throws an error if the index is out of bounds." } ;
1024
1025 { head-slice head-slice* } related-words
1026
1027 HELP: tail-slice*
1028 { $values { "seq" sequence } { "n" "a non-negative integer" } { "slice" "a slice" } }
1029 { $description "Outputs a virtual sequence sharing storage with the last " { $snippet "n" } " elements of the input sequence." }
1030 { $errors "Throws an error if the index is out of bounds." } ;
1031
1032 { tail-slice tail-slice* } related-words
1033
1034 HELP: head
1035 { $values { "seq" sequence } { "n" "a non-negative integer" } { "headseq" "a new sequence" } }
1036 { $description "Outputs a new sequence consisting of the first " { $snippet "n" } " elements of the input sequence." }
1037 { $examples
1038     { $example "USING: sequences prettyprint ;"
1039         "{ 1 2 3 4 5 6 7 } 2 head ."
1040         "{ 1 2 }"
1041     }
1042     "When a sequence may not have enough elements:"
1043     { $example "USING: sequences prettyprint ;"
1044         "{ 1 2 } 5 short head ."
1045         "{ 1 2 }"
1046     }
1047 }
1048 { $errors "Throws an error if the index is out of bounds." } ;
1049
1050 HELP: tail
1051 { $values { "seq" sequence } { "n" "a non-negative integer" } { "tailseq" "a new sequence" } }
1052 { $description "Outputs a new sequence consisting of the input sequence with the first " { $snippet "n" } " items removed." }
1053 { $examples
1054     { $example "USING: sequences prettyprint ;"
1055         "{ 1 2 3 4 5 6 7 } 2 tail ."
1056         "{ 3 4 5 6 7 }"
1057     }
1058     "When a sequence may not have enough elements:"
1059     { $example "USING: sequences prettyprint ;"
1060         "{ 1 2 } 5 short tail ."
1061         "{ }"
1062     }
1063 }
1064 { $errors "Throws an error if the index is out of bounds." } ;
1065
1066 HELP: but-last
1067 { $values { "seq" sequence } { "headseq" "a new sequence" } }
1068 { $description "Outputs a new sequence consisting of the input sequence with the last item removed." }
1069 { $errors "Throws an error on an empty sequence." } ;
1070
1071 HELP: rest
1072 { $values { "seq" sequence } { "tailseq" "a new sequence" } }
1073 { $description "Outputs a new sequence consisting of the input sequence with the first item removed." }
1074 { $errors "Throws an error on an empty sequence." } ;
1075
1076 HELP: head*
1077 { $values { "seq" sequence } { "n" "a non-negative integer" } { "headseq" "a new sequence" } }
1078 { $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." }
1079 { $examples
1080     { $example "USING: sequences prettyprint ;"
1081         "{ 1 2 3 4 5 6 7 } 2 head* ."
1082         "{ 1 2 3 4 5 }"
1083     }
1084     "When a sequence may not have enough elements:"
1085     { $example "USING: sequences prettyprint ;"
1086         "{ 1 2 } 5 short head* ."
1087         "{ }"
1088     }
1089 }
1090 { $errors "Throws an error if the index is out of bounds." } ;
1091
1092 HELP: tail*
1093 { $values { "seq" sequence } { "n" "a non-negative integer" } { "tailseq" "a new sequence" } }
1094 { $description "Outputs a new sequence consisting of the last " { $snippet "n" } " elements of the input sequence." }
1095 { $examples
1096     { $example "USING: sequences prettyprint ;"
1097         "{ 1 2 3 4 5 6 7 } 2 tail* ."
1098         "{ 6 7 }"
1099     }
1100     "When a sequence may not have enough elements:"
1101     { $example "USING: sequences prettyprint ;"
1102         "{ 1 2 } 5 short tail* ."
1103         "{ 1 2 }"
1104     }
1105 }
1106 { $errors "Throws an error if the index is out of bounds." } ;
1107
1108 { tail tail* tail-slice tail-slice* } related-words
1109 { head head* head-slice head-slice* } related-words
1110 { cut cut* cut-slice } related-words
1111 { unclip unclip-slice unclip-last unclip-last-slice } related-words
1112 { first last but-last but-last-slice rest rest-slice } related-words
1113
1114 HELP: shorter?
1115 { $values { "seq1" sequence } { "seq2" sequence } { "?" boolean } }
1116 { $description "Tests if the length of " { $snippet "seq1" } " is smaller than the length of " { $snippet "seq2" } "." } ;
1117
1118 HELP: head?
1119 { $values { "seq" sequence } { "begin" sequence } { "?" boolean } }
1120 { $description "Tests if " { $snippet "seq" } " starts with " { $snippet "begin" } ". If " { $snippet "begin" } " is longer than " { $snippet "seq" } ", this word outputs " { $link f } "." }
1121 { $examples
1122   { $example
1123     "USING: prettyprint sequences ;"
1124     "{ \"accept\" \"adept\" \"advance\" \"advice\" \"affect\" } [ \"ad\" head? ] filter ."
1125     "{ \"adept\" \"advance\" \"advice\" }"
1126   }
1127 } ;
1128
1129 HELP: tail?
1130 { $values { "seq" sequence } { "end" sequence } { "?" boolean } }
1131 { $description "Tests if " { $snippet "seq" } " ends with " { $snippet "end" } ". If " { $snippet "end" } " is longer than " { $snippet "seq" } ", this word outputs " { $link f } "." } ;
1132
1133 { remove remove-nth remove-eq remove-eq! remove! remove-nth! } related-words
1134
1135 HELP: cut-slice
1136 { $values { "seq" sequence } { "n" "a non-negative integer" } { "before-slice" sequence } { "after-slice" "a slice" } }
1137 { $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." }
1138 { $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." } ;
1139
1140 HELP: cut
1141 { $values { "seq" sequence } { "n" "a non-negative integer" } { "before" sequence } { "after" sequence } }
1142 { $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" } "." }
1143 { $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." } ;
1144
1145 HELP: cut*
1146 { $values { "seq" sequence } { "n" "a non-negative integer" } { "before" sequence } { "after" sequence } }
1147 { $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" } "." } ;
1148
1149 HELP: subseq-start-from
1150 { $values { "subseq" sequence } { "seq" sequence } { "n" "a start index" } { "i" "a start index" } }
1151 { $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 } "." } ;
1152
1153 HELP: subseq-start
1154 { $values { "subseq" sequence } { "seq" sequence } { "i" "a start index" } }
1155 { $description "Outputs the start index of the first contiguous subsequence equal to " { $snippet "subseq" } ", or " { $link f } " if no matching subsequence is found." } ;
1156
1157 HELP: subseq?
1158 { $values { "subseq" sequence } { "seq" sequence } { "?" boolean } }
1159 { $description "Tests if " { $snippet "seq" } " contains the elements of " { $snippet "subseq" } " as a contiguous subsequence." } ;
1160
1161 HELP: drop-prefix
1162 { $values { "seq1" sequence } { "seq2" sequence } { "slice1" "a slice" } { "slice2" "a slice" } }
1163 { $description "Outputs a pair of virtual sequences with the common prefix of " { $snippet "seq1" } " and " { $snippet "seq2" } " removed." } ;
1164
1165 HELP: unclip
1166 { $values { "seq" sequence } { "rest" sequence } { "first" object } }
1167 { $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." }
1168 { $examples
1169     { $example "USING: prettyprint sequences ;" "{ 1 2 3 } unclip suffix ." "{ 2 3 1 }" }
1170 } ;
1171
1172 HELP: unclip-slice
1173 { $values { "seq" sequence } { "rest-slice" slice } { "first" object } }
1174 { $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." }
1175 { $examples { $example "USING: math.order prettyprint sequences ;" "{ 3 -1 -10 5 7 } unclip-slice [ min ] reduce ." "-10" } } ;
1176
1177 HELP: unclip-last
1178 { $values { "seq" sequence } { "butlast" sequence } { "last" object } }
1179 { $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." }
1180 { $examples
1181     { $example "USING: prettyprint sequences ;" "{ 1 2 3 } unclip-last prefix ." "{ 3 1 2 }" }
1182 } ;
1183
1184 HELP: unclip-last-slice
1185 { $values { "seq" sequence } { "butlast-slice" slice } { "last" object } }
1186 { $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." } ;
1187
1188 HELP: sum
1189 { $values { "seq" { $sequence number } } { "n" number } }
1190 { $description "Outputs the sum of all elements of " { $snippet "seq" } ". Outputs zero given an empty sequence." } ;
1191
1192 HELP: product
1193 { $values { "seq" { $sequence number } } { "n" number } }
1194 { $description "Outputs the product of all elements of " { $snippet "seq" } ". Outputs one given an empty sequence." } ;
1195
1196 HELP: infimum
1197 { $values { "seq" sequence } { "elt" object } }
1198 { $description "Outputs the least element of " { $snippet "seq" } "." }
1199 { $errors "Throws an error if the sequence is empty." } ;
1200
1201 HELP: supremum
1202 { $values { "seq" sequence } { "elt" object } }
1203 { $description "Outputs the greatest element of " { $snippet "seq" } "." }
1204 { $errors "Throws an error if the sequence is empty." } ;
1205
1206 { min max supremum infimum } related-words
1207
1208 HELP: shortest
1209 { $values { "seq" sequence } { "elt" object } }
1210 { $description "Outputs the shortest element of " { $snippet "seq" } "." } ;
1211
1212 HELP: longest
1213 { $values { "seq" sequence } { "elt" object } }
1214 { $description "Outputs the longest element of " { $snippet "seq" } "." } ;
1215
1216 { shortest longest } related-words
1217
1218 HELP: produce
1219 { $values { "pred" { $quotation ( ..a -- ..b ? ) } } { "quot" { $quotation ( ..b -- ..a obj ) } } { "seq" sequence } }
1220 { $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." }
1221 { $examples
1222     "The following example divides a number by two until we reach zero, and accumulates intermediate results:"
1223     { $example "USING: kernel math prettyprint sequences ;" "1337 [ dup 0 > ] [ 2/ dup ] produce nip ." "{ 668 334 167 83 41 20 10 5 2 1 0 }" }
1224     "The following example collects random numbers as long as they are greater than 1:"
1225     { $unchecked-example "USING: kernel prettyprint random sequences ;" "[ 10 random dup 1 > ] [ ] produce nip ." "{ 8 2 2 9 }" }
1226 } ;
1227
1228 HELP: produce-as
1229 { $values { "pred" { $quotation ( ..a -- ..b ? ) } } { "quot" { $quotation ( ..b -- ..a obj ) } } { "exemplar" sequence } { "seq" sequence } }
1230 { $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." }
1231 { $examples "See " { $link produce } " for examples." } ;
1232
1233 HELP: map-sum
1234 { $values { "seq" sequence } { "quot" quotation } { "n" number } }
1235 { $description "Like " { $snippet "map sum" } ", but without creating an intermediate sequence." }
1236 { $examples
1237     { $example
1238         "USING: math math.ranges sequences prettyprint ;"
1239         "100 [1,b] [ sq ] map-sum ."
1240         "338350"
1241     }
1242 } ;
1243
1244 HELP: count
1245 { $values { "seq" sequence } { "quot" quotation } { "n" integer } }
1246 { $description "Efficiently returns the number of elements that the predicate quotation matches." }
1247 { $examples
1248     { $example
1249         "USING: math math.ranges sequences prettyprint ;"
1250         "100 [1,b] [ even? ] count ."
1251         "50"
1252     }
1253 } ;
1254
1255 HELP: selector
1256 { $values
1257      { "quot" { $quotation ( ... elt -- ... ? ) } }
1258      { "selector" { $quotation ( ... elt -- ... ) } } { "accum" vector } }
1259 { $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." }
1260 { $examples
1261     { $example "! Find all the even numbers:" "USING: prettyprint sequences math kernel ;"
1262                "10 <iota> [ even? ] selector [ each ] dip ."
1263                "V{ 0 2 4 6 8 }"
1264     }
1265 }
1266 { $notes "Used to implement the " { $link filter } " word. Compare this word with " { $link collector } ", which is an unfiltering version." } ;
1267
1268 HELP: trim-head
1269 { $values
1270      { "seq" sequence } { "quot" quotation }
1271      { "newseq" sequence } }
1272 { $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." }
1273 { $examples
1274     { $example "USING: prettyprint math sequences ;"
1275                "{ 0 0 1 2 3 0 0 } [ zero? ] trim-head ."
1276                "{ 1 2 3 0 0 }"
1277     }
1278 } ;
1279
1280 HELP: trim-head-slice
1281 { $values
1282      { "seq" sequence } { "quot" quotation }
1283      { "slice" slice } }
1284 { $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." }
1285 { $examples
1286     { $example "USING: prettyprint math sequences ;"
1287                "{ 0 0 1 2 3 0 0 } [ zero? ] trim-head-slice ."
1288                "T{ slice { from 2 } { to 7 } { seq { 0 0 1 2 3 0 0 } } }"
1289     }
1290 } ;
1291
1292 HELP: trim-tail
1293 { $values
1294      { "seq" sequence } { "quot" quotation }
1295      { "newseq" sequence } }
1296 { $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." }
1297 { $examples
1298     { $example "USING: prettyprint math sequences ;"
1299                "{ 0 0 1 2 3 0 0 } [ zero? ] trim-tail ."
1300                "{ 0 0 1 2 3 }"
1301     }
1302 } ;
1303
1304 HELP: trim-tail-slice
1305 { $values
1306      { "seq" sequence } { "quot" quotation }
1307      { "slice" slice } }
1308 { $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." }
1309 { $examples
1310     { $example "USING: prettyprint math sequences ;"
1311                "{ 0 0 1 2 3 0 0 } [ zero? ] trim-tail-slice ."
1312                "T{ slice { to 5 } { seq { 0 0 1 2 3 0 0 } } }"
1313     }
1314 } ;
1315
1316 HELP: trim
1317 { $values
1318      { "seq" sequence } { "quot" quotation }
1319      { "newseq" sequence } }
1320 { $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." }
1321 { $examples
1322     { $example "USING: prettyprint math sequences ;"
1323                "{ 0 0 1 2 3 0 0 } [ zero? ] trim ."
1324                "{ 1 2 3 }"
1325     }
1326 } ;
1327
1328 HELP: trim-slice
1329 { $values
1330      { "seq" sequence } { "quot" quotation }
1331      { "slice" slice } }
1332 { $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." }
1333 { $examples
1334     { $example "USING: prettyprint math sequences ;"
1335                "{ 0 0 1 2 3 0 0 } [ zero? ] trim-slice ."
1336                "T{ slice { from 2 } { to 5 } { seq { 0 0 1 2 3 0 0 } } }"
1337     }
1338 } ;
1339
1340 { trim trim-slice trim-head trim-head-slice trim-tail trim-tail-slice } related-words
1341
1342 HELP: sift
1343 { $values
1344      { "seq" sequence }
1345      { "newseq" sequence } }
1346  { $description "Outputs a new sequence with all instances of " { $link f } " removed." }
1347  { $examples
1348     { $example "USING: prettyprint sequences ;"
1349         "{ \"a\" 3 { } f } sift ."
1350         "{ \"a\" 3 { } }"
1351     }
1352 } ;
1353
1354 HELP: harvest
1355 { $values
1356      { "seq" sequence }
1357      { "newseq" sequence } }
1358 { $description "Outputs a new sequence with all empty sequences removed." }
1359 { $examples
1360     { $example "USING: prettyprint sequences ;"
1361                "{ { } { 2 3 } { 5 } { } } harvest ."
1362                "{ { 2 3 } { 5 } }"
1363     }
1364 } ;
1365
1366 { filter filter-as filter! reject reject-as reject! sift harvest } related-words
1367
1368 HELP: set-first
1369 { $values
1370      { "first" object } { "seq" sequence } }
1371 { $description "Sets the first element of a sequence." }
1372 { $examples
1373     { $example "USING: prettyprint kernel sequences ;"
1374         "{ 1 2 3 4 } 5 over set-first ."
1375         "{ 5 2 3 4 }"
1376     }
1377 } ;
1378
1379 HELP: set-second
1380 { $values
1381      { "second" object } { "seq" sequence } }
1382 { $description "Sets the second element of a sequence." }
1383 { $examples
1384     { $example "USING: prettyprint kernel sequences ;"
1385         "{ 1 2 3 4 } 5 over set-second ."
1386         "{ 1 5 3 4 }"
1387     }
1388 } ;
1389
1390 HELP: set-third
1391 { $values
1392      { "third" object } { "seq" sequence } }
1393 { $description "Sets the third element of a sequence." }
1394 { $examples
1395     { $example "USING: prettyprint kernel sequences ;"
1396         "{ 1 2 3 4 } 5 over set-third ."
1397         "{ 1 2 5 4 }"
1398     }
1399 } ;
1400
1401 HELP: set-fourth
1402 { $values
1403      { "fourth" object } { "seq" sequence } }
1404 { $description "Sets the fourth element of a sequence." }
1405 { $examples
1406     { $example "USING: prettyprint kernel sequences ;"
1407         "{ 1 2 3 4 } 5 over set-fourth ."
1408         "{ 1 2 3 5 }"
1409     }
1410 } ;
1411
1412 { set-first set-second set-third set-fourth } related-words
1413
1414 HELP: replicate
1415 { $values
1416      { "len" integer } { "quot" { $quotation ( ... -- ... newelt ) } }
1417      { "newseq" sequence } }
1418      { $description "Calls the quotation " { $snippet "len" } " times, collecting results into a new array." }
1419 { $examples
1420     { $unchecked-example "USING: kernel prettyprint random sequences ;"
1421         "5 [ 100 random ] replicate ."
1422         "{ 52 10 45 81 30 }"
1423     }
1424 } ;
1425
1426 HELP: replicate-as
1427 { $values
1428      { "len" integer } { "quot" { $quotation ( ... -- ... newelt ) } } { "exemplar" sequence }
1429      { "newseq" sequence } }
1430  { $description "Calls the quotation " { $snippet "len" } " times, collecting results into a new sequence of the same type as the exemplar sequence." }
1431 { $examples
1432     { $unchecked-example "USING: prettyprint kernel sequences ;"
1433         "5 [ 100 random ] B{ } replicate-as ."
1434         "B{ 44 8 2 33 18 }"
1435     }
1436 } ;
1437
1438 { replicate replicate-as } related-words
1439
1440 HELP: partition
1441 { $values
1442      { "seq" sequence } { "quot" quotation }
1443      { "trueseq" sequence } { "falseseq" sequence } }
1444      { $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" } "." }
1445 { $examples
1446     { $example "USING: prettyprint kernel math sequences ;"
1447         "{ 1 2 3 4 5 } [ even? ] partition [ . ] bi@"
1448         "{ 2 4 }\n{ 1 3 5 }"
1449     }
1450 } ;
1451
1452 HELP: virtual-exemplar
1453 { $values
1454      { "seq" sequence }
1455      { "seq'" sequence } }
1456 { $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 } "." } ;
1457
1458 HELP: virtual@
1459 { $values
1460      { "n" integer } { "seq" sequence }
1461      { "n'" integer } { "seq'" sequence } }
1462 { $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." } ;
1463
1464 HELP: 2map-reduce
1465 { $values
1466      { "seq1" sequence } { "seq2" sequence } { "map-quot" { $quotation ( ..a elt1 elt2 -- ..b intermediate ) } } { "reduce-quot" { $quotation ( ..b prev intermediate -- ..a next ) } }
1467      { "result" object } }
1468  { $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." }
1469 { $errors "Throws an error if the sequence is empty." }
1470 { $examples { $example "USING: sequences prettyprint math ;"
1471     "{ 10 30 50 } { 200 400 600 } [ + ] [ + ] 2map-reduce ."
1472     "1290"
1473 } } ;
1474
1475 HELP: 2selector
1476 { $values
1477      { "quot" quotation }
1478      { "selector" quotation } { "accum1" vector } { "accum2" vector } }
1479 { $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." } ;
1480
1481 HELP: collector
1482 { $values
1483      { "quot" quotation }
1484      { "quot'" quotation } { "vec" vector } }
1485 { $description "Creates a new quotation that pushes its result to a vector and outputs that vector on the stack." }
1486 { $examples { $example "USING: sequences prettyprint kernel math ;"
1487     "{ 1 2 } [ 30 + ] collector [ each ] dip ."
1488     "V{ 31 32 }"
1489 } } ;
1490
1491 HELP: binary-reduce
1492 { $values
1493      { "seq" sequence } { "start" integer } { "quot" { $quotation ( elt1 elt2 -- newelt ) } }
1494      { "value" object } }
1495 { $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 } "." }
1496 { $examples "Computing factorial:"
1497     { $example "USING: prettyprint sequences math ;"
1498     "40 <iota> rest-slice 1 [ * ] binary-reduce ."
1499     "20397882081197443358640281739902897356800000000" }
1500 } ;
1501
1502 HELP: follow
1503 { $values
1504      { "obj" object } { "quot" { $quotation ( ... prev -- ... result/f ) } }
1505      { "seq" sequence } }
1506 { $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." }
1507 { $examples "Get random numbers until zero is reached:"
1508     { $unchecked-example
1509     "USING: random sequences prettyprint math ;"
1510     "100 [ random [ f ] when-zero ] follow ."
1511     "{ 100 86 34 32 24 11 7 2 }"
1512 } } ;
1513
1514 HELP: halves
1515 { $values
1516      { "seq" sequence }
1517      { "first-slice" slice } { "second-slice" slice } }
1518 { $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." }
1519 { $examples { $example "USING: arrays sequences prettyprint kernel ;"
1520     "{ 1 2 3 4 5 } halves [ >array . ] bi@"
1521     "{ 1 2 }\n{ 3 4 5 }"
1522 } } ;
1523
1524 HELP: indices
1525 { $values
1526      { "obj" object } { "seq" sequence }
1527      { "indices" sequence } }
1528 { $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." }
1529 { $examples { $example "USING: sequences prettyprint ;"
1530     "2 { 2 4 2 6 2 8 2 10 } indices ."
1531     "V{ 0 2 4 6 }"
1532 } } ;
1533
1534 HELP: insert-nth
1535 { $values
1536      { "elt" object } { "n" integer } { "seq" sequence }
1537      { "seq'" sequence } }
1538 { $description "Creates a new sequence where the " { $snippet "n" } "th index is set to the input object." }
1539 { $examples { $example "USING: prettyprint sequences ;"
1540     "40 3 { 10 20 30 50 } insert-nth ."
1541     "{ 10 20 30 40 50 }"
1542 } } ;
1543
1544 HELP: map-reduce
1545 { $values
1546      { "seq" sequence } { "map-quot" { $quotation ( ..a elt -- ..b intermediate ) } } { "reduce-quot" { $quotation ( ..b prev intermediate -- ..a next ) } }
1547      { "result" object } }
1548 { $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." }
1549 { $errors "Throws an error if the sequence is empty." }
1550 { $examples { $example "USING: sequences prettyprint math ;"
1551     "{ 1 3 5 } [ sq ] [ + ] map-reduce ."
1552     "35"
1553 } } ;
1554
1555 HELP: new-like
1556 { $values
1557      { "len" integer } { "exemplar" "an exemplar sequence" } { "quot" quotation }
1558      { "seq" sequence } }
1559 { $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." } ;
1560
1561 HELP: push-either
1562 { $values
1563      { "elt" object } { "quot" quotation } { "accum1" vector } { "accum2" vector } }
1564 { $description "Pushes the input object onto one of the accumulators; the first if the quotation yields true, the second if false." } ;
1565
1566 HELP: sequence-hashcode
1567 { $values
1568      { "n" integer } { "seq" sequence }
1569      { "x" integer } }
1570 { $description "Iterates over a sequence, computes a hashcode with " { $link hashcode* } " for each element, and combines them using " { $link sequence-hashcode-step } "." } ;
1571
1572 HELP: sequence-hashcode-step
1573 { $values
1574      { "oldhash" integer } { "newpart" integer }
1575      { "newhash" integer } }
1576 { $description "An implementation word that computes a running hashcode of a sequence using some bit-twiddling. The resulting hashcode is always a fixnum." } ;
1577
1578 HELP: short
1579 { $values
1580      { "seq" sequence } { "n" integer } { "n'" integer } }
1581 { $description "Returns the input sequence and its length or " { $snippet "n" } ", whichever is less." }
1582 { $examples { $example "USING: sequences kernel prettyprint ;"
1583     "\"abcd\" 3 short [ . ] bi@"
1584     "\"abcd\"\n3"
1585 } } ;
1586
1587 HELP: shorten
1588 { $values
1589      { "n" integer } { "seq" sequence } }
1590 { $description "Shortens a " { $link "growable" } " sequence to be " { $snippet "n" } " elements long." }
1591 { $examples { $example "USING: sequences prettyprint kernel ;"
1592     "V{ 1 2 3 4 5 } 3 over shorten ."
1593     "V{ 1 2 3 }"
1594 } } ;
1595
1596 HELP: <iota>
1597 { $values { "n" integer } { "iota" iota } }
1598 { $description "Creates an immutable virtual sequence containing the integers from 0 to " { $snippet "n-1" } "." }
1599 { $examples
1600   { $example
1601     "USING: math sequences prettyprint ;"
1602     "3 <iota> [ sq ] map ."
1603     "{ 0 1 4 }"
1604   }
1605 } ;
1606
1607 HELP: assert-sequence=
1608 { $values
1609     { "a" sequence } { "b" sequence }
1610 }
1611 { $description "Throws an error if all the elements of two sequences, taken pairwise, are not equal." }
1612 { $notes "The sequences need not be of the same type." }
1613 { $examples
1614   { $code
1615     "USING: prettyprint sequences ;"
1616     "{ 1 2 3 } V{ 1 2 3 } assert-sequence="
1617   }
1618 } ;
1619
1620 HELP: cartesian-each
1621 { $values { "seq1" sequence } { "seq2" sequence } { "quot" { $quotation ( ... elt1 elt2 -- ... ) } } }
1622 { $description "Applies the quotation to every possible pairing of elements from the two sequences." } ;
1623
1624 HELP: cartesian-map
1625 { $values { "seq1" sequence } { "seq2" sequence } { "quot" { $quotation ( ... elt1 elt2 -- ... newelt ) } } { "newseq" "a new sequence of sequences" } }
1626 { $description "Applies the quotation to every possible pairing of elements from the two sequences, collecting results into a new sequence of sequences." } ;
1627
1628 HELP: cartesian-product
1629 { $values { "seq1" sequence } { "seq2" sequence } { "newseq" "a new sequence of sequences of pairs" } }
1630 { $description "Outputs a sequence of all possible pairings of elements from the two sequences." }
1631 { $examples
1632     { $example
1633         "USING: prettyprint sequences ;"
1634         "{ 1 2 } { 3 4 } cartesian-product ."
1635         "{ { { 1 3 } { 1 4 } } { { 2 3 } { 2 4 } } }"
1636     }
1637 } ;
1638
1639 ARTICLE: "sequences-unsafe" "Unsafe sequence operations"
1640 "The " { $link nth-unsafe } " and " { $link set-nth-unsafe } " sequence protocol bypasses bounds checks for increased performance."
1641 $nl
1642 "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."
1643 $nl
1644 "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."
1645 $nl
1646 "The justification for this is that the VM should not crash if a resizable sequence is resized during the execution of an iteration combinator."
1647 $nl
1648 "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 } "." ;
1649
1650 ARTICLE: "sequence-protocol" "Sequence protocol"
1651 "All sequences must be instances of a mixin class:"
1652 { $subsections sequence sequence? }
1653 "All sequences must know their length:"
1654 { $subsections length }
1655 "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:"
1656 { $subsections nth nth-unsafe }
1657 "Note that sequences are always indexed starting from zero."
1658 $nl
1659 "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:"
1660 { $subsections set-nth set-nth-unsafe }
1661 "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."
1662 $nl
1663 "The following two generic words are optional, as not all sequences are resizable:"
1664 { $subsections set-length lengthen }
1665 "An optional generic word for creating sequences of the same class as a given sequence:"
1666 { $subsections like }
1667 "Optional generic words for optimization purposes:"
1668 { $subsections new-sequence new-resizable }
1669 { $see-also "sequences-unsafe" } ;
1670
1671 ARTICLE: "virtual-sequences-protocol" "Virtual sequence protocol"
1672 "Virtual sequences must know their length:"
1673 { $subsections length }
1674 "An exemplar of the underlying storage:"
1675 { $subsections virtual-exemplar }
1676 "The index and the underlying storage where the value is located:"
1677 { $subsections virtual@ } ;
1678
1679 ARTICLE: "virtual-sequences" "Virtual sequences"
1680 "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."
1681 $nl
1682 "Implementations include the following:"
1683 { $subsections reversed slice }
1684 "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."
1685 { $see-also "sequences-integers" } ;
1686
1687 ARTICLE: "sequences-integers" "Counted loops"
1688 "A virtual sequence is defined for iterating over integers from zero."
1689 { $subsection <iota> }
1690 "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 } ":"
1691 { $example "USING: sequences prettyprint ; 3 <iota> [ . ] each" "0\n1\n2" }
1692 "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 } "."
1693 $nl
1694 "Combinators that produce new sequences, such as " { $link map } ", will output an array if the input is an instance of " { $link <iota> } "."
1695 $nl
1696 "More elaborate counted loops can be performed with " { $link "math.ranges" } "." ;
1697
1698 ARTICLE: "sequences-if" "Control flow with sequences"
1699 "To reduce the boilerplate of checking if a sequence is empty, several combinators are provided."
1700 $nl
1701 "Checking if a sequence is empty:"
1702 { $subsections if-empty when-empty unless-empty } ;
1703
1704 ARTICLE: "sequences-access" "Accessing sequence elements"
1705 "Element access by index, without raising exceptions:"
1706 { $subsections ?nth }
1707 "Concise way of extracting one of the first four elements:"
1708 { $subsections first second third fourth ?first ?second }
1709 "Extracting the last element:"
1710 { $subsections last ?last }
1711 "Unpacking sequences:"
1712 { $subsections first2 first3 first4 }
1713 { $see-also nth } ;
1714
1715 ARTICLE: "sequences-add-remove" "Adding and removing sequence elements"
1716 "Adding elements:"
1717 { $subsections prefix suffix insert-nth }
1718 "Removing elements:"
1719 { $subsections remove remove-eq remove-nth } ;
1720
1721 ARTICLE: "sequences-reshape" "Reshaping sequences"
1722 "A " { $emphasis "repetition" } " is a virtual sequence consisting of a single element repeated multiple times:"
1723 { $subsections repetition <repetition> }
1724 "Reversing a sequence:"
1725 { $subsections reverse }
1726 "A " { $emphasis "reversal" } " presents a reversed view of an underlying sequence:"
1727 { $subsections reversed <reversed> }
1728 "Transposing a matrix:"
1729 { $subsections flip } ;
1730
1731 ARTICLE: "sequences-appending" "Appending sequences"
1732 "Basic append operations:"
1733 { $subsections
1734     append
1735     append-as
1736     prepend
1737     3append
1738     3append-as
1739     surround
1740     glue
1741 }
1742 "Collapse a sequence unto itself:"
1743 { $subsections concat join }
1744 "A pair of words useful for aligning strings:"
1745 { $subsections pad-head pad-tail } ;
1746
1747 ARTICLE: "sequences-slices" "Subsequences and slices"
1748 "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."
1749 $nl
1750 "Some general guidelines for choosing between the two approaches:"
1751 { $list
1752   "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."
1753   { "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." }
1754   "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."
1755   "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."
1756 }
1757 { $heading "Subsequence operations" }
1758 "Extracting a subsequence:"
1759 { $subsections
1760     subseq
1761     subseq-as
1762     head
1763     tail
1764     head*
1765     tail*
1766 }
1767 "Removing the first or last element:"
1768 { $subsections rest but-last }
1769 "Taking a sequence apart into a head and a tail:"
1770 { $subsections
1771     unclip
1772     unclip-last
1773     cut
1774     cut*
1775 }
1776 { $heading "Slice operations" }
1777 "The slice data type:"
1778 { $subsections slice slice? }
1779 "Extracting a slice:"
1780 { $subsections
1781     <slice>
1782     head-slice
1783     tail-slice
1784     head-slice*
1785     tail-slice*
1786 }
1787 "Removing the first or last element:"
1788 { $subsections rest-slice but-last-slice }
1789 "Taking a sequence apart into a head and a tail:"
1790 { $subsections unclip-slice unclip-last-slice cut-slice }
1791 "Replacing slices with new elements:"
1792 { $subsections replace-slice } ;
1793
1794 ARTICLE: "sequences-combinators" "Sequence combinators"
1795 "Iteration:"
1796 { $subsections
1797     each
1798     each-index
1799     reduce
1800     interleave
1801 }
1802 "Mapping:"
1803 { $subsections
1804     map
1805     map-as
1806     map-index
1807     map-index-as
1808     map-reduce
1809     accumulate
1810     accumulate-as
1811     accumulate*
1812     accumulate*-as
1813 }
1814 "Filtering:"
1815 { $subsections
1816     filter
1817     filter-as
1818     partition
1819 }
1820 "Generating:"
1821 { $subsections
1822     replicate
1823     replicate-as
1824     produce
1825     produce-as
1826 }
1827 "Testing if a sequence contains elements satisfying a predicate:"
1828 { $subsections
1829     any?
1830     all?
1831 }
1832 { $heading "Related Articles" }
1833 { $subsections
1834     "sequence-2combinators"
1835     "sequence-3combinators"
1836 } ;
1837
1838 ARTICLE: "sequence-2combinators" "Pair-wise sequence combinators"
1839 "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."
1840 { $subsections
1841     2each
1842     2reduce
1843     2map
1844     2map-as
1845     2map-reduce
1846     2all?
1847 } ;
1848
1849 ARTICLE: "sequence-3combinators" "Triple-wise sequence combinators"
1850 "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."
1851 { $subsections 3each 3map 3map-as } ;
1852
1853 ARTICLE: "sequences-tests" "Testing sequences"
1854 "Testing for an empty sequence:"
1855 { $subsections empty? }
1856 "Testing indices:"
1857 { $subsections bounds-check? }
1858 "Testing if a sequence contains an object:"
1859 { $subsections member? member-eq? }
1860 "Testing if a sequence contains a subsequence:"
1861 { $subsections head? tail? subseq? } ;
1862
1863 ARTICLE: "sequences-search" "Searching sequences"
1864 "Finding the index of an element:"
1865 { $subsections
1866     index
1867     index-from
1868     last-index
1869     last-index-from
1870 }
1871 "Finding the start of a subsequence:"
1872 { $subsections subseq-start subseq-start-from }
1873 "Finding the index of an element satisfying a predicate:"
1874 { $subsections
1875     find
1876     find-from
1877     find-last
1878     find-last-from
1879     map-find
1880 } ;
1881
1882 ARTICLE: "sequences-trimming" "Trimming sequences"
1883 "Trimming words:"
1884 { $subsections trim trim-head trim-tail }
1885 "Potentially more efficient trim:"
1886 { $subsections trim-slice trim-head-slice trim-tail-slice } ;
1887
1888 ARTICLE: "sequences-destructive-discussion" "When to use destructive operations"
1889 "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:"
1890 { $list
1891     "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."
1892     { "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." }
1893 }
1894 "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." ;
1895
1896 ARTICLE: "sequences-destructive" "Destructive sequence operations"
1897 "Many operations have destructive variants that side effect an input sequence, instead of creating a new sequence:"
1898 { $table
1899     { "Constructive" "Destructive" }
1900     { { $link suffix } { $link suffix! } }
1901     { { $link remove } { $link remove! } }
1902     { { $link remove-eq } { $link remove-eq! } }
1903     { { $link remove-nth } { $link remove-nth! } }
1904     { { $link reverse } { $link reverse! } }
1905     { { $link append } { $link append! } }
1906     { { $link map } { $link map! } }
1907     { { $link accumulate } { $link accumulate! } }
1908     { { $link accumulate* } { $link accumulate*! } }
1909     { { $link filter } { $link filter! } }
1910 }
1911 "Changing elements:"
1912 { $subsections map! accumulate! accumulate*! change-nth }
1913 "Deleting elements:"
1914 { $subsections
1915     remove!
1916     remove-eq!
1917     remove-nth!
1918     delete-slice
1919     delete-all
1920     filter!
1921 }
1922 "Adding elements:"
1923 { $subsections
1924     suffix!
1925     append!
1926 }
1927 "Other destructive words:"
1928 { $subsections
1929     reverse!
1930     move
1931     exchange
1932     copy
1933 }
1934 { $heading "Related Articles" }
1935 { $subsections
1936     "sequences-destructive-discussion"
1937     "sequences-stacks"
1938 }
1939 { $see-also set-nth push push-all pop pop* } ;
1940
1941 ARTICLE: "sequences-stacks" "Treating sequences as stacks"
1942 "The classical stack operations, modifying a sequence in place:"
1943 { $subsections push push-all pop pop* }
1944 { $see-also empty? } ;
1945
1946 ARTICLE: "sequences-comparing" "Comparing sequences"
1947 "Element equality testing:"
1948 { $subsections
1949     sequence=
1950     mismatch
1951     drop-prefix
1952     assert-sequence=
1953 }
1954 "The " { $link <=> } " generic word performs lexicographic comparison when applied to sequences." ;
1955
1956 ARTICLE: "sequences-f" "The f object as a sequence"
1957 "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." ;
1958
1959 ARTICLE: "sequences-combinator-implementation" "Implementing sequence combinators"
1960 "Creating a new sequence unconditionally:"
1961 { $subsections
1962     collector
1963     collector-as
1964     collector-for-as
1965 }
1966 "Creating a new sequence conditionally:"
1967 { $subsections
1968     selector
1969     selector-as
1970     2selector
1971 } ;
1972
1973 ARTICLE: "sequences-cartesian" "Cartesian product operations"
1974 "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."
1975 $nl
1976 "Combinators which pair every element of the first sequence with every element of the second:"
1977 { $subsections
1978     cartesian-each
1979     cartesian-map
1980 }
1981 "Computing the cartesian product of two sequences:"
1982 { $subsections
1983     cartesian-product
1984 } ;
1985
1986 ARTICLE: "sequences" "Sequence operations"
1987 "A " { $emphasis "sequence" } " is a finite, linearly-ordered collection of elements. Words for working with sequences are in the " { $vocab-link "sequences" } " vocabulary."
1988 $nl
1989 "Sequences implement a protocol:"
1990 { $subsections
1991     "sequence-protocol"
1992     "sequences-f"
1993 }
1994 "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" } "."
1995 { $subsections
1996     "sequences-access"
1997     "sequences-combinators"
1998     "sequences-add-remove"
1999     "sequences-appending"
2000     "sequences-slices"
2001     "sequences-reshape"
2002     "sequences-tests"
2003     "sequences-search"
2004     "sequences-comparing"
2005     "sequences-split"
2006     "grouping"
2007     "sequences-destructive"
2008     "sequences-stacks"
2009     "sequences-sorting"
2010     "binary-search"
2011     "sets"
2012     "sequences-trimming"
2013     "sequences-cartesian"
2014     "sequences.deep"
2015 }
2016 "Using sequences for looping:"
2017 { $subsections
2018     "sequences-integers"
2019     "math.ranges"
2020 }
2021 "Using sequences for control flow:"
2022 { $subsections "sequences-if" }
2023 "For inner loops:"
2024 { $subsections "sequences-unsafe" }
2025 "Implementing sequence combinators:"
2026 { $subsections "sequences-combinator-implementation" } ;
2027
2028 ABOUT: "sequences"