1 USING: arrays help.markup help.syntax math
2 sequences.private vectors strings kernel math.order layouts
3 quotations generic.standard ;
7 { $class-description "A mixin class whose instances are sequences. Custom implementations of the sequence protocol should be declared as instances of this mixin for all sequence functionality to work correctly:"
8 { $code "INSTANCE: my-sequence sequence" }
12 { $values { "seq" sequence } { "n" "a non-negative integer" } }
13 { $contract "Outputs the length of the sequence. All sequences support this operation." } ;
16 { $values { "n" "a non-negative integer" } { "seq" "a resizable sequence" } }
17 { $contract "Resizes a sequence. The initial contents of the new area is undefined." }
18 { $errors "Throws a " { $link no-method } " error if the sequence is not resizable, and a " { $link bounds-error } " if the new length is negative." }
19 { $side-effects "seq" } ;
22 { $values { "n" "a non-negative integer" } { "seq" "a resizable sequence" } }
23 { $contract "Ensures the sequence has a length of at least " { $snippet "n" } " elements. This word differs from " { $link set-length } " in two respects:"
25 { "This word does not shrink the sequence if " { $snippet "n" } " is less than its length." }
26 { "The word doubles the underlying storage of " { $snippet "seq" } ", whereas " { $link set-length } " is permitted to set it to equal " { $snippet "n" } ". This ensures that repeated calls to this word with constant increments of " { $snippet "n" } " do not result in a quadratic amount of copying, so that for example " { $link push-all } " can run efficiently when used in a loop." }
31 { $values { "n" "a non-negative integer" } { "seq" sequence } { "elt" "the element at the " { $snippet "n" } "th index" } }
32 { $contract "Outputs the " { $snippet "n" } "th element of the sequence. Elements are numbered from zero, so the last element has an index one less than the length of the sequence. All sequences support this operation." }
33 { $errors "Throws a " { $link bounds-error } " if the index is negative, or greater than or equal to the length of the sequence." } ;
36 { $values { "elt" object } { "n" "a non-negative integer" } { "seq" "a mutable sequence" } }
37 { $contract "Sets the " { $snippet "n" } "th element of the sequence. Storing beyond the end of a resizable sequence such as a vector or string buffer grows the sequence." }
38 { $errors "Throws an error if the index is negative, or if the sequence is not resizable and the index is greater than or equal to the length of the sequence."
40 "Throws an error if the sequence cannot hold elements of the given type." }
41 { $side-effects "seq" } ;
45 { "indices" sequence } { "seq" sequence }
47 { $description "Ouptuts a sequence of elements from the input sequence indexed by the indices." }
49 { $example "USING: prettyprint sequences ;"
50 "{ 0 2 } { \"a\" \"b\" \"c\" } nths ."
56 { $values { "seq" sequence } }
57 { $description "Throws an " { $link immutable } " error." }
58 { $error-description "Thrown if an attempt is made to modify an immutable sequence." } ;
61 { $values { "len" "a non-negative integer" } { "seq" sequence } { "newseq" "a mutable sequence" } }
62 { $contract "Outputs a mutable sequence of length " { $snippet "n" } " which can hold the elements of " { $snippet "seq" } ". The initial contents of the sequence are undefined." } ;
65 { $values { "len" "a non-negative integer" } { "seq" sequence } { "newseq" "a resizable mutable sequence" } }
66 { $contract "Outputs a resizable mutable sequence with an initial capacity of " { $snippet "n" } " elements and zero length, which can hold the elements of " { $snippet "seq" } "." }
68 { $example "USING: prettyprint sequences ;" "300 V{ } new-resizable ." "V{ }" }
69 { $example "USING: prettyprint sequences ;" "300 SBUF\" \" new-resizable ." "SBUF\" \"" }
73 { $values { "seq" sequence } { "exemplar" sequence } { "newseq" "a new sequence" } }
74 { $contract "Outputs a sequence with the same elements as " { $snippet "seq" } ", but " { $emphasis "like" } " the template sequence, in the sense that it either has the same class as the template sequence, or if the template sequence is a virtual sequence, the same class as the template sequence's underlying sequence."
76 "The default implementation does nothing." }
77 { $notes "Unlike " { $link clone-like } ", the output sequence might share storage with the input sequence." } ;
80 { $values { "seq" sequence } { "?" "a boolean" } }
81 { $description "Tests if the sequence has zero length." } ;
84 { $values { "seq" sequence } { "quot1" quotation } { "quot2" quotation } }
85 { $description "Makes an implicit check if the sequence is empty. An empty sequence is dropped and " { $snippet "quot1" } " is called. Otherwise, if the sequence has any elements, " { $snippet "quot2" } " is called on it." }
87 "USING: kernel prettyprint sequences ;"
88 "{ 1 2 3 } [ \"empty sequence\" ] [ sum ] if-empty ."
94 { "seq" sequence } { "quot" "the first quotation of an " { $link if-empty } } }
95 { $description "Makes an implicit check if the sequence is empty. An empty sequence is dropped and the " { $snippet "quot" } " is called." }
96 { $examples "This word is equivalent to " { $link if-empty } " with an empty second quotation:"
98 "USING: sequences prettyprint ;"
99 "{ } [ { 4 5 6 } ] [ ] if-empty ."
103 "USING: sequences prettyprint ;"
104 "{ } [ { 4 5 6 } ] when-empty ."
111 { "seq" sequence } { "quot" "the second quotation of an " { $link if-empty } } }
112 { $description "Makes an implicit check if the sequence is empty. An empty sequence is dropped. Otherwise, the " { $snippet "quot" } " is called on the sequence." }
113 { $examples "This word is equivalent to " { $link if-empty } " with an empty first quotation:"
115 "USING: sequences prettyprint ;"
116 "{ 4 5 6 } [ ] [ sum ] if-empty ."
120 "USING: sequences prettyprint ;"
121 "{ 4 5 6 } [ sum ] unless-empty ."
126 { if-empty when-empty unless-empty } related-words
129 { $values { "seq" "a resizable sequence" } }
130 { $description "Resizes the sequence to zero length, removing all elements. Not all sequences are resizable." }
131 { $errors "Throws a " { $link bounds-error } " if the new length is negative, or if the sequence is not resizable." }
132 { $side-effects "seq" } ;
135 { $values { "n" "a non-negative integer" } { "seq" sequence } { "newseq" "a new sequence" } }
136 { $description "Creates a new sequence of the same type as " { $snippet "seq" } " with " { $snippet "n" } " elements, and copies the contents of " { $snippet "seq" } " into the new sequence. If " { $snippet "n" } " exceeds the length of " { $snippet "seq" } ", the remaining elements are filled with a default value; " { $link f } " for arrays and 0 for strings." }
137 { $notes "This generic word is only implemented for strings and arrays." } ;
140 { $values { "seq" sequence } { "first" "the first element of the sequence" } }
141 { $description "Outputs the first element of the sequence." }
142 { $errors "Throws an error if the sequence is empty." } ;
145 { $values { "seq" sequence } { "second" "the second element of the sequence" } }
146 { $description "Outputs the second element of the sequence." }
147 { $errors "Throws an error if the sequence contains less than two elements." } ;
150 { $values { "seq" sequence } { "third" "the third element of the sequence" } }
151 { $description "Outputs the third element of the sequence." }
152 { $errors "Throws an error if the sequence contains less than three elements." } ;
155 { $values { "seq" sequence } { "fourth" "the fourth element of the sequence" } }
156 { $description "Outputs the fourth element of the sequence." }
157 { $errors "Throws an error if the sequence contains less than four elements." } ;
160 { $values { "elt" object } { "seq" "a resizable mutable sequence" } }
161 { $description "Adds an element at the end of the sequence. The sequence length is adjusted accordingly." }
162 { $errors "Throws an error if " { $snippet "seq" } " is not resizable, or if the type of " { $snippet "elt" } " is not permitted in " { $snippet "seq" } "." }
163 { $side-effects "seq" } ;
166 { $values { "n" "an integer" } { "seq" sequence } { "?" "a boolean" } }
167 { $description "Tests if the index is within the bounds of the sequence." } ;
170 { $values { "n" "a positive integer" } { "seq" sequence } }
171 { $description "Throws a " { $link bounds-error } "." }
172 { $error-description "Thrown by " { $link nth } ", " { $link set-nth } " and " { $link set-length } " if the given index lies beyond the bounds of the sequence." } ;
175 { $values { "n" "a positive integer" } { "seq" sequence } }
176 { $description "Throws an error if " { $snippet "n" } " is negative or if it is greater than or equal to the length of " { $snippet "seq" } ". Otherwise the two inputs remain on the stack." } ;
179 { $values { "n" "an integer" } { "seq" sequence } { "elt/f" "an object or " { $link f } } }
180 { $description "A forgiving version of " { $link nth } ". If the index is out of bounds, or if the sequence is " { $link f } ", simply outputs " { $link f } "." } ;
183 { $values { "n" "an integer" } { "seq" sequence } { "elt" object } }
184 { $contract "Unsafe variant of " { $link nth } " that does not perform bounds checks." } ;
187 { $values { "elt" object } { "n" "an integer" } { "seq" sequence } }
188 { $contract "Unsafe variant of " { $link set-nth } " that does not perform bounds checks." } ;
190 HELP: exchange-unsafe
191 { $values { "m" "a non-negative integer" } { "n" "a non-negative integer" } { "seq" "a mutable sequence" } }
192 { $description "Unsafe variant of " { $link exchange } " that does not perform bounds checks." } ;
195 { $values { "seq" sequence } { "first" "the first element" } }
196 { $contract "Unsafe variant of " { $link first } " that does not perform bounds checks." } ;
199 { $values { "seq" sequence } { "first" "the first element" } { "second" "the second element" } }
200 { $contract "Unsafe variant of " { $link first2 } " that does not perform bounds checks." } ;
203 { $values { "seq" sequence } { "first" "the first element" } { "second" "the second element" } { "third" "the third element" } }
204 { $contract "Unsafe variant of " { $link first3 } " that does not perform bounds checks." } ;
207 { $values { "seq" sequence } { "first" "the first element" } { "second" "the second element" } { "third" "the third element" } { "fourth" "the fourth element" } }
208 { $contract "Unsafe variant of " { $link first4 } " that does not perform bounds checks." } ;
211 { $values { "obj1" object } { "obj2" object } { "exemplar" sequence } { "seq" sequence } }
212 { $description "Creates a two-element sequence of the same type as " { $snippet "exemplar" } "." } ;
215 { $values { "obj1" object } { "obj2" object } { "obj3" object } { "exemplar" sequence } { "seq" sequence } }
216 { $description "Creates a three-element sequence of the same type as " { $snippet "exemplar" } "." } ;
219 { $values { "obj1" object } { "obj2" object } { "exemplar" sequence } { "obj3" object } { "obj4" object } { "seq" sequence } }
220 { $description "Creates a four-element sequence of the same type as " { $snippet "exemplar" } "." } ;
223 { $values { "seq" sequence } { "first" "the first element" } { "second" "the second element" } }
224 { $description "Pushes the first two elements of a sequence." }
225 { $errors "Throws an error if the sequence has less than two elements." } ;
228 { $values { "seq" sequence } { "first" "the first element" } { "second" "the second element" } { "third" "the third element" } }
229 { $description "Pushes the first three elements of a sequence." }
230 { $errors "Throws an error if the sequence has less than three elements." } ;
233 { $values { "seq" sequence } { "first" "the first element" } { "second" "the second element" } { "third" "the third element" } { "fourth" "the fourth element" } }
234 { $description "Pushes the first four elements of a sequence." }
235 { $errors "Throws an error if the sequence has less than four elements." } ;
238 { $values { "array" "an array" } { "n" "a non-negative fixnum" } }
239 { $class-description "A predicate class whose instances are valid array sizes for the current architecture. The minimum value is zero and the maximum value is " { $link max-array-capacity } "." }
240 { $description "Low-level array length accessor." }
241 { $warning "This word is in the " { $vocab-link "sequences.private" } " vocabulary because it is unsafe. It does not check types, so improper use can corrupt memory." } ;
244 { $values { "n" "a non-negative fixnum" } { "array" "an array" } { "elt" object } }
245 { $description "Low-level array element accessor." }
246 { $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." } ;
249 { $values { "elt" object } { "n" "a non-negative fixnum" } { "array" "an array" } }
250 { $description "Low-level array element mutator." }
251 { $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." } ;
254 { $values { "n" "a non-negative integer" } { "quot" { $quotation "( n -- value )" } } { "into" "a sequence of length at least " { $snippet "n" } } }
255 { $description "A primitive mapping operation that applies a quotation to all integers from 0 up to but not including " { $snippet "n" } ", and collects the results in a new array. User code should use " { $link map } " instead." } ;
258 { $values { "seq" sequence } { "quot" { $quotation "( elt -- )" } } }
259 { $description "Applies the quotation to each element of the sequence in order." } ;
262 { $values { "seq" sequence } { "identity" object } { "quot" { $quotation "( prev elt -- next )" } } { "result" "the final result" } }
263 { $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." }
265 { $example "USING: math prettyprint sequences ;" "{ 1 5 3 } 0 [ + ] reduce ." "9" }
270 { "seq" sequence } { "identity" object } { "quot" quotation } }
271 { $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." }
272 { $examples { $example "USING: sequences prettyprint math ;"
273 "{ 10 50 90 } 0 [ + + ] reduce-index ."
278 { $values { "identity" object } { "seq" sequence } { "quot" { $quotation "( prev elt -- next )" } } { "final" "the final result" } { "newseq" "a new sequence" } }
279 { $description "Combines successive elements of the sequence using a binary operation, and outputs a sequence of intermediate results together with the final result. On the first iteration, the two inputs to the quotation are " { $snippet "identity" } ", and the first element of the sequence. On successive iterations, the first input is the result of the previous iteration, and the second input is the corresponding element of the sequence."
281 "When given the empty sequence, outputs an empty sequence together with the " { $snippet "identity" } "." }
283 { $example "USING: math prettyprint sequences ;" "{ 2 2 2 2 2 } 0 [ + ] accumulate . ." "{ 0 2 4 6 8 }\n10" }
287 { $values { "seq" sequence } { "quot" { $quotation "( old -- new )" } } { "newseq" "a new sequence" } }
288 { $description "Applies the quotation to each element of the sequence in order. The new elements are collected into a sequence of the same class as the input sequence." } ;
291 { $values { "seq" sequence } { "quot" { $quotation "( old -- new )" } } { "newseq" "a new sequence" } { "exemplar" sequence } }
292 { $description "Applies the quotation to each element of the sequence in order. The new elements are collected into a sequence of the same class as " { $snippet "exemplar" } "." }
294 "The following example converts a string into an array of one-element strings:"
295 { $example "USING: prettyprint strings sequences ;" "\"Hello\" [ 1string ] { } map-as ." "{ \"H\" \"e\" \"l\" \"l\" \"o\" }" }
296 "Note that " { $link map } " could not be used here, because it would create another string to hold results, and one-element strings cannot themselves be elements of strings."
301 { "seq" sequence } { "quot" quotation } }
302 { $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." }
303 { $examples { $example "USING: sequences prettyprint math ;"
304 "{ 10 20 30 } [ + . ] each-index"
310 { "seq" sequence } { "quot" quotation } }
311 { $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." }
312 { $examples { $example "USING: sequences prettyprint math ;"
313 "{ 10 20 30 } [ + ] map-index ."
318 { $values { "i" "a non-negative integer" } { "seq" "a mutable sequence" } { "quot" { $quotation "( elt -- newelt )" } } }
319 { $description "Applies the quotation to the " { $snippet "i" } "th element of the sequence, storing the result back into the sequence." }
320 { $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" } "." }
321 { $side-effects "seq" } ;
324 { $values { "seq" "a mutable sequence" } { "quot" { $quotation "( old -- new )" } } }
325 { $description "Applies the quotation to each element yielding a new element, storing the new elements back in the original sequence." }
326 { $errors "Throws an error if the sequence is immutable, or the sequence cannot hold elements of the type output by " { $snippet "quot" } "." }
327 { $side-effects "seq" } ;
330 { $values { "seq1" sequence } { "seq2" sequence } { "n" "a non-negative integer" } }
331 { $description "Outputs the minimum of the lengths of the two sequences." } ;
334 { $values { "seq1" sequence } { "seq2" sequence } { "n" "a non-negative integer" } }
335 { $description "Outputs the maximum of the lengths of the two sequences." } ;
338 { $values { "seq1" sequence } { "seq2" sequence } { "quot" { $quotation "( elt1 elt2 -- )" } } }
339 { $description "Applies the quotation to pairs of elements from " { $snippet "seq1" } " and " { $snippet "seq2" } "." } ;
342 { $values { "seq1" sequence } { "seq2" sequence } { "seq3" sequence } { "quot" { $quotation "( elt1 elt2 elt3 -- )" } } }
343 { $description "Applies the quotation to triples of elements from " { $snippet "seq1" } ", " { $snippet "seq2" } " and " { $snippet "seq3" } "." } ;
346 { $values { "seq1" sequence }
348 { "identity" object }
349 { "quot" { $quotation "( prev elt1 elt2 -- next )" } }
350 { "result" "the final result" } }
351 { $description "Combines successive pairs of elements from the two sequences using a ternary operation. The first input value at each iteration except the first one is the result of the previous iteration. The first input value at the first iteration is " { $snippet "identity" } "." } ;
354 { $values { "seq1" sequence } { "seq2" sequence } { "quot" { $quotation "( elt1 elt2 -- new )" } } { "newseq" "a new sequence" } }
355 { $description "Applies the quotation to each pair of elements in turn, yielding new elements which are collected into a new sequence having the same class as " { $snippet "seq1" } "." } ;
358 { $values { "seq1" sequence } { "seq2" sequence } { "seq3" sequence } { "quot" { $quotation "( elt1 elt2 elt3 -- new )" } } { "newseq" "a new sequence" } }
359 { $description "Applies the quotation to each triple of elements in turn, yielding new elements which are collected into a new sequence having the same class as " { $snippet "seq1" } "." } ;
362 { $values { "seq1" sequence } { "seq2" sequence } { "quot" { $quotation "( elt1 elt2 -- new )" } } { "exemplar" sequence } { "newseq" "a new sequence" } }
363 { $description "Applies the quotation to each pair of elements in turn, yielding new elements which are collected into a new sequence having the same class as " { $snippet "exemplar" } "." } ;
366 { $values { "seq1" sequence } { "seq2" sequence } { "seq3" sequence } { "quot" { $quotation "( elt1 elt2 elt3 -- new )" } } { "exemplar" sequence } { "newseq" "a new sequence" } }
367 { $description "Applies the quotation to each triple of elements in turn, yielding new elements which are collected into a new sequence having the same class as " { $snippet "exemplar" } "." } ;
370 { $values { "seq1" sequence } { "seq2" sequence } { "quot" { $quotation "( elt1 elt2 -- ? )" } } { "?" "a boolean" } }
371 { $description "Tests the predicate pairwise against elements of " { $snippet "seq1" } " and " { $snippet "seq2" } "." } ;
374 { $values { "seq" sequence }
375 { "quot" { $quotation "( elt -- ? )" } }
376 { "i" "the index of the first match, or " { $link f } }
377 { "elt" "the first matching element, or " { $link f } } }
378 { $description "A simpler variant of " { $link find-from } " where the starting index is 0." } ;
381 { $values { "n" "a starting index" }
383 { "quot" { $quotation "( elt -- ? )" } }
384 { "i" "the index of the first match, or " { $link f } }
385 { "elt" "the first matching element, or " { $link f } } }
386 { $description "Applies the quotation to each element of the sequence in turn, until it outputs a true value or the end of the sequence is reached. If the quotation yields a true value for some sequence element, the word outputs the element index and the element itself. Otherwise, the word outputs an index of f and " { $link f } " as the element." } ;
389 { $values { "seq" sequence } { "quot" { $quotation "( elt -- ? )" } } { "i" "the index of the first match, or f" } { "elt" "the first matching element, or " { $link f } } }
390 { $description "A simpler variant of " { $link find-last-from } " where the starting index is one less than the length of the sequence." } ;
393 { $values { "n" "a starting index" } { "seq" sequence } { "quot" { $quotation "( elt -- ? )" } } { "i" "the index of the first match, or f" } { "elt" "the first matching element, or " { $link f } } }
394 { $description "Applies the quotation to each element of the sequence in reverse order, until it outputs a true value or the start of the sequence is reached. If the quotation yields a true value for some sequence element, the word outputs the element index and the element itself. Otherwise, the word outputs an index of f and " { $link f } " as the element." } ;
397 { $values { "seq" sequence } { "quot" { $quotation "( elt -- ? )" } } { "?" "a boolean" } }
398 { $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 } "." } ;
401 { $values { "seq" sequence } { "quot" { $quotation "( elt -- ? )" } } { "?" "a boolean" } }
402 { $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 } "." } ;
405 { $values { "elt" object } { "quot" { $quotation "( elt -- ? )" } } { "accum" "a resizable mutable sequence" } }
406 { $description "Adds the element at the end of the sequence if the quotation yields a true value." }
407 { $notes "This word is a factor of " { $link filter } "." } ;
410 { $values { "seq" sequence } { "quot" { $quotation "( elt -- ? )" } } { "subseq" "a new sequence" } }
411 { $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." } ;
414 { $values { "seq" "a resizable mutable sequence" } { "quot" { $quotation "( elt -- ? )" } } }
415 { $description "Applies the quotation to each element in turn, and removes elements for which the quotation outputs a false value." }
416 { $side-effects "seq" } ;
419 { $values { "seq" sequence } { "between" "a quotation" } { "quot" { $quotation "( elt -- )" } } }
420 { $description "Applies " { $snippet "quot" } " to each element in turn, also invoking " { $snippet "between" } " in-between each pair of elements." }
421 { $example "USING: io sequences ;" "{ \"a\" \"b\" \"c\" } [ \"X\" write ] [ write ] interleave" "aXbXc" } ;
424 { $values { "obj" object } { "seq" sequence } { "n" "an index" } }
425 { $description "Outputs the index of the first element in the sequence equal to " { $snippet "obj" } ". If no element is found, outputs " { $link f } "." } ;
428 { $values { "obj" object } { "i" "a start index" } { "seq" sequence } { "n" "an index" } }
429 { $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 } "." } ;
432 { $values { "obj" object } { "seq" sequence } { "n" "an index" } }
433 { $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 } "." } ;
435 HELP: last-index-from
436 { $values { "obj" object } { "i" "a start index" } { "seq" sequence } { "n" "an index" } }
437 { $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 } "." } ;
440 { $values { "elt" object } { "seq" sequence } { "?" "a boolean" } }
441 { $description "Tests if the sequence contains an element equal to the object." }
442 { $notes "This word uses equality comparison (" { $link = } ")." } ;
445 { $values { "elt" object } { "seq" sequence } { "?" "a boolean" } }
446 { $description "Tests if the sequence contains the object." }
447 { $notes "This word uses identity comparison (" { $link eq? } ")." } ;
450 { $values { "elt" object } { "seq" sequence } { "newseq" "a new sequence" } }
451 { $description "Outputs a new sequence containing all elements of the input sequence except for given element." }
452 { $notes "This word uses equality comparison (" { $link = } ")." } ;
455 { $values { "elt" object } { "seq" sequence } { "newseq" "a new sequence" } }
456 { $description "Outputs a new sequence containing all elements of the input sequence except those equal to the given element." }
457 { $notes "This word uses identity comparison (" { $link eq? } ")." } ;
461 { "n" integer } { "seq" sequence }
462 { "seq'" sequence } }
463 { $description "Creates a new sequence without the element at index " { $snippet "n" } "." }
464 { $examples "Notice that the original sequence is left intact:" { $example "USING: sequences prettyprint kernel ;"
465 "{ 1 2 3 } 1 over remove-nth . ."
470 { $values { "from" "an index in " { $snippet "seq" } } { "to" "an index in " { $snippet "seq" } } { "seq" "a mutable sequence" } }
471 { $description "Sets the element with index " { $snippet "m" } " to the element with index " { $snippet "n" } "." }
472 { $side-effects "seq" } ;
475 { $values { "elt" object } { "seq" "a resizable mutable sequence" } }
476 { $description "Removes all elements equal to " { $snippet "elt" } " from " { $snippet "seq" } "." }
477 { $notes "This word uses equality comparison (" { $link = } ")." }
478 { $side-effects "seq" } ;
481 { $values { "elt" object } { "seq" "a resizable mutable sequence" } }
482 { $description "Outputs a new sequence containing all elements of the input sequence except the given element." }
483 { $notes "This word uses identity comparison (" { $link eq? } ")." }
484 { $side-effects "seq" } ;
487 { $values { "n" "a non-negative integer" } { "seq" "a resizable mutable sequence" } }
488 { $description "Removes the " { $snippet "n" } "th element from the sequence, shifting all other elements down and reducing its length by one." }
489 { $side-effects "seq" } ;
492 { $values { "from" "a non-negative integer" } { "to" "a non-negative integer" } { "seq" "a resizable mutable sequence" } }
493 { $description "Removes a range of elements beginning at index " { $snippet "from" } " and ending before index " { $snippet "to" } "." }
494 { $side-effects "seq" } ;
497 { $values { "new" sequence } { "seq" "a mutable sequence" } { "from" "a non-negative integer" } { "to" "a non-negative integer" } }
498 { $description "Replaces a range of elements beginning at index " { $snippet "from" } " and ending before index " { $snippet "to" } " with a new sequence." }
499 { $notes "If the " { $snippet "to - from" } " is equal to the length of " { $snippet "new" } ", the sequence remains the same size, and does not have to support resizing. However, if " { $snippet "to - from" } " is not equal to the length of " { $snippet "new" } ", the " { $link set-length } " word is called on " { $snippet "seq" } ", so fixed-size sequences should not be passed in this case." }
500 { $errors "Throws an error if " { $snippet "new" } " contains elements whose types are not permissible in " { $snippet "seq" } "." }
501 { $side-effects "seq" } ;
503 { push prefix suffix } related-words
506 { $values { "seq" sequence } { "elt" object } { "newseq" sequence } }
507 { $description "Outputs a new sequence obtained by adding " { $snippet "elt" } " at the end of " { $snippet "seq" } "." }
508 { $errors "Throws an error if the type of " { $snippet "elt" } " is not permitted in sequences of the same class as " { $snippet "seq1" } "." }
510 { $example "USING: prettyprint sequences ;" "{ 1 2 3 } 4 suffix ." "{ 1 2 3 4 }" }
514 { $values { "seq" sequence } { "elt" object } { "newseq" sequence } }
515 { $description "Outputs a new sequence obtained by adding " { $snippet "elt" } " at the beginning of " { $snippet "seq" } "." }
516 { $errors "Throws an error if the type of " { $snippet "elt" } " is not permitted in sequences of the same class as " { $snippet "seq1" } "." }
518 { $example "USING: prettyprint sequences ;" "{ 1 2 3 } 0 prefix ." "{ 0 1 2 3 }" }
522 { $values { "seq" "a sequence of sequences" } { "n" integer } }
523 { $description "Outputs the sum of the lengths of all sequences in " { $snippet "seq" } "." } ;
526 { $values { "seq" sequence } { "newseq" sequence } }
527 { $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" } "." }
528 { $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" } "." } ;
531 { $values { "seq" sequence } { "glue" sequence } { "newseq" sequence } }
532 { $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" } "." }
533 { $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" } "." } ;
535 { join concat } related-words
538 { $values { "seq" sequence } { "elt" object } }
539 { $description "Outputs the last element of a sequence." }
540 { $errors "Throws an error if the sequence is empty." } ;
542 { peek pop pop* } related-words
545 { $values { "seq" "a resizable mutable sequence" } }
546 { $description "Removes the last element and shortens the sequence." }
547 { $side-effects "seq" }
548 { $errors "Throws an error if the sequence is empty." } ;
551 { $values { "seq" "a resizable mutable sequence" } { "elt" object } }
552 { $description "Outputs the last element after removing it and shortening the sequence." }
553 { $side-effects "seq" }
554 { $errors "Throws an error if the sequence is empty." } ;
557 { $values { "seq1" sequence } { "seq2" sequence } { "i" "an index" } }
558 { $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." } ;
561 { $values { "matrix" "a sequence of equal-length sequences" } { "newmatrix" "a sequence of equal-length sequences" } }
562 { $description "Transposes the matrix; that is, rows become columns and columns become rows." }
563 { $examples { $example "USING: prettyprint sequences ;" "{ { 1 2 3 } { 4 5 6 } } flip ." "{ { 1 4 } { 2 5 } { 3 6 } }" } } ;
566 { $values { "m" "a non-negative integer" } { "n" "a non-negative integer" } { "seq" "a mutable sequence" } }
567 { $description "Exchanges the " { $snippet "m" } "th and " { $snippet "n" } "th elements of " { $snippet "seq" } "." } ;
570 { $values { "seq" "a mutable sequence" } }
571 { $description "Reverses a sequence in-place." }
572 { $side-effects "seq" } ;
575 { $values { "seq" sequence } { "n" "a non-negative integer" } { "elt" object } { "quot" { $quotation "( seq1 seq2 -- newseq )" } } { "newseq" "a new sequence" } }
576 { $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." } ;
579 { $values { "seq" sequence } { "n" "a non-negative integer" } { "elt" object } { "padded" "a new sequence" } }
580 { $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" } "." }
581 { $examples { $example "USING: io sequences ;" "{ \"ab\" \"quux\" } [ 5 CHAR: - pad-left print ] each" "---ab\n-quux" } } ;
584 { $values { "seq" sequence } { "n" "a non-negative integer" } { "elt" object } { "padded" "a new sequence" } }
585 { $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" } "." }
586 { $examples { $example "USING: io sequences ;" "{ \"ab\" \"quux\" } [ 5 CHAR: - pad-right print ] each" "ab---\nquux-" } } ;
589 { $values { "seq1" sequence } { "seq2" sequence } { "?" "a boolean" } }
590 { $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." } ;
593 { $class-description "A virtual sequence which presents a reversed view of an underlying sequence. New instances can be created by calling " { $link <reversed> } "." } ;
596 { $values { "seq" sequence } { "newseq" "a new sequence" } }
597 { $description "Outputs a new sequence having the same elements as " { $snippet "seq" } " but in reverse order." } ;
599 { reverse <reversed> reverse-here } related-words
602 { $values { "seq" sequence } { "reversed" "a new sequence" } }
603 { $description "Creates an instance of the " { $link reversed } " class." }
604 { $see-also "virtual-sequences" } ;
607 { $values { "str" "a reason" } }
608 { $description "Throws a " { $link slice-error } "." }
609 { $error-description "Thrown by " { $link <slice> } " if one of the following invalid conditions holds:"
611 "The start index is negative"
612 "The end index is greater than the length of the sequence"
613 "The start index is greater than the end index"
618 { $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> } "."
620 "Slices are mutable if the underlying sequence is mutable, and mutating a slice changes the underlying sequence. However, slices cannot be resized after creation." } ;
623 { $values { "from" "a non-negative integer" } { "to" "a non-negative integer" } { "seq" sequence } }
624 { $description "Ensures that " { $snippet "m" } " is less than or equal to " { $snippet "m" } ", and that both indices are within bounds for " { $snippet "seq" } "." }
625 { $errors "Throws a " { $link slice-error } " if the preconditions are not met." } ;
628 { $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 } }
629 { $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." }
633 { $values { "seq" sequence } { "slice" slice } }
634 { $description "Outputs a slice with the same elements as " { $snippet "seq" } ", and " { $snippet "from" } " equal to 0 and " { $snippet "to" } " equal to the length of " { $snippet "seq" } "." }
635 { $notes "Some words create slices then proceed to read the " { $snippet "to" } " and " { $snippet "from" } " slots of the slice. To behave predictably when they are themselves given a slice as input, they apply this word first to get a canonical slice." } ;
638 { $values { "from" "a non-negative integer" } { "to" "a non-negative integer" } { "seq" sequence } { "slice" slice } }
639 { $description "Outputs a new virtual sequence sharing storage with the subrange of elements in " { $snippet "seq" } " with indices starting from and including " { $snippet "m" } ", and up to but not including " { $snippet "n" } "." }
640 { $errors "Throws an error if " { $snippet "m" } " or " { $snippet "n" } " is out of bounds." }
641 { $notes "Taking the slice of a slice outputs a slice of the underlying sequence of the original slice. Keep this in mind when writing code which depends on the values of " { $snippet "from" } " and " { $snippet "to" } " being equal to the inputs to this word. The " { $link <flat-slice> } " word might be helpful in such situations." } ;
643 { <slice> subseq } related-words
646 { $class-description "A virtual sequence consisting of " { $snippet "elt" } " repeated " { $snippet "len" } " times. Repetitions are created by calling " { $link <repetition> } "." } ;
649 { $values { "len" "a non-negative integer" } { "elt" object } { "repetition" repetition } }
650 { $description "Creates a new " { $link repetition } "." }
652 { $example "USING: arrays prettyprint sequences ;" "10 \"X\" <repetition> >array ." "{ \"X\" \"X\" \"X\" \"X\" \"X\" \"X\" \"X\" \"X\" \"X\" \"X\" }" }
653 { $example "USING: prettyprint sequences ;" "10 \"X\" <repetition> concat ." "\"XXXXXXXXXX\"" }
656 { $values { "src" sequence } { "i" "an index in " { $snippet "dest" } } { "dst" "a mutable sequence" } }
657 { $description "Copies all elements of " { $snippet "src" } " to " { $snippet "dest" } ", with destination indices starting from " { $snippet "i" } ". Grows " { $snippet "to" } " first if necessary." }
658 { $side-effects "dest" }
659 { $errors "An error is thrown if " { $snippet "to" } " is not resizable, and not large enough to hold the copied elements." } ;
662 { $values { "src" sequence } { "dest" "a resizable mutable sequence" } }
663 { $description "Appends " { $snippet "src" } " to the end of " { $snippet "dest" } "." }
664 { $side-effects "dest" }
665 { $errors "Throws an error if " { $snippet "src" } " contains elements not permitted in " { $snippet "dest" } "." } ;
668 { $values { "seq1" sequence } { "seq2" sequence } { "newseq" sequence } }
669 { $description "Outputs a new sequence of the same type as " { $snippet "seq1" } " consisting of the elements of " { $snippet "seq1" } " followed by " { $snippet "seq2" } "." }
670 { $errors "Throws an error if " { $snippet "seq2" } " contains elements not permitted in sequences of the same class as " { $snippet "seq1" } "." }
672 { $example "USING: prettyprint sequences ;"
673 "{ 1 2 } B{ 3 4 } append ."
676 { $example "USING: prettyprint sequences strings ;"
677 "\"go\" \"ing\" append ."
683 { $values { "seq1" sequence } { "seq2" sequence } { "exemplar" sequence } { "newseq" sequence } }
684 { $description "Outputs a new sequence of the same type as " { $snippet "exemplar" } " consisting of the elements of " { $snippet "seq1" } " followed by " { $snippet "seq2" } "." }
685 { $errors "Throws an error if " { $snippet "seq1" } " or " { $snippet "seq2" } " contain elements not permitted in sequences of the same class as " { $snippet "exemplar" } "." }
687 { $example "USING: prettyprint sequences ;"
688 "{ 1 2 } B{ 3 4 } B{ } append-as ."
691 { $example "USING: prettyprint sequences strings ;"
692 "\"go\" \"ing\" SBUF\" \" append-as ."
697 { append append-as } related-words
700 { $values { "seq1" sequence } { "seq2" sequence } { "newseq" sequence } }
701 { $description "Outputs a new sequence of the same type as " { $snippet "seq2" } " consisting of the elements of " { $snippet "seq2" } " followed by " { $snippet "seq1" } "." }
702 { $errors "Throws an error if " { $snippet "seq1" } " contains elements not permitted in sequences of the same class as " { $snippet "seq2" } "." }
703 { $examples { $example "USING: prettyprint sequences ;"
704 "{ 1 2 } B{ 3 4 } prepend ."
707 { $example "USING: prettyprint sequences strings ;"
708 "\"go\" \"car\" prepend ."
714 { $values { "seq1" sequence } { "seq2" sequence } { "seq3" sequence } { "newseq" sequence } }
715 { $description "Outputs a new sequence consisting of the elements of " { $snippet "seq1" } ", " { $snippet "seq2" } " and " { $snippet "seq3" } " in turn." }
716 { $errors "Throws an error if " { $snippet "seq2" } " or " { $snippet "seq3" } " contain elements not permitted in sequences of the same class as " { $snippet "seq1" } "." }
718 { $example "USING: prettyprint sequences ;"
719 "\"a\" \"b\" \"c\" 3append ."
725 { $values { "seq1" sequence } { "seq2" sequence } { "seq3" sequence } { "exemplar" sequence } { "newseq" sequence } }
726 { $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" } "." }
727 { $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" } "." }
729 { $example "USING: prettyprint sequences ;"
730 "\"a\" \"b\" \"c\" SBUF\" \" 3append-as ."
735 { 3append 3append-as } related-words
738 { $values { "seq1" sequence } { "seq2" sequence } { "seq3" sequence } { "newseq" sequence } }
739 { $description "Outputs a new sequence with " { $snippet "seq1" } " inserted between " { $snippet "seq2" } " and " { $snippet "seq3" } "." }
741 { $example "USING: sequences prettyprint ;"
742 "\"sssssh\" \"(\" \")\" surround ."
748 { $values { "seq1" sequence } { "seq2" sequence } { "seq3" sequence } { "newseq" sequence } }
749 { $description "Outputs a new sequence with " { $snippet "seq3" } " inserted between " { $snippet "seq1" } " and " { $snippet "seq2" } "." }
751 { $example "USING: sequences prettyprint ;"
752 "\"a\" \"b\" \",\" glue ."
758 { $values { "from" "a non-negative integer" } { "to" "a non-negative integer" } { "seq" sequence } { "subseq" "a new sequence" } }
759 { $description "Outputs a new sequence consisting of all elements starting from and including " { $snippet "from" } ", and up to but not including " { $snippet "to" } "." }
760 { $errors "Throws an error if " { $snippet "from" } " or " { $snippet "to" } " is out of bounds." } ;
763 { $values { "seq" sequence } { "exemplar" sequence } { "newseq" "a new sequence" } }
764 { $description "Outputs a newly-allocated sequence with the same elements as " { $snippet "seq" } " but of the same type as " { $snippet "exemplar" } "." }
765 { $notes "Unlike " { $link like } ", this word always creates a new sequence which never shares storage with the original." } ;
768 { $values { "seq" sequence } { "n" "a non-negative integer" } { "slice" "a slice" } }
769 { $description "Outputs a virtual sequence sharing storage with the first " { $snippet "n" } " elements of the input sequence." }
770 { $errors "Throws an error if the index is out of bounds." } ;
773 { $values { "seq" sequence } { "n" "a non-negative integer" } { "slice" "a slice" } }
774 { $description "Outputs a virtual sequence sharing storage with all elements from the " { $snippet "n" } "th index until the end of the input sequence." }
775 { $errors "Throws an error if the index is out of bounds." } ;
778 { $values { "seq" sequence } { "slice" "a slice" } }
779 { $description "Outputs a virtual sequence sharing storage with all but the last element of the input sequence." }
780 { $errors "Throws an error on an empty sequence." } ;
783 { $values { "seq" sequence } { "slice" "a slice" } }
784 { $description "Outputs a virtual sequence sharing storage with all elements from the 1st index until the end of the input sequence." }
785 { $notes "Equivalent to " { $snippet "1 tail" } }
786 { $errors "Throws an error on an empty sequence." } ;
789 { $values { "seq" sequence } { "n" "a non-negative integer" } { "slice" "a slice" } }
790 { $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." }
791 { $errors "Throws an error if the index is out of bounds." } ;
794 { $values { "seq" sequence } { "n" "a non-negative integer" } { "slice" "a slice" } }
795 { $description "Outputs a virtual sequence sharing storage with the last " { $snippet "n" } " elements of the input sequence." }
796 { $errors "Throws an error if the index is out of bounds." } ;
799 { $values { "seq" sequence } { "n" "a non-negative integer" } { "headseq" "a new sequence" } }
800 { $description "Outputs a new sequence consisting of the first " { $snippet "n" } " elements of the input sequence." }
801 { $errors "Throws an error if the index is out of bounds." } ;
804 { $values { "seq" sequence } { "n" "a non-negative integer" } { "tailseq" "a new sequence" } }
805 { $description "Outputs a new sequence consisting of the input sequence with the first n items removed." }
806 { $errors "Throws an error if the index is out of bounds." } ;
809 { $values { "seq" sequence } { "headseq" "a new sequence" } }
810 { $description "Outputs a new sequence consisting of the input sequence with the last item removed." }
811 { $errors "Throws an error on an empty sequence." } ;
814 { $values { "seq" sequence } { "tailseq" "a new sequence" } }
815 { $description "Outputs a new sequence consisting of the input sequence with the first item removed." }
816 { $errors "Throws an error on an empty sequence." } ;
819 { $values { "seq" sequence } { "n" "a non-negative integer" } { "headseq" "a new sequence" } }
820 { $description "Outputs a new sequence consisting of all elements of " { $snippet "seq" } " until the " { $snippet "n" } "th element from the end. In other words, it outputs a sequence of the first " { $snippet "l-n" } " elements of the input sequence, where " { $snippet "l" } " is its length." }
821 { $errors "Throws an error if the index is out of bounds." } ;
824 { $values { "seq" sequence } { "n" "a non-negative integer" } { "tailseq" "a new sequence" } }
825 { $description "Outputs a new sequence consisting of the last " { $snippet "n" } " elements of the input sequence." }
826 { $errors "Throws an error if the index is out of bounds." } ;
829 { $values { "seq1" sequence } { "seq2" sequence } { "?" "a boolean" } }
830 { $description "Tets if the length of " { $snippet "seq1" } " is smaller than the length of " { $snippet "seq2" } "." } ;
833 { $values { "seq" sequence } { "begin" sequence } { "?" "a boolean" } }
834 { $description "Tests if " { $snippet "seq" } " starts with " { $snippet "begin" } ". If " { $snippet "begin" } " is longer than " { $snippet "seq" } ", this word outputs " { $link f } "." } ;
837 { $values { "seq" sequence } { "end" sequence } { "?" "a boolean" } }
838 { $description "Tests if " { $snippet "seq" } " ends with " { $snippet "end" } ". If " { $snippet "end" } " is longer than " { $snippet "seq" } ", this word outputs " { $link f } "." } ;
840 { remove remove-nth remq delq delete delete-nth } related-words
843 { $values { "seq" sequence } { "n" "a non-negative integer" } { "before-slice" sequence } { "after-slice" "a slice" } }
844 { $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." }
845 { $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." } ;
848 { $values { "seq" sequence } { "n" "a non-negative integer" } { "before" sequence } { "after" sequence } }
849 { $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" } "." }
850 { $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." } ;
853 { $values { "seq" sequence } { "n" "a non-negative integer" } { "before" sequence } { "after" sequence } }
854 { $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" } "." } ;
857 { $values { "subseq" sequence } { "seq" sequence } { "n" "a start index" } { "i" "a start index" } }
858 { $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 } "." } ;
861 { $values { "subseq" sequence } { "seq" sequence } { "i" "a start index" } }
862 { $description "Outputs the start index of the first contiguous subsequence equal to " { $snippet "subseq" } ", or " { $link f } " if no matching subsequence is found." } ;
865 { $values { "subseq" sequence } { "seq" sequence } { "?" "a boolean" } }
866 { $description "Tests if " { $snippet "seq" } " contains the elements of " { $snippet "subseq" } " as a contiguous subsequence." } ;
869 { $values { "seq1" sequence } { "seq2" sequence } { "slice1" "a slice" } { "slice2" "a slice" } }
870 { $description "Outputs a pair of virtual sequences with the common prefix of " { $snippet "seq1" } " and " { $snippet "seq2" } " removed." } ;
873 { $values { "seq" sequence } { "rest" sequence } { "first" object } }
874 { $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." }
876 { $example "USING: prettyprint sequences ;" "{ 1 2 3 } unclip suffix ." "{ 2 3 1 }" }
880 { $values { "seq" sequence } { "rest-slice" slice } { "first" object } }
881 { $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." }
882 { $examples { $example "USING: math.order prettyprint sequences ;" "{ 3 -1 -10 5 7 } unclip-slice [ min ] reduce ." "-10" } } ;
885 { $values { "seq" sequence } { "butlast" sequence } { "last" object } }
886 { $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." }
888 { $example "USING: prettyprint sequences ;" "{ 1 2 3 } unclip-last prefix ." "{ 3 1 2 }" }
891 HELP: unclip-last-slice
892 { $values { "seq" sequence } { "butlast-slice" slice } { "last" object } }
893 { $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." } ;
896 { $values { "seq" "a sequence of numbers" } { "n" "a number" } }
897 { $description "Outputs the sum of all elements of " { $snippet "seq" } ". Outputs zero given an empty sequence." } ;
900 { $values { "seq" "a sequence of numbers" } { "n" "a number" } }
901 { $description "Outputs the product of all elements of " { $snippet "seq" } ". Outputs one given an empty sequence." } ;
904 { $values { "seq" "a sequence of real numbers" } { "n" "a number" } }
905 { $description "Outputs the least element of " { $snippet "seq" } "." }
906 { $errors "Throws an error if the sequence is empty." } ;
909 { $values { "seq" "a sequence of real numbers" } { "n" "a number" } }
910 { $description "Outputs the greatest element of " { $snippet "seq" } "." }
911 { $errors "Throws an error if the sequence is empty." } ;
914 { $values { "pred" { $quotation "( -- ? )" } } { "quot" { $quotation "( -- obj )" } } { "tail" "a quotation" } { "seq" "a sequence" } }
915 { $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." }
917 "The following example divides a number by two until we reach zero, and accumulates intermediate results:"
918 { $example "USING: kernel math prettyprint sequences ;" "1337 [ dup 0 > ] [ 2/ dup ] [ ] produce nip ." "{ 668 334 167 83 41 20 10 5 2 1 0 }" }
919 "The " { $snippet "tail" } " quotation is used when the predicate produces more than one output value. In this case, we have to drop this value even if the predicate fails in order for stack inference to calculate a stack effect for the " { $link produce } " call:"
920 { $unchecked-example "USING: kernel prettyprint random sequences ;" "[ 10 random dup 1 > ] [ ] [ drop ] produce ." "{ 8 2 2 9 }" }
924 { $values { "pred" { $quotation "( -- ? )" } } { "quot" { $quotation "( -- obj )" } } { "tail" "a quotation" } { "exemplar" sequence } { "seq" "a sequence" } }
925 { $description "Calls " { $snippet "pred" } " repeatedly. If the predicate yields " { $link f } ", stops, otherwise, calls " { $snippet "quot" } " to yield a value. Values are accumulated and returned in a sequence of type " { $snippet "exemplar" } " at the end." }
927 "The following example divides a number by two until we reach zero, and accumulates intermediate results:"
928 { $example "USING: kernel math prettyprint sequences ;" "1337 [ dup 0 > ] [ 2/ dup ] [ ] V{ } produce-as nip ." "V{ 668 334 167 83 41 20 10 5 2 1 0 }" }
929 "The " { $snippet "tail" } " quotation is used when the predicate produces more than one output value. In this case, we have to drop this value even if the predicate fails in order for stack inference to calculate a stack effect for the " { $link produce } " call:"
930 { $unchecked-example "USING: kernel prettyprint random sequences ;" "[ 10 random dup 1 > ] [ ] [ drop ] B{ } produce-as ." "B{ 8 2 2 9 }" }
934 { $values { "seq" sequence } { "quot" quotation } { "n" number } }
935 { $description "Like map sum, but without creating an intermediate sequence." }
937 "! Find the sum of the squares [0,99]"
938 "USING: math math.ranges sequences prettyprint ;"
939 "100 [1,b] [ sq ] sigma ."
944 { $values { "seq" sequence } { "quot" quotation } { "n" integer } }
945 { $description "Efficiently returns the number of elements that the predicate quotation matches." }
947 "USING: math math.ranges sequences prettyprint ;"
948 "100 [1,b] [ even? ] count ."
954 { "quot" "a predicate quotation" }
955 { "quot" quotation } { "accum" vector } }
956 { $description "Creates a new vector to accumulate the values which return true for a predicate. Returns a new quotation which accepts an object to be tested and stored in the accumulator if the test yields true. The accumulator is left on the stack for convenience." }
957 { $example "! Find all the even numbers:" "USING: prettyprint sequences math kernel ;"
958 "10 [ even? ] pusher [ each ] dip ."
961 { $notes "Used to implement the " { $link filter } " word." } ;
965 { "seq" sequence } { "quot" quotation }
966 { "newseq" sequence } }
967 { $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." }
968 { $example "" "USING: prettyprint math sequences ;"
969 "{ 0 0 1 2 3 0 0 } [ zero? ] trim-left ."
973 HELP: trim-left-slice
975 { "seq" sequence } { "quot" quotation }
977 { $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" }
978 { $example "" "USING: prettyprint math sequences ;"
979 "{ 0 0 1 2 3 0 0 } [ zero? ] trim-left-slice ."
980 "T{ slice { from 2 } { to 7 } { seq { 0 0 1 2 3 0 0 } } }"
985 { "seq" sequence } { "quot" quotation }
986 { "newseq" sequence } }
987 { $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." }
988 { $example "" "USING: prettyprint math sequences ;"
989 "{ 0 0 1 2 3 0 0 } [ zero? ] trim-right ."
993 HELP: trim-right-slice
995 { "seq" sequence } { "quot" quotation }
997 { $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." }
998 { $example "" "USING: prettyprint math sequences ;"
999 "{ 0 0 1 2 3 0 0 } [ zero? ] trim-right-slice ."
1000 "T{ slice { from 0 } { to 5 } { seq { 0 0 1 2 3 0 0 } } }"
1005 { "seq" sequence } { "quot" quotation }
1006 { "newseq" sequence } }
1007 { $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." }
1008 { $example "" "USING: prettyprint math sequences ;"
1009 "{ 0 0 1 2 3 0 0 } [ zero? ] trim ."
1015 { "seq" sequence } { "quot" quotation }
1017 { $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." }
1018 { $example "" "USING: prettyprint math sequences ;"
1019 "{ 0 0 1 2 3 0 0 } [ zero? ] trim-slice ."
1020 "T{ slice { from 2 } { to 5 } { seq { 0 0 1 2 3 0 0 } } }"
1023 { trim trim-slice trim-left trim-left-slice trim-right trim-right-slice } related-words
1028 { "newseq" sequence } }
1029 { $description "Outputs a new sequence with all instance of " { $link f } " removed." }
1031 { $example "USING: prettyprint sequences ;"
1032 "{ \"a\" 3 { } f } sift ."
1040 { "newseq" sequence } }
1041 { $description "Outputs a new sequence with all empty sequences removed." }
1043 { $example "USING: prettyprint sequences ;"
1044 "{ { } { 2 3 } { 5 } { } } harvest ."
1049 { filter filter-here sift harvest } related-words
1053 { "first" object } { "seq" sequence } }
1054 { $description "Sets the first element of a sequence." }
1056 { $example "USING: prettyprint kernel sequences ;"
1057 "{ 1 2 3 4 } 5 over set-first ."
1064 { "second" object } { "seq" sequence } }
1065 { $description "Sets the second element of a sequence." }
1067 { $example "USING: prettyprint kernel sequences ;"
1068 "{ 1 2 3 4 } 5 over set-second ."
1075 { "third" object } { "seq" sequence } }
1076 { $description "Sets the third element of a sequence." }
1078 { $example "USING: prettyprint kernel sequences ;"
1079 "{ 1 2 3 4 } 5 over set-third ."
1086 { "fourth" object } { "seq" sequence } }
1087 { $description "Sets the fourth element of a sequence." }
1089 { $example "USING: prettyprint kernel sequences ;"
1090 "{ 1 2 3 4 } 5 over set-fourth ."
1095 { set-first set-second set-third set-fourth } related-words
1099 { "seq" sequence } { "quot" { $quotation "( -- elt )" } }
1100 { "newseq" sequence } }
1101 { $description "Calls the quotation for every element of the sequence in order. However, the element is not passed to the quotation -- it is dropped, and the quotation produces an element of its own that is collected into a sequence of the same class as the input sequence." }
1103 { $unchecked-example "USING: prettyprint kernel sequences ;"
1104 "5 [ 100 random ] replicate ."
1105 "{ 52 10 45 81 30 }"
1111 { "seq" sequence } { "quot" quotation } { "exemplar" sequence }
1112 { "newseq" sequence } }
1113 { $description "Calls the quotation for every element of the sequence in order. However, the element is not passed to the quotation -- it is dropped, and the quotation produces an element of its own that is collected into a sequence of the same class as the exemplar sequence." }
1115 { $unchecked-example "USING: prettyprint kernel sequences ;"
1116 "5 [ 100 random ] B{ } replicate-as ."
1120 { replicate replicate-as } related-words
1124 { "seq" sequence } { "quot" quotation }
1125 { "trueseq" sequence } { "falseseq" sequence } }
1126 { $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" } "." }
1128 { $example "USING: prettyprint kernel math sequences ;"
1129 "{ 1 2 3 4 5 } [ even? ] partition [ . ] bi@"
1130 "{ 2 4 }\n{ 1 3 5 }"
1137 { "seq'" sequence } }
1138 { $description "Part of the virtual sequence protocol, this word is used to return an underlying array from which to look up a value at an index given by " { $link virtual@ } "." } ;
1142 { "n" integer } { "seq" sequence }
1143 { "n'" integer } { "seq'" sequence } }
1144 { $description "Part of the sequence protocol, this word translates the input index " { $snippet "n" } " into an index into the underlying storage returned by " { $link virtual-seq } "." } ;
1148 { "seq1" sequence } { "seq2" sequence } { "map-quot" quotation } { "reduce-quot" quotation }
1149 { "result" object } }
1150 { $description "Unclips the first element of each sequence and calls " { $snippet "map-quot" } " on both objects. The result of this calculation is passed, along with the rest of both sequences, to " { $link 2reduce } ", with the computed object as the identity." }
1151 { $examples { $example "USING: sequences prettyprint math ;"
1152 "{ 10 30 50 } { 200 400 600 } [ + ] [ + ] 2map-reduce ."
1158 { "quot" quotation }
1159 { "quot" quotation } { "accum1" vector } { "accum2" vector } }
1160 { $description "Creates two new vectors to accumultate values based on a predicate. The first vector accumulates values for which the predicate yields true; the second for false." } ;
1164 { "seq1" sequence } { "seq2" sequence } { "quot" quotation } }
1165 { $description "Reverse the sequences using the " { $link <reversed> } " word and calls " { $link 2each } " on the reversed sequences." }
1166 { $examples { $example "USING: sequences math prettyprint ;"
1167 "{ 10 20 30 } { 1 2 3 } [ + . ] 2reverse-each"
1173 { "seq1" sequence } { "seq2" sequence }
1174 { "rest-slice1" sequence } { "rest-slice2" sequence } { "first1" object } { "first2" object } }
1175 { $description "Unclips the first element of each sequence and leaves two slice elements and the two unclipped objects on the stack." }
1176 { $examples { $example "USING: sequences prettyprint kernel arrays ;"
1177 "{ 1 2 } { 3 4 } 2unclip-slice 4array [ . ] each"
1178 "T{ slice { from 1 } { to 2 } { seq { 1 2 } } }\nT{ slice { from 1 } { to 2 } { seq { 3 4 } } }\n1\n3"
1183 { "quot" quotation }
1184 { "quot'" quotation } { "vec" vector } }
1185 { $description "Creates a new quotation that pushes its result to a vector and outputs that vector on the stack." }
1186 { $examples { $example "USING: sequences prettyprint kernel math ;"
1187 "{ 1 2 } [ 30 + ] accumulator [ each ] dip ."
1193 { "seq" sequence } { "start" integer } { "quot" quotation }
1194 { "value" object } }
1195 { $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 } "." }
1196 { $examples "Computing factorial:"
1197 { $example "USING: prettyprint sequences math ;"
1198 "40 rest-slice 1 [ * ] binary-reduce ."
1199 "20397882081197443358640281739902897356800000000" }
1204 { "obj" object } { "quot" quotation }
1205 { "seq" sequence } }
1206 { $description "Outputs a sequence containing the input object and all of the objects generated by successively feeding the result of the quotation called on the input object to the quotation recursuively. Objects yielded by the quotation are added to the output sequence until the quotation yields " { $link f } ", at which point the recursion terminates." }
1207 { $examples "Get random numbers until zero is reached:"
1208 { $unchecked-example
1209 "USING: random sequences prettyprint math ;"
1210 "100 [ random dup zero? [ drop f ] when ] follow ."
1211 "{ 100 86 34 32 24 11 7 2 }"
1217 { "first-slice" slice } { "second-slice" slice } }
1218 { $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." }
1219 { $examples { $example "USING: arrays sequences prettyprint kernel ;"
1220 "{ 1 2 3 4 5 } halves [ >array . ] bi@"
1221 "{ 1 2 }\n{ 3 4 5 }"
1226 { "obj" object } { "seq" sequence }
1227 { "indices" sequence } }
1228 { $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." }
1229 { $examples { $example "USING: sequences prettyprint ;"
1230 "2 { 2 4 2 6 2 8 2 10 } indices ."
1236 { "elt" object } { "n" integer } { "seq" sequence }
1237 { "seq'" sequence } }
1238 { $description "Creates a new sequence where the " { $snippet "n" } "th index is set to the input object." }
1239 { $examples { $example "USING: prettyprint sequences ;"
1240 "40 3 { 10 20 30 50 } insert-nth ."
1241 "{ 10 20 30 40 50 }"
1246 { "seq" sequence } { "map-quot" quotation } { "reduce-quot" quotation }
1247 { "result" object } }
1248 { $description "Unclips the first element of the sequence, calls " { $snippet "map-quot" } " on that element, and proceeds like a " { $link reduce } ", where the calculated element is the identity element and the rest of the sequence is the sequence to reduce." }
1249 { $examples { $example "USING: sequences prettyprint math ;"
1250 "{ 1 3 5 } [ sq ] [ + ] map-reduce ."
1256 { "len" integer } { "exemplar" "an exemplar sequence" } { "quot" quotation }
1257 { "seq" sequence } }
1258 { $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." } ;
1262 { "elt" object } { "quot" quotation } { "accum1" vector } { "accum2" vector } }
1263 { $description "Pushes the input object onto one of the accumualators; the first if the quotation yields true, the second if false." } ;
1265 HELP: sequence-hashcode
1267 { "n" integer } { "seq" sequence }
1269 { $description "Iterates over a sequence, computes a hashcode with " { $link hashcode* } " for each element, and combines them using " { $link sequence-hashcode-step } "." } ;
1271 HELP: sequence-hashcode-step
1273 { "oldhash" integer } { "newpart" integer }
1274 { "newhash" integer } }
1275 { $description "An implementation word that computes a running hashcode of a sequence using some bit-twiddling. The resulting hashcode is always a fixnum." } ;
1279 { "seq" sequence } { "n" integer }
1280 { "seq" sequence } { "n'" integer } }
1281 { $description "Returns the input sequence and its length or " { $snippet "n" } ", whichever is less." }
1282 { $examples { $example "USING: sequences kernel prettyprint ;"
1283 "\"abcd\" 3 short [ . ] bi@"
1289 { "n" integer } { "seq" sequence } }
1290 { $description "Shortens a " { $link "growable" } " sequence to by " { $snippet "n" } " elements long." }
1291 { $examples { $example "USING: sequences prettyprint kernel ;"
1292 "V{ 1 2 3 4 5 } 3 over shorten ."
1297 { $values { "n" integer } { "iota" iota } }
1298 { $description "Creates an immutable virtual sequence containing the integers from 0 to " { $snippet "n-1" } "." }
1301 "USING: math sequences prettyprint ;"
1302 "3 iota [ sq ] map ."
1307 ARTICLE: "sequences-unsafe" "Unsafe sequence operations"
1308 "The " { $link nth-unsafe } " and " { $link set-nth-unsafe } " sequence protocol bypasses bounds checks for increased performance."
1310 "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."
1312 "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."
1314 "The justification for this is that the VM should not crash if a resizable sequence is resized during the execution of an iteration combinator."
1316 "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 } "." ;
1318 ARTICLE: "sequence-protocol" "Sequence protocol"
1319 "All sequences must be instances of a mixin class:"
1320 { $subsection sequence }
1321 { $subsection sequence? }
1322 "All sequences must know their length:"
1323 { $subsection length }
1324 "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:"
1326 { $subsection nth-unsafe }
1327 "Note that sequences are always indexed starting from zero."
1329 "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:"
1330 { $subsection set-nth }
1331 { $subsection set-nth-unsafe }
1332 "Note that even if the sequence is immutable, at least one of the generic words must be specialized, otherwise calling them will result in an infinite recursion. There is a standard word which throws an error indicating a sequence is immutable:"
1333 { $subsection immutable }
1334 "The following two generic words are optional, as not all sequences are resizable:"
1335 { $subsection set-length }
1336 { $subsection lengthen }
1337 "An optional generic word for creating sequences of the same class as a given sequence:"
1338 { $subsection like }
1339 "Optional generic words for optimization purposes:"
1340 { $subsection new-sequence }
1341 { $subsection new-resizable }
1342 { $see-also "sequences-unsafe" } ;
1344 ARTICLE: "virtual-sequences-protocol" "Virtual sequence protocol"
1345 "Virtual sequences must know their length:"
1346 { $subsection length }
1347 "The underlying sequence to look up a value in:"
1348 { $subsection virtual-seq }
1349 "The index of the value in the underlying sequence:"
1350 { $subsection virtual@ } ;
1352 ARTICLE: "virtual-sequences" "Virtual sequences"
1353 "Virtual sequences allow different ways of accessing a sequence without having to create a new sequence or a new data structure altogether. To do this, they translate the virtual index into a normal index into an underlying sequence using the " { $link "virtual-sequences-protocol" } "."
1354 { $subsection "virtual-sequences-protocol" } ;
1356 ARTICLE: "sequences-integers" "Integer sequences and counted loops"
1357 "Integers support the sequence protocol in a trivial fashion; a non-negative integer presents its non-negative predecessors as elements. For example, the integer 3, when viewed as a sequence, contains the elements 0, 1, and 2. This is very useful for performing counted loops."
1359 "For example, the " { $link each } " combinator, given an integer, simply calls a quotation that number of times, pushing a counter on each iteration that ranges from 0 up to that integer:"
1360 { $example "3 [ . ] each" "0\n1\n2" }
1361 "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 } "."
1363 "Combinators that produce new sequences, such as " { $link map } ", will output an array if the input is an integer." ;
1365 ARTICLE: "sequences-access" "Accessing sequence elements"
1366 { $subsection ?nth }
1367 "Concise way of extracting one of the first four elements:"
1368 { $subsection first }
1369 { $subsection second }
1370 { $subsection third }
1371 { $subsection fourth }
1372 "Unpacking sequences:"
1373 { $subsection first2 }
1374 { $subsection first3 }
1375 { $subsection first4 }
1376 { $see-also nth peek } ;
1378 ARTICLE: "sequences-add-remove" "Adding and removing sequence elements"
1380 { $subsection prefix }
1381 { $subsection suffix }
1382 "Removing elements:"
1383 { $subsection remove }
1384 { $subsection remq }
1385 { $subsection remove-nth } ;
1387 ARTICLE: "sequences-reshape" "Reshaping sequences"
1388 "A " { $emphasis "repetition" } " is a virtual sequence consisting of a single element repeated multiple times:"
1389 { $subsection repetition }
1390 { $subsection <repetition> }
1391 "Reversing a sequence:"
1392 { $subsection reverse }
1393 "A " { $emphasis "reversal" } " presents a reversed view of an underlying sequence:"
1394 { $subsection reversed }
1395 { $subsection <reversed> }
1396 "Transposing a matrix:"
1397 { $subsection flip } ;
1399 ARTICLE: "sequences-appending" "Appending sequences"
1400 { $subsection append }
1401 { $subsection append-as }
1402 { $subsection prepend }
1403 { $subsection 3append }
1404 { $subsection 3append-as }
1405 { $subsection surround }
1406 { $subsection glue }
1407 { $subsection concat }
1408 { $subsection join }
1409 "A pair of words useful for aligning strings:"
1410 { $subsection pad-left }
1411 { $subsection pad-right } ;
1413 ARTICLE: "sequences-slices" "Subsequences and slices"
1414 "Extracting a subsequence:"
1415 { $subsection subseq }
1416 { $subsection head }
1417 { $subsection tail }
1418 { $subsection head* }
1419 { $subsection tail* }
1420 "Removing the first or last element:"
1421 { $subsection rest }
1422 { $subsection but-last }
1423 "Taking a sequence apart into a head and a tail:"
1424 { $subsection unclip }
1425 { $subsection unclip-last }
1427 { $subsection cut* }
1428 "A " { $emphasis "slice" } " is a virtual sequence which presents as view of a subsequence of an underlying sequence:"
1429 { $subsection slice }
1430 { $subsection slice? }
1431 "Extracting a slice:"
1432 { $subsection <slice> }
1433 { $subsection head-slice }
1434 { $subsection tail-slice }
1435 { $subsection head-slice* }
1436 { $subsection tail-slice* }
1437 "Removing the first or last element:"
1438 { $subsection rest-slice }
1439 { $subsection but-last-slice }
1440 "Taking a sequence apart into a head and a tail:"
1441 { $subsection unclip-slice }
1442 { $subsection unclip-last-slice }
1443 { $subsection cut-slice }
1444 "A utility for words which use slices as iterators:"
1445 { $subsection <flat-slice> } ;
1447 ARTICLE: "sequences-combinators" "Sequence combinators"
1449 { $subsection each }
1450 { $subsection each-index }
1451 { $subsection reduce }
1452 { $subsection interleave }
1453 { $subsection replicate }
1454 { $subsection replicate-as }
1457 { $subsection map-as }
1458 { $subsection map-index }
1459 { $subsection accumulate }
1460 { $subsection produce }
1461 { $subsection produce-as }
1463 { $subsection push-if }
1464 { $subsection filter }
1465 "Testing if a sequence contains elements satisfying a predicate:"
1466 { $subsection contains? }
1467 { $subsection all? }
1468 { $subsection "sequence-2combinators" }
1469 { $subsection "sequence-3combinators" } ;
1471 ARTICLE: "sequence-2combinators" "Pair-wise sequence combinators"
1472 "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."
1473 { $subsection 2each }
1474 { $subsection 2reduce }
1475 { $subsection 2map }
1476 { $subsection 2map-as }
1477 { $subsection 2all? } ;
1479 ARTICLE: "sequence-3combinators" "Triple-wise sequence combinators"
1480 "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."
1481 { $subsection 3each }
1482 { $subsection 3map }
1483 { $subsection 3map-as } ;
1485 ARTICLE: "sequences-tests" "Testing sequences"
1486 "Testing for an empty sequence:"
1487 { $subsection empty? }
1489 { $subsection bounds-check? }
1490 "Testing if a sequence contains an object:"
1491 { $subsection member? }
1492 { $subsection memq? }
1493 "Testing if a sequence contains a subsequence:"
1494 { $subsection head? }
1495 { $subsection tail? }
1496 { $subsection subseq? } ;
1498 ARTICLE: "sequences-search" "Searching sequences"
1499 "Finding the index of an element:"
1500 { $subsection index }
1501 { $subsection index-from }
1502 { $subsection last-index }
1503 { $subsection last-index-from }
1504 "Finding the start of a subsequence:"
1505 { $subsection start }
1506 { $subsection start* }
1507 "Finding the index of an element satisfying a predicate:"
1508 { $subsection find }
1509 { $subsection find-from }
1510 { $subsection find-last }
1511 { $subsection find-last-from } ;
1513 ARTICLE: "sequences-trimming" "Trimming sequences"
1515 { $subsection trim }
1516 { $subsection trim-left }
1517 { $subsection trim-right }
1518 "Potentially more efficient trim:"
1519 { $subsection trim-slice }
1520 { $subsection trim-left-slice }
1521 { $subsection trim-right-slice } ;
1523 ARTICLE: "sequences-destructive-discussion" "When to use destructive operations"
1524 "Constructive (non-destructive) operations should be preferred where possible because code without side-effects is usually more re-usable and easier to reason about. There are two main reasons to use destructive operations:"
1526 "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."
1527 { "As an optimization. Some code can be written to use constructive operations, however would suffer from worse performance. An example is a loop which adds an element to a sequence on each iteration; one could use either " { $link suffix } " or " { $link push } ", however the former copies the entire sequence first, which would cause the loop to run in quadratic time." }
1529 "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." ;
1531 ARTICLE: "sequences-destructive" "Destructive operations"
1532 "These words modify their input, instead of creating a new sequence."
1533 { $subsection "sequences-destructive-discussion" }
1534 "Changing elements:"
1535 { $subsection change-each }
1536 { $subsection change-nth }
1537 "Deleting elements:"
1538 { $subsection delete }
1539 { $subsection delq }
1540 { $subsection delete-nth }
1541 { $subsection delete-slice }
1542 { $subsection delete-all }
1543 { $subsection filter-here }
1544 "Other destructive words:"
1545 { $subsection reverse-here }
1546 { $subsection push-all }
1547 { $subsection move }
1548 { $subsection exchange }
1549 { $subsection copy }
1550 { $subsection replace-slice }
1551 "Many operations have constructive and destructive variants:"
1553 { "Constructive" "Destructive" }
1554 { { $link suffix } { $link push } }
1555 { { $link but-last } { $link pop* } }
1556 { { $link unclip-last } { $link pop } }
1557 { { $link remove } { $link delete } }
1558 { { $link remq } { $link delq } }
1559 { { $link remove-nth } { $link delete-nth } }
1560 { { $link reverse } { $link reverse-here } }
1561 { { $link append } { $link push-all } }
1562 { { $link map } { $link change-each } }
1563 { { $link filter } { $link filter-here } }
1565 { $see-also set-nth push pop "sequences-stacks" } ;
1567 ARTICLE: "sequences-stacks" "Treating sequences as stacks"
1568 "The classical stack operations, modifying a sequence in place:"
1569 { $subsection peek }
1570 { $subsection push }
1572 { $subsection pop* }
1573 { $see-also empty? } ;
1575 ARTICLE: "sequences-comparing" "Comparing sequences"
1576 "Element equality testing:"
1577 { $subsection sequence= }
1578 { $subsection mismatch }
1579 { $subsection drop-prefix }
1580 "The " { $link <=> } " generic word performs lexicographic comparison when applied to sequences." ;
1582 ARTICLE: "sequences-f" "The f object as a sequence"
1583 "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." ;
1585 ARTICLE: "sequences" "Sequence operations"
1586 "A " { $emphasis "sequence" } " is a finite, linearly-ordered collection of elements. Words for working with sequences are in the " { $vocab-link "sequences" } " vocabulary."
1588 "Sequences implement a protocol:"
1589 { $subsection "sequence-protocol" }
1590 { $subsection "sequences-f" }
1591 { $subsection "sequences-integers" }
1592 "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" } "."
1593 { $subsection "sequences-access" }
1594 { $subsection "sequences-combinators" }
1595 { $subsection "sequences-add-remove" }
1596 { $subsection "sequences-appending" }
1597 { $subsection "sequences-slices" }
1598 { $subsection "sequences-reshape" }
1599 { $subsection "sequences-tests" }
1600 { $subsection "sequences-search" }
1601 { $subsection "sequences-comparing" }
1602 { $subsection "sequences-split" }
1603 { $subsection "grouping" }
1604 { $subsection "sequences-destructive" }
1605 { $subsection "sequences-stacks" }
1606 { $subsection "sequences-sorting" }
1607 { $subsection "binary-search" }
1608 { $subsection "sets" }
1609 { $subsection "sequences-trimming" }
1611 { $subsection "sequences-unsafe" } ;