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