]> 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 ;
3 IN: sequences
4
5 ARTICLE: "sequences-unsafe" "Unsafe sequence operations"
6 "The " { $link nth-unsafe } " and " { $link set-nth-unsafe } " sequence protocol bypasses bounds checks for increased performance."
7 $nl
8 "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."
9 $nl
10 "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."
11 $nl
12 "The justification for this is that the VM should not crash if a resizable sequence is resized during the execution of an iteration combinator."
13 $nl
14 "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 } "." ;
15
16 ARTICLE: "sequence-protocol" "Sequence protocol"
17 "All sequences must be instances of a mixin class:"
18 { $subsection sequence }
19 { $subsection sequence? }
20 "All sequences must know their length:"
21 { $subsection length }
22 "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:"
23 { $subsection nth }
24 { $subsection nth-unsafe }
25 "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:"
26 { $subsection set-nth }
27 { $subsection set-nth-unsafe }
28 "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:"
29 { $subsection immutable }
30 "The following two generic words are optional, as not all sequences are resizable:"
31 { $subsection set-length }
32 { $subsection lengthen }
33 "An optional generic word for creating sequences of the same class as a given sequence:"
34 { $subsection like }
35 "Optional generic words for optimization purposes:"
36 { $subsection new-sequence }
37 { $subsection new-resizable }
38 { $see-also "sequences-unsafe" } ;
39
40 ARTICLE: "sequences-integers" "Integer sequences and counted loops"
41 "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."
42 $nl
43 "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:"
44 { $example "3 [ . ] each" "0\n1\n2" }
45 "A common idiom is to iterate over a sequence, while also maintaining a loop counter. This can be done using " { $link 2each } ":"
46 { $example "{ \"a\" \"b\" \"c\" } dup length [\n    \"Index: \" write . \"Element: \" write .\n] 2each" "Index: 0\nElement: \"a\"\nIndex: 1\nElement: \"b\"\nIndex: 2\nElement: \"c\"" }
47 "Combinators that produce new sequences, such as " { $link map } ", will output an array if the input is an integer." ;
48
49 ARTICLE: "sequences-access" "Accessing sequence elements"
50 { $subsection ?nth }
51 "Concise way of extracting one of the first four elements:"
52 { $subsection first }
53 { $subsection second }
54 { $subsection third }
55 { $subsection fourth }
56 "Unpacking sequences:"
57 { $subsection first2 }
58 { $subsection first3 }
59 { $subsection first4 }
60 { $see-also nth peek } ;
61
62 ARTICLE: "sequences-add-remove" "Adding and removing sequence elements"
63 "Adding elements:"
64 { $subsection prefix }
65 { $subsection suffix }
66 "Removing elements:"
67 { $subsection remove } ;
68
69 ARTICLE: "sequences-reshape" "Reshaping sequences"
70 "A " { $emphasis "repetition" } " is a virtual sequence consisting of a single element repeated multiple times:"
71 { $subsection repetition }
72 { $subsection <repetition> }
73 "Reversing a sequence:"
74 { $subsection reverse }
75 "A " { $emphasis "reversal" } " presents a reversed view of an underlying sequence:"
76 { $subsection reversed }
77 { $subsection <reversed> }
78 "Transposing a matrix:"
79 { $subsection flip } ;
80
81 ARTICLE: "sequences-appending" "Appending sequences"
82 { $subsection append }
83 { $subsection 3append }
84 { $subsection concat }
85 { $subsection join }
86 "A pair of words useful for aligning strings:"
87 { $subsection pad-left }
88 { $subsection pad-right } ;
89
90 ARTICLE: "sequences-slices" "Subsequences and slices"
91 "Extracting a subsequence:"
92 { $subsection subseq }
93 { $subsection head }
94 { $subsection tail }
95 { $subsection head* }
96 { $subsection tail* }
97 "Removing the first or last element:"
98 { $subsection rest }
99 { $subsection but-last }
100 "Taking a sequence apart into a head and a tail:"
101 { $subsection unclip }
102 { $subsection cut }
103 { $subsection cut* }
104 "A " { $emphasis "slice" } " is a virtual sequence which presents as view of a subsequence of an underlying sequence:"
105 { $subsection slice }
106 { $subsection slice? }
107 "Creating slices:"
108 { $subsection <slice> }
109 { $subsection head-slice }
110 { $subsection tail-slice }
111 { $subsection but-last-slice }
112 { $subsection rest-slice }
113 { $subsection head-slice* }
114 { $subsection tail-slice* }
115 "Taking a sequence apart into a head and a tail:"
116 { $subsection unclip-slice }
117 { $subsection cut-slice }
118 "A utility for words which use slices as mutable iterators:"
119 { $subsection <flat-slice> } ;
120
121 ARTICLE: "sequences-combinators" "Sequence combinators"
122 "Iteration:"
123 { $subsection each }
124 { $subsection reduce }
125 { $subsection interleave }
126 { $subsection 2each }
127 { $subsection 2reduce }
128 "Mapping:"
129 { $subsection map }
130 { $subsection 2map }
131 { $subsection accumulate }
132 { $subsection unfold }
133 "Filtering:"
134 { $subsection push-if }
135 { $subsection filter } ;
136
137 ARTICLE: "sequences-tests" "Testing sequences"
138 "Testing for an empty sequence:"
139 { $subsection empty? }
140 "Testing indices:"
141 { $subsection bounds-check? }
142 "Testing if a sequence contains an object:"
143 { $subsection member? }
144 { $subsection memq? }
145 "Testing if a sequence contains a subsequence:"
146 { $subsection head? }
147 { $subsection tail? }
148 { $subsection subseq? }
149 "Testing if a sequence contains elements satisfying a predicate:"
150 { $subsection contains? }
151 { $subsection all? }
152 { $subsection 2all? }
153 "Testing how elements are related:"
154 { $subsection monotonic? }
155 { $subsection all-eq? }
156 { $subsection all-equal? } ;
157
158 ARTICLE: "sequences-search" "Searching sequences"
159 "Finding the index of an element:"
160 { $subsection index }
161 { $subsection index-from }
162 { $subsection last-index }
163 { $subsection last-index-from }
164 "Finding the start of a subsequence:"
165 { $subsection start }
166 { $subsection start* }
167 "Finding the index of an element satisfying a predicate:"
168 { $subsection find }
169 { $subsection find-from }
170 { $subsection find-last }
171 { $subsection find-last-from } ;
172
173 ARTICLE: "sequences-destructive" "Destructive operations"
174 "These words modify their input, instead of creating a new sequence."
175 $nl
176 "In-place variant of " { $link reverse } ":"
177 { $subsection reverse-here }
178 "In-place variant of " { $link append } ":"
179 { $subsection push-all }
180 "In-place variant of " { $link remove } ":"
181 { $subsection delete }
182 "In-place variant of " { $link map } ":"
183 { $subsection change-each }
184 "Changing elements:"
185 { $subsection change-nth }
186 { $subsection cache-nth }
187 "Deleting elements:"
188 { $subsection delete-nth }
189 { $subsection delete-slice }
190 { $subsection delete-all }
191 "Other destructive words:"
192 { $subsection move }
193 { $subsection exchange }
194 { $subsection copy }
195 { $subsection replace-slice }
196 { $see-also set-nth push pop "sequences-stacks" } ;
197
198 ARTICLE: "sequences-stacks" "Treating sequences as stacks"
199 "The classical stack operations, modifying a sequence in place:"
200 { $subsection peek }
201 { $subsection push }
202 { $subsection pop }
203 { $subsection pop* }
204 { $see-also empty? } ;
205
206 ARTICLE: "sequences-comparing" "Comparing sequences"
207 "Element equality testing:"
208 { $subsection sequence= }
209 { $subsection mismatch }
210 { $subsection drop-prefix }
211 "The " { $link <=> } " generic word performs lexicographic comparison when applied to sequences." ;
212
213 ARTICLE: "sequences-f" "The f object as a sequence"
214 "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." ;
215
216 ARTICLE: "sequences" "Sequence operations"
217 "A " { $emphasis "sequence" } " is a finite, linearly-ordered collection of elements. Words for working with sequences are in the " { $vocab-link "sequences" } " vocabulary."
218 $nl
219 "Sequences implement a protocol:"
220 { $subsection "sequence-protocol" }
221 { $subsection "sequences-f" }
222 { $subsection "sequences-integers" }
223 "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 " { $emphasis "virtual sequences" } "."
224 { $subsection "sequences-access" }
225 { $subsection "sequences-combinators" }
226 { $subsection "sequences-add-remove" }
227 { $subsection "sequences-appending" }
228 { $subsection "sequences-slices" }
229 { $subsection "sequences-reshape" }
230 { $subsection "sequences-tests" }
231 { $subsection "sequences-search" }
232 { $subsection "sequences-comparing" }
233 { $subsection "sequences-split" }
234 { $subsection "grouping" }
235 { $subsection "sequences-destructive" }
236 { $subsection "sequences-stacks" }
237 { $subsection "sequences-sorting" }
238 { $subsection "sets" }
239 "For inner loops:"
240 { $subsection "sequences-unsafe" } ;
241
242 ABOUT: "sequences"
243
244 HELP: sequence
245 { $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:"
246     { $code "INSTANCE: my-sequence sequence" }
247 } ;
248
249 HELP: length
250 { $values { "seq" sequence } { "n" "a non-negative integer" } }
251 { $contract "Outputs the length of the sequence. All sequences support this operation." } ;
252
253 HELP: set-length
254 { $values { "n" "a non-negative integer" } { "seq" "a resizable sequence" } }
255 { $contract "Resizes the sequence. Not all sequences are resizable." }
256 { $errors "Throws a " { $link bounds-error } " if the new length is negative." }
257 { $side-effects "seq" } ;
258
259 HELP: lengthen
260 { $values { "n" "a non-negative integer" } { "seq" "a resizable sequence" } }
261 { $contract "Ensures the sequence has a length of at least " { $snippet "n" } " elements. This word differs from " { $link set-length } " in two respects:"
262     { $list
263         { "This word does not shrink the sequence if " { $snippet "n" } " is less than its length." }
264         { "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." }
265     }
266 } ;
267
268 HELP: nth
269 { $values { "n" "a non-negative integer" } { "seq" sequence } { "elt" "the element at the " { $snippet "n" } "th index" } }
270 { $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." }
271 { $errors "Throws a " { $link bounds-error } " if the index is negative, or greater than or equal to the length of the sequence." } ;
272
273 HELP: set-nth
274 { $values { "elt" object } { "n" "a non-negative integer" } { "seq" "a mutable sequence" } }
275 { $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." }
276 { $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."
277 $nl
278 "Throws an error if the sequence cannot hold elements of the given type." }
279 { $side-effects "seq" } ;
280
281 HELP: immutable
282 { $values { "seq" sequence } }
283 { $description "Throws an " { $link immutable } " error." }
284 { $error-description "Thrown if an attempt is made to modify an immutable sequence." } ;
285
286 HELP: new-sequence
287 { $values { "len" "a non-negative integer" } { "seq" sequence } { "newseq" "a mutable sequence" } }
288 { $contract "Outputs a mutable sequence of length " { $snippet "n" } " which can hold the elements of " { $snippet "seq" } "." } ;
289
290 HELP: new-resizable
291 { $values { "len" "a non-negative integer" } { "seq" sequence } { "newseq" "a resizable mutable sequence" } }
292 { $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" } "." }
293 { $examples
294     { $example "USING: prettyprint sequences ;" "300 V{ } new-resizable ." "V{ }" }
295     { $example "USING: prettyprint sequences ;" "300 SBUF\" \" new-resizable ." "SBUF\" \"" }
296 } ;
297
298 HELP: like
299 { $values { "seq" sequence } { "exemplar" sequence } { "newseq" "a new sequence" } }
300 { $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."
301 $nl
302 "The default implementation does nothing." }
303 { $notes "Unlike " { $link clone-like } ", the output sequence might share storage with the input sequence." } ;
304
305 HELP: empty?
306 { $values { "seq" sequence } { "?" "a boolean" } }
307 { $description "Tests if the sequence has zero length." } ;
308
309 HELP: delete-all
310 { $values { "seq" "a resizable sequence" } }
311 { $description "Resizes the sequence to zero length, removing all elements. Not all sequences are resizable." }
312 { $errors "Throws a " { $link bounds-error } " if the new length is negative, or if the sequence is not resizable." }
313 { $side-effects "seq" } ;
314
315 HELP: resize
316 { $values { "n" "a non-negative integer" } { "seq" sequence } { "newseq" "a new sequence" } }
317 { $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." }
318 { $notes "This generic word is only implemented for strings and arrays." } ;
319
320 HELP: first
321 { $values { "seq" sequence } { "first" "the first element of the sequence" } }
322 { $description "Outputs the first element of the sequence." }
323 { $errors "Throws an error if the sequence is empty." } ;
324
325 HELP: second
326 { $values { "seq" sequence } { "second" "the second element of the sequence" } }
327 { $description "Outputs the second element of the sequence." }
328 { $errors "Throws an error if the sequence contains less than two elements." } ;
329
330 HELP: third
331 { $values { "seq" sequence } { "third" "the third element of the sequence" } }
332 { $description "Outputs the third element of the sequence." }
333 { $errors "Throws an error if the sequence contains less than three elements." } ;
334
335 HELP: fourth
336 { $values { "seq" sequence } { "fourth" "the fourth element of the sequence" } }
337 { $description "Outputs the fourth element of the sequence." }
338 { $errors "Throws an error if the sequence contains less than four elements." } ;
339
340 HELP: push
341 { $values { "elt" object } { "seq" "a resizable mutable sequence" } }
342 { $description "Adds an element at the end of the sequence. The sequence length is adjusted accordingly." }
343 { $errors "Throws an error if " { $snippet "seq" } " is not resizable, or if the type of " { $snippet "elt" } " is not permitted in " { $snippet "seq" } "." }
344 { $side-effects "seq" } ;
345
346 HELP: bounds-check?
347 { $values { "n" "an integer" } { "seq" sequence } { "?" "a boolean" } }
348 { $description "Tests if the index is within the bounds of the sequence." } ;
349
350 HELP: bounds-error
351 { $values { "n" "a positive integer" } { "seq" sequence } }
352 { $description "Throws a " { $link bounds-error } "." }
353 { $error-description "Thrown by " { $link nth } ", " { $link set-nth } " and " { $link set-length } " if the given index lies beyond the bounds of the sequence." } ;
354
355 HELP: bounds-check
356 { $values { "n" "a positive integer" } { "seq" sequence } }
357 { $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." } ;
358
359 HELP: ?nth
360 { $values { "n" "an integer" } { "seq" sequence } { "elt/f" "an object or " { $link f } } }
361 { $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 } "." } ;
362
363 HELP: nth-unsafe
364 { $values { "n" "an integer" } { "seq" sequence } { "elt" object } }
365 { $contract "Unsafe variant of " { $link nth } " that does not perform bounds checks." } ;
366
367 HELP: set-nth-unsafe
368 { $values { "elt" object } { "n" "an integer" } { "seq" sequence } }
369 { $contract "Unsafe variant of " { $link set-nth } " that does not perform bounds checks." } ;
370
371 HELP: exchange-unsafe
372 { $values { "m" "a non-negative integer" } { "n" "a non-negative integer" } { "seq" "a mutable sequence" } }
373 { $description "Unsafe variant of " { $link exchange } " that does not perform bounds checks." } ;
374
375 HELP: first2-unsafe
376 { $values { "seq" sequence } { "first" "the first element" } { "second" "the second element" } }
377 { $contract "Unsafe variant of " { $link first2 } " that does not perform bounds checks." } ;
378
379 HELP: first3-unsafe
380 { $values { "seq" sequence } { "first" "the first element" } { "second" "the second element" } { "third" "the third element" } }
381 { $contract "Unsafe variant of " { $link first3 } " that does not perform bounds checks." } ;
382
383 HELP: first4-unsafe
384 { $values { "seq" sequence } { "first" "the first element" } { "second" "the second element" } { "third" "the third element" } { "fourth" "the fourth element" } }
385 { $contract "Unsafe variant of " { $link first4 } " that does not perform bounds checks." } ;
386
387 HELP: 2sequence
388 { $values { "obj1" object } { "obj2" object } { "exemplar" sequence } { "seq" sequence } }
389 { $description "Creates a two-element sequence of the same type as " { $snippet "exemplar" } "." } ;
390
391 HELP: 3sequence
392 { $values { "obj1" object } { "obj2" object } { "obj3" object } { "exemplar" sequence } { "seq" sequence } }
393 { $description "Creates a three-element sequence of the same type as " { $snippet "exemplar" } "." } ;
394
395 HELP: 4sequence
396 { $values { "obj1" object } { "obj2" object } { "exemplar" sequence } { "obj3" object } { "obj4" object } { "seq" sequence } }
397 { $description "Creates a four-element sequence of the same type as " { $snippet "exemplar" } "." } ;
398
399 HELP: first2
400 { $values { "seq" sequence } { "first" "the first element" } { "second" "the second element" } }
401 { $description "Pushes the first two elements of a sequence." }
402 { $errors "Throws an error if the sequence has less than two elements." } ;
403
404 HELP: first3
405 { $values { "seq" sequence } { "first" "the first element" } { "second" "the second element" } { "third" "the third element" } }
406 { $description "Pushes the first three elements of a sequence." }
407 { $errors "Throws an error if the sequence has less than three elements." } ;
408
409 HELP: first4
410 { $values { "seq" sequence } { "first" "the first element" } { "second" "the second element" } { "third" "the third element" } { "fourth" "the fourth element" } }
411 { $description "Pushes the first four elements of a sequence." }
412 { $errors "Throws an error if the sequence has less than four elements." } ;
413
414 HELP: array-capacity
415 { $values { "array" "an array" } { "n" "a non-negative fixnum" } }
416 { $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 } "." }
417 { $description "Low-level array length accessor." }
418 { $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." } ;
419
420 HELP: array-nth
421 { $values { "n" "a non-negative fixnum" } { "array" "an array" }  { "elt" object } }
422 { $description "Low-level array element accessor." }
423 { $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." } ;
424
425 HELP: set-array-nth
426 { $values { "elt" object } { "n" "a non-negative fixnum" } { "array" "an array" }  }
427 { $description "Low-level array element mutator." }
428 { $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." } ;
429
430 HELP: collect
431 { $values { "n" "a non-negative integer" } { "quot" "a quotation with stack effect " { $snippet "( n -- value )" } } { "into" "a sequence of length at least " { $snippet "n" } } }
432 { $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." } ;
433
434 HELP: each
435 { $values { "seq" sequence } { "quot" "a quotation with stack effect " { $snippet "( elt -- )" } } }
436 { $description "Applies the quotation to each element of the sequence in order." } ;
437
438 HELP: reduce
439 { $values { "seq" sequence } { "identity" object } { "quot" "a quotation with stack effect " { $snippet "( prev elt -- next )" } } { "result" "the final result" } }
440 { $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." }
441 { $examples
442     { $example "USING: math prettyprint sequences ;" "{ 1 5 3 } 0 [ + ] reduce ." "9" }
443 } ;
444
445 HELP: accumulate
446 { $values { "identity" object } { "seq" sequence } { "quot" "a quotation with stack effect " { $snippet "( prev elt -- next )" } } { "final" "the final result" } { "newseq" "a new sequence" } }
447 { $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."
448 $nl
449 "When given the empty sequence, outputs an empty sequence together with the " { $snippet "identity" } "." }
450 { $examples
451     { $example "USING: math prettyprint sequences ;" "{ 2 2 2 2 2 } 0 [ + ] accumulate . ." "{ 0 2 4 6 8 }\n10" }
452 } ;
453
454 HELP: map
455 { $values { "seq" sequence } { "quot" "a quotation with stack effect " { $snippet "( old -- new )" } } { "newseq" "a new sequence" } }
456 { $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." } ;
457
458 HELP: change-nth
459 { $values { "i" "a non-negative integer" } { "seq" "a mutable sequence" } { "quot" "a quotation with stack effect " { $snippet "( elt -- newelt )" } } }
460 { $description "Applies the quotation to the " { $snippet "i" } "th element of the sequence, storing the result back into the sequence." }
461 { $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" } "." }
462 { $side-effects "seq" } ;
463
464 HELP: change-each
465 { $values { "seq" "a mutable sequence" } { "quot" "a quotation with stack effect " { $snippet "( old -- new )" } } }
466 { $description "Applies the quotation to each element yielding a new element, storing the new elements back in the original sequence." }
467 { $errors "Throws an error if the sequence is immutable, or the sequence cannot hold elements of the type output by " { $snippet "quot" } "." }
468 { $side-effects "seq" } ;
469
470 HELP: min-length
471 { $values { "seq1" sequence } { "seq2" sequence } { "n" "a non-negative integer" } }
472 { $description "Outputs the minimum of the lengths of the two sequences." } ;
473
474 HELP: max-length
475 { $values { "seq1" sequence } { "seq2" sequence } { "n" "a non-negative integer" } }
476 { $description "Outputs the maximum of the lengths of the two sequences." } ;
477
478 HELP: 2each
479 { $values { "seq1" sequence } { "seq2" sequence } { "quot" "a quotation with stack effect " { $snippet "( elt1 elt2 -- )" } } }
480 { $description "Applies the quotation to pairs of elements from " { $snippet "seq1" } " and " { $snippet "seq2" } "." }
481 { $notes "If one sequence is shorter than the other, than only the prefix having the length of the minimum of the two is examined." } ;
482
483 HELP: 2reduce
484 { $values { "seq1" sequence }
485           { "seq2" sequence }
486           { "identity" object }
487           { "quot" "a quotation with stack effect "
488                    { $snippet "( prev elt1 elt2 -- next )" } }
489           { "result" "the final result" } }
490 { $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" } "." }
491 { $notes "If one sequence is shorter than the other, then only the prefix having the length of the minimum of the two is examined." } ;
492
493 HELP: 2map
494 { $values { "seq1" sequence } { "seq2" sequence } { "quot" "a quotation with stack effect " { $snippet "( elt1 elt2 -- new )" } } { "newseq" "a new sequence" } }
495 { $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" } "." }
496 { $notes "If one sequence is shorter than the other, than only the prefix having the length of the minimum of the two is examined." } ;
497
498 HELP: 2all?
499 { $values { "seq1" sequence } { "seq2" sequence } { "quot" "a quotation with stack effect " { $snippet "( elt1 elt2 -- ? )" } } { "?" "a boolean" } }
500 { $description "Tests the predicate pairwise against elements of " { $snippet "seq1" } " and " { $snippet "seq2" } "." }
501 { $notes "If one sequence is shorter than the other, than only the prefix having the length of the minimum of the two is examined." } ;
502
503 HELP: find
504 { $values { "seq" sequence }
505           { "quot" "a quotation with stack effect "
506                    { $snippet "( elt -- ? )" } }
507           { "i" "the index of the first match, or f" }
508           { "elt" "the first matching element, or " { $link f } } }
509 { $description "A simpler variant of " { $link find-from } " where the starting index is 0." } ;
510
511 HELP: find-from
512 { $values { "n" "a starting index" }
513           { "seq" sequence }
514           { "quot" "a quotation with stack effect "
515                    { $snippet "( elt -- ? )" } }
516           { "i" "the index of the first match, or f" }
517           { "elt" "the first matching element, or " { $link f } } }
518 { $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." } ;
519
520 HELP: find-last
521 { $values { "seq" sequence } { "quot" "a quotation with stack effect " { $snippet "( elt -- ? )" } } { "i" "the index of the first match, or f" } { "elt" "the first matching element, or " { $link f } } }
522 { $description "A simpler variant of " { $link find-last-from } " where the starting index is one less than the length of the sequence." } ;
523
524 HELP: find-last-from
525 { $values { "n" "a starting index" } { "seq" sequence } { "quot" "a quotation with stack effect " { $snippet "( elt -- ? )" } } { "i" "the index of the first match, or f" } { "elt" "the first matching element, or " { $link f } } }
526 { $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." } ;
527
528 HELP: contains?
529 { $values { "seq" sequence } { "quot" "a quotation with stack effect " { $snippet "( elt -- ? )" } } { "?" "a boolean" } }
530 { $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 } "." } ;
531
532 HELP: all?
533 { $values { "seq" sequence } { "quot" "a quotation with stack effect " { $snippet "( elt -- ? )" } } { "?" "a boolean" } }
534 { $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 } "." } ;
535
536 HELP: push-if
537 { $values { "elt" object } { "quot" "a quotation with stack effect " { $snippet "( elt -- ? )" } } { "accum" "a resizable mutable sequence" } }
538 { $description "Adds the element at the end of the sequence if the quotation yields a true value." } 
539 { $notes "This word is a factor of " { $link filter } "." } ;
540
541 HELP: filter
542 { $values { "seq" sequence } { "quot" "a quotation with stack effect " { $snippet "( elt -- ? )" } } { "subseq" "a new sequence" } }
543 { $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." } ;
544
545 HELP: monotonic?
546 { $values { "seq" sequence } { "quot" "a quotation with stack effect " { $snippet "( elt elt -- ? )" } } { "?" "a boolean" } }
547 { $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." }
548 { $examples
549     "Testing if a sequence is non-decreasing:"
550     { $example "USING: math prettyprint sequences ;" "{ 1 1 2 } [ <= ] monotonic? ." "t" }
551     "Testing if a sequence is decreasing:"
552     { $example "USING: math prettyprint sequences ;" "{ 9 8 6 7 } [ < ] monotonic? ." "f" }
553 } ;
554
555 { monotonic? all-eq? all-equal? } related-words
556
557 HELP: interleave
558 { $values { "seq" sequence } { "between" "a quotation" } { "quot" "a quotation with stack effect " { $snippet "( elt -- )" } } }
559 { $description "Applies " { $snippet "quot" } " to each element in turn, also invoking " { $snippet "between" } " in-between each pair of elements." }
560 { $example "USING: io sequences ;" "{ \"a\" \"b\" \"c\" } [ \"X\" write ] [ write ] interleave" "aXbXc" } ;
561
562 HELP: cache-nth
563 { $values { "i" "a non-negative integer" } { "seq" "a mutable sequence" } { "quot" "a quotation with stack effect " { $snippet "( i -- elt )" } } { "elt" object } }
564 { $description "If the sequence does not contain at least " { $snippet "i" } " elements or if the " { $snippet "i" } "th element of the sequence is " { $link f } ", calls the quotation to produce a new value, and stores it back into the sequence. Otherwise, this word outputs the " { $snippet "i" } "th element of the sequence." }
565 { $side-effects "seq" } ;
566
567 HELP: index
568 { $values { "obj" object } { "seq" sequence } { "n" "an index" } }
569 { $description "Outputs the index of the first element in the sequence equal to " { $snippet "obj" } ". If no element is found, outputs " { $link f } "." } ;
570
571 { index index-from last-index last-index-from member? memq? } related-words
572
573 HELP: index-from
574 { $values { "obj" object } { "i" "a start index" } { "seq" sequence } { "n" "an index" } }
575 { $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 } "." } ;
576
577 HELP: last-index
578 { $values { "obj" object } { "seq" sequence } { "n" "an index" } }
579 { $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 } "." } ;
580
581 HELP: last-index-from
582 { $values { "obj" object } { "i" "a start index" } { "seq" sequence } { "n" "an index" } }
583 { $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 } "." } ;
584
585 HELP: member?
586 { $values { "obj" object } { "seq" sequence } { "?" "a boolean" } }
587 { $description "Tests if the sequence contains an element equal to the object." } ;
588
589 HELP: memq?
590 { $values { "obj" object } { "seq" sequence } { "?" "a boolean" } }
591 { $description "Tests if the sequence contains the object." }
592 { $examples
593     "This word uses identity comparison, so the following will most likely print " { $link f } ":"
594     { $example "USING: prettyprint sequences ;" "\"hello\" { \"hello\" } memq? ." "f" }
595 } ;
596
597 HELP: remove
598 { $values { "obj" object } { "seq" sequence } { "newseq" "a new sequence" } }
599 { $description "Outputs a new sequence containing all elements of the input sequence except those equal to the given element." } ;
600
601 HELP: move
602 { $values { "from" "an index in " { $snippet "seq" } } { "to" "an index in " { $snippet "seq" } } { "seq" "a mutable sequence" } }
603 { $description "Sets the element with index " { $snippet "m" } " to the element with index " { $snippet "n" } "." }
604 { $side-effects "seq" } ;
605
606 HELP: delete
607 { $values { "elt" object } { "seq" "a resizable mutable sequence" } }
608 { $description "Removes all elements equal to " { $snippet "elt" } " from " { $snippet "seq" } "." }
609 { $side-effects "seq" } ;
610
611 HELP: delete-nth
612 { $values { "n" "a non-negative integer" } { "seq" "a resizable mutable sequence" } }
613 { $description "Removes the " { $snippet "n" } "th element from the sequence, shifting all other elements down and reducing its length by one." }
614 { $side-effects "seq" } ;
615
616 HELP: delete-slice
617 { $values { "from" "a non-negative integer" } { "to" "a non-negative integer" } { "seq" "a resizable mutable sequence" } }
618 { $description "Removes a range of elements beginning at index " { $snippet "from" } " and ending before index " { $snippet "to" } "." }
619 { $side-effects "seq" } ;
620
621 HELP: replace-slice
622 { $values { "new" sequence } { "seq" "a mutable sequence" } { "from" "a non-negative integer" } { "to" "a non-negative integer" } }
623 { $description "Replaces a range of elements beginning at index " { $snippet "from" } " and ending before index " { $snippet "to" } " with a new sequence." }
624 { $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." }
625 { $errors "Throws an error if " { $snippet "new" } " contains elements whose types are not permissible in " { $snippet "seq" } "." }
626 { $side-effects "seq" } ;
627
628 { push prefix suffix } related-words
629
630 HELP: suffix
631 { $values { "seq" sequence } { "elt" object } { "newseq" sequence } }
632 { $description "Outputs a new sequence obtained by adding " { $snippet "elt" } " at the end of " { $snippet "seq" } "." }
633 { $errors "Throws an error if the type of " { $snippet "elt" } " is not permitted in sequences of the same class as " { $snippet "seq1" } "." }
634 { $examples
635     { $example "USING: prettyprint sequences ;" "{ 1 2 3 } 4 suffix ." "{ 1 2 3 4 }" }
636 } ;
637
638 HELP: prefix
639 { $values { "seq" sequence } { "elt" object } { "newseq" sequence } }
640 { $description "Outputs a new sequence obtained by adding " { $snippet "elt" } " at the beginning of " { $snippet "seq" } "." }
641 { $errors "Throws an error if the type of " { $snippet "elt" } " is not permitted in sequences of the same class as " { $snippet "seq1" } "." } 
642 { $examples
643 { $example "USING: prettyprint sequences ;" "{ 1 2 3 } 0 prefix ." "{ 0 1 2 3 }" }
644 } ;
645
646 HELP: sum-lengths
647 { $values { "seq" "a sequence of sequences" } { "n" integer } }
648 { $description "Outputs the sum of the lengths of all sequences in " { $snippet "seq" } "." } ;
649
650 HELP: concat
651 { $values { "seq" sequence } { "newseq" sequence } }
652 { $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" } "." }
653 { $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" } "." } ;
654
655 HELP: join
656 { $values { "seq" sequence } { "glue" sequence } { "newseq" sequence } }
657 { $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" } "." }
658 { $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" } "." } ;
659
660 { join concat } related-words
661
662 HELP: peek
663 { $values { "seq" sequence } { "elt" object } }
664 { $description "Outputs the last element of a sequence." }
665 { $errors "Throws an error if the sequence is empty." } ;
666
667 { peek pop pop* } related-words
668
669 HELP: pop*
670 { $values { "seq" "a resizable mutable sequence" } }
671 { $description "Removes the last element and shortens the sequence." }
672 { $side-effects "seq" }
673 { $errors "Throws an error if the sequence is empty." } ;
674
675 HELP: pop
676 { $values { "seq" "a resizable mutable sequence" } { "elt" object } }
677 { $description "Outputs the last element after removing it and shortening the sequence." }
678 { $side-effects "seq" }
679 { $errors "Throws an error if the sequence is empty." } ;
680
681 HELP: all-equal?
682 { $values { "seq" sequence } { "?" "a boolean" } }
683 { $description "Tests if all elements in the sequence are equal. Yields true with an empty sequence." } ;
684
685 HELP: all-eq?
686 { $values { "seq" sequence } { "?" "a boolean" } }
687 { $description "Tests if all elements in the sequence are the same identical object. Yields true with an empty sequence." } ;
688
689 HELP: mismatch
690 { $values { "seq1" sequence } { "seq2" sequence } { "i" "an index" } }
691 { $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." } ;
692
693 HELP: flip
694 { $values { "matrix" "a sequence of equal-length sequences" } { "newmatrix" "a sequence of equal-length sequences" } }
695 { $description "Transposes the matrix; that is, rows become columns and columns become rows." }
696 { $examples { $example "USING: prettyprint sequences ;" "{ { 1 2 3 } { 4 5 6 } } flip ." "{ { 1 4 } { 2 5 } { 3 6 } }" } } ;
697
698 HELP: exchange
699 { $values { "m" "a non-negative integer" } { "n" "a non-negative integer" } { "seq" "a mutable sequence" } }
700 { $description "Exchanges the " { $snippet "m" } "th and " { $snippet "n" } "th elements of " { $snippet "seq" } "." } ;
701
702 HELP: reverse-here
703 { $values { "seq" "a mutable sequence" } }
704 { $description "Reverses a sequence in-place." }
705 { $side-effects "seq" } ;
706
707 HELP: padding
708 { $values { "seq" sequence } { "n" "a non-negative integer" } { "elt" object } { "quot" "a quotation with stack effect " { $snippet "( seq1 seq2 -- newseq )" } } { "newseq" "a new sequence" } }
709 { $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." } ;
710
711 HELP: pad-left
712 { $values { "seq" sequence } { "n" "a non-negative integer" } { "elt" object } { "padded" "a new sequence" } }
713 { $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" } "." }
714 { $examples { $example "USING: io sequences ;" "{ \"ab\" \"quux\" } [ 5 CHAR: - pad-left print ] each" "---ab\n-quux" } } ;
715
716 HELP: pad-right
717 { $values { "seq" sequence } { "n" "a non-negative integer" } { "elt" object } { "padded" "a new sequence" } }
718 { $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" } "." }
719 { $examples { $example "USING: io sequences ;" "{ \"ab\" \"quux\" } [ 5 CHAR: - pad-right print ] each" "ab---\nquux-" } } ;
720
721 HELP: sequence=
722 { $values { "seq1" sequence } { "seq2" sequence } { "?" "a boolean" } }
723 { $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." } ;
724
725 HELP: reversed
726 { $class-description "A virtual sequence which presents a reversed view of an underlying sequence. New instances can be created by calling " { $link <reversed> } "." } ;
727
728 HELP: reverse
729 { $values { "seq" sequence } { "newseq" "a new sequence" } }
730 { $description "Outputs a new sequence having the same elements as " { $snippet "seq" } " but in reverse order." } ;
731
732 { reverse <reversed> } related-words
733
734 HELP: <reversed> ( seq -- reversed )
735 { $values { "seq" sequence } { "reversed" "a new sequence" } }
736 { $description "Creates an instance of the " { $link reversed } " virtual sequence." } ;
737
738 HELP: slice-error
739 { $values { "str" "a reason" } }
740 { $description "Throws a " { $link slice-error } "." }
741 { $error-description "Thrown by " { $link <slice> } " if one of the following invalid conditions holds:"
742     { $list
743         "The start index is negative"
744         "The end index is greater than the length of the sequence"
745         "The start index is greater than the end index"
746     }
747 } ;
748
749 HELP: slice
750 { $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> } ". Slices are mutable if the underlying sequence is mutable, and mutating a slice changes the underlying sequence." }
751 { $notes "The slots of a slice should not be changed after the slice has been created, because this can break invariants." } ;
752
753 HELP: check-slice
754 { $values { "from" "a non-negative integer" } { "to" "a non-negative integer" } { "seq" sequence } }
755 { $description "Ensures that " { $snippet "m" } " is less than or equal to " { $snippet "m" } ", and that both indices are within bounds for " { $snippet "seq" } "." }
756 { $errors "Throws a " { $link slice-error } " if the preconditions are not met." } ;
757
758 HELP: collapse-slice
759 { $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 } }
760 { $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." }
761 ;
762
763 HELP: <flat-slice>
764 { $values { "seq" sequence } { "slice" slice } }
765 { $description "Outputs a slice with the same elements as " { $snippet "seq" } ", and " { $link slice-from } " equal to 0 and " { $link slice-to } " equal to the length of " { $snippet "seq" } "." }
766 { $notes "Some words create slices then proceed to read and write the " { $link slice-from } " and " { $link slice-to } " 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." } ;
767
768 HELP: <slice>
769 { $values { "from" "a non-negative integer" } { "to" "a non-negative integer" } { "seq" sequence } { "slice" "a slice" } }
770 { $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" } "." }
771 { $errors "Throws an error if " { $snippet "m" } " or " { $snippet "n" } " is out of bounds." }
772 { $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 " { $link slice-from } " and " { $link slice-to } " being equal to the inputs to this word. The " { $link <flat-slice> } " word might be helpful in such situations." } ;
773
774 { <slice> subseq } related-words
775
776 HELP: repetition
777 { $class-description "A virtual sequence consisting of " { $link repetition-elt } " repeated " { $link repetition-len } " times. Repetitions are created by calling " { $link <repetition> } "." } ;
778
779 HELP: <repetition> ( len elt -- repetition )
780 { $values { "len" "a non-negative integer" } { "elt" object } { "repetition" repetition } }
781 { $description "Creates a new " { $link repetition } "." }
782 { $examples
783     { $example "USING: arrays prettyprint sequences ;" "10 \"X\" <repetition> >array ." "{ \"X\" \"X\" \"X\" \"X\" \"X\" \"X\" \"X\" \"X\" \"X\" \"X\" }" }
784     { $example "USING: prettyprint sequences ;" "10 \"X\" <repetition> concat ." "\"XXXXXXXXXX\"" }
785 } ;
786 HELP: copy
787 { $values { "src" sequence } { "i" "an index in " { $snippet "dest" } } { "dst" "a mutable sequence" } }
788 { $description "Copies all elements of " { $snippet "src" } " to " { $snippet "dest" } ", with destination indices starting from " { $snippet "i" } ". Grows " { $snippet "to" } " first if necessary." }
789 { $side-effects "dest" }
790 { $errors "An error is thrown if " { $snippet "to" } " is not resizable, and not large enough to hold the copied elements." } ;
791
792 HELP: push-all
793 { $values { "src" sequence } { "dest" "a resizable mutable sequence" } }
794 { $description "Appends " { $snippet "src" } " to the end of " { $snippet "dest" } "." }
795 { $side-effects "dest" }
796 { $errors "Throws an error if " { $snippet "src" } " contains elements not permitted in " { $snippet "dest" } "." } ;
797
798 HELP: append
799 { $values { "seq1" sequence } { "seq2" sequence } { "newseq" sequence } }
800 { $description "Outputs a new sequence of the same type as " { $snippet "seq1" } " consisting of the elements of " { $snippet "seq1" } " followed by " { $snippet "seq2" } "." }
801 { $errors "Throws an error if " { $snippet "seq2" } " contains elements not permitted in sequences of the same class as " { $snippet "seq1" } "." } ;
802
803 HELP: 3append
804 { $values { "seq1" sequence } { "seq2" sequence } { "seq3" sequence } { "newseq" sequence } }
805 { $description "Outputs a new sequence consisting of the elements of " { $snippet "seq1" } ", " { $snippet "seq2" } " and " { $snippet "seq3" } " in turn." }
806 { $errors "Throws an error if " { $snippet "seq2" } " or " { $snippet "seq3" } " contain elements not permitted in sequences of the same class as " { $snippet "seq1" } "." } ;
807
808 HELP: subseq
809 { $values { "from" "a non-negative integer" } { "to" "a non-negative integer" } { "seq" sequence } { "subseq" "a new sequence" } }
810 { $description "Outputs a new sequence consisting of all elements starting from and including " { $snippet "from" } ", and up to but not including " { $snippet "to" } "." }
811 { $errors "Throws an error if " { $snippet "from" } " or " { $snippet "to" } " is out of bounds." } ;
812
813 HELP: clone-like
814 { $values { "seq" sequence } { "exemplar" sequence } { "newseq" "a new sequence" } }
815 { $description "Outputs a newly-allocated sequence with the same elements as " { $snippet "seq" } " but of the same type as " { $snippet "exemplar" } "." }
816 { $notes "Unlike " { $link like } ", this word always creates a new sequence which never shares storage with the original." } ;
817
818 HELP: head-slice
819 { $values { "seq" sequence } { "n" "a non-negative integer" } { "slice" "a slice" } }
820 { $description "Outputs a virtual sequence sharing storage with the first " { $snippet "n" } " elements of the input sequence." }
821 { $errors "Throws an error if the index is out of bounds." } ;
822
823 HELP: tail-slice
824 { $values { "seq" sequence } { "n" "a non-negative integer" } { "slice" "a slice" } }
825 { $description "Outputs a virtual sequence sharing storage with all elements from the " { $snippet "n" } "th index until the end of the input sequence." }
826 { $errors "Throws an error if the index is out of bounds." } ;
827
828 HELP: but-last-slice
829 { $values { "seq" sequence } { "slice" "a slice" } }
830 { $description "Outputs a virtual sequence sharing storage with all but the last element of the input sequence." }
831 { $errors "Throws an error on an empty sequence." } ;
832
833 HELP: rest-slice
834 { $values { "seq" sequence } { "slice" "a slice" } }
835 { $description "Outputs a virtual sequence sharing storage with all elements from the 1st index until the end of the input sequence." }
836 { $notes "Equivalent to " { $snippet "1 tail" } }
837 { $errors "Throws an error on an empty sequence." } ;
838
839 HELP: head-slice*
840 { $values { "seq" sequence } { "n" "a non-negative integer" } { "slice" "a slice" } }
841 { $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." }
842 { $errors "Throws an error if the index is out of bounds." } ;
843
844 HELP: tail-slice*
845 { $values { "seq" sequence } { "n" "a non-negative integer" } { "slice" "a slice" } }
846 { $description "Outputs a virtual sequence sharing storage with the last " { $snippet "n" } " elements of the input sequence." }
847 { $errors "Throws an error if the index is out of bounds." } ;
848
849 HELP: head
850 { $values { "seq" sequence } { "n" "a non-negative integer" } { "headseq" "a new sequence" } }
851 { $description "Outputs a new sequence consisting of the first " { $snippet "n" } " elements of the input sequence." }
852 { $errors "Throws an error if the index is out of bounds." } ;
853
854 HELP: tail
855 { $values { "seq" sequence } { "n" "a non-negative integer" } { "tailseq" "a new sequence" } }
856 { $description "Outputs a new sequence consisting of the input sequence with the first n items removed." }
857 { $errors "Throws an error if the index is out of bounds." } ;
858
859 HELP: but-last
860 { $values { "seq" sequence } { "headseq" "a new sequence" } }
861 { $description "Outputs a new sequence consisting of the input sequence with the last item removed." }
862 { $errors "Throws an error on an empty sequence." } ;
863
864 HELP: rest
865 { $values { "seq" sequence } { "tailseq" "a new sequence" } }
866 { $description "Outputs a new sequence consisting of the input sequence with the first item removed." }
867 { $errors "Throws an error on an empty sequence." } ;
868
869 HELP: head*
870 { $values { "seq" sequence } { "n" "a non-negative integer" } { "headseq" "a new sequence" } }
871 { $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." }
872 { $errors "Throws an error if the index is out of bounds." } ;
873
874 HELP: tail*
875 { $values { "seq" sequence } { "n" "a non-negative integer" } { "tailseq" "a new sequence" } }
876 { $description "Outputs a new sequence consisting of the last " { $snippet "n" } " elements of the input sequence." }
877 { $errors "Throws an error if the index is out of bounds." } ;
878
879 HELP: shorter?
880 { $values { "seq1" sequence } { "seq2" sequence } { "?" "a boolean" } }
881 { $description "Tets if the length of " { $snippet "seq1" } " is smaller than the length of " { $snippet "seq2" } "." } ;
882
883 HELP: head?
884 { $values { "seq" sequence } { "begin" sequence } { "?" "a boolean" } }
885 { $description "Tests if " { $snippet "seq" } " starts with " { $snippet "begin" } ". If " { $snippet "begin" } " is longer than " { $snippet "seq" } ", this word outputs " { $link f } "." } ;
886
887 HELP: tail?
888 { $values { "seq" sequence } { "end" sequence } { "?" "a boolean" } }
889 { $description "Tests if " { $snippet "seq" } " ends with " { $snippet "end" } ". If " { $snippet "end" } " is longer than " { $snippet "seq" } ", this word outputs " { $link f } "." } ;
890
891 { delete-nth remove delete } related-words
892
893 HELP: cut-slice
894 { $values { "seq" sequence } { "n" "a non-negative integer" } { "before" sequence } { "after" "a slice" } }
895 { $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." }
896 { $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." } ;
897
898 HELP: cut
899 { $values { "seq" sequence } { "n" "a non-negative integer" } { "before" sequence } { "after" sequence } }
900 { $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" } "." }
901 { $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." } ;
902
903 HELP: cut*
904 { $values { "seq" sequence } { "n" "a non-negative integer" } { "before" sequence } { "after" sequence } }
905 { $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" } "." } ;
906
907 HELP: start*
908 { $values { "subseq" sequence } { "seq" sequence } { "n" "a start index" } { "i" "a start index" } }
909 { $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 } "." } ;
910
911 HELP: start
912 { $values { "subseq" sequence } { "seq" sequence } { "i" "a start index" } }
913 { $description "Outputs the start index of the first contiguous subsequence equal to " { $snippet "subseq" } ", or " { $link f } " if no matching subsequence is found." } ;
914
915 HELP: subseq?
916 { $values { "subseq" sequence } { "seq" sequence } { "?" "a boolean" } }
917 { $description "Tests if " { $snippet "seq" } " contains the elements of " { $snippet "subseq" } " as a contiguous subsequence." } ;
918
919 HELP: drop-prefix
920 { $values { "seq1" sequence } { "seq2" sequence } { "slice1" "a slice" } { "slice2" "a slice" } }
921 { $description "Outputs a pair of virtual sequences with the common prefix of " { $snippet "seq1" } " and " { $snippet "seq2" } " removed." } ;
922
923 HELP: unclip
924 { $values { "seq" sequence } { "rest" sequence } { "first" object } }
925 { $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." }
926 { $examples
927     { $example "USING: prettyprint sequences ;" "{ 1 2 3 } unclip suffix ." "{ 2 3 1 }" }
928 } ;
929
930 HELP: unclip-slice
931 { $values { "seq" sequence } { "rest" slice } { "first" object } }
932 { $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." } ;
933
934 HELP: sum
935 { $values { "seq" "a sequence of numbers" } { "n" "a number" } }
936 { $description "Outputs the sum of all elements of " { $snippet "seq" } ". Outputs zero given an empty sequence." } ;
937
938 HELP: product
939 { $values { "seq" "a sequence of numbers" } { "n" "a number" } }
940 { $description "Outputs the product of all elements of " { $snippet "seq" } ". Outputs one given an empty sequence." } ;
941
942 HELP: infimum
943 { $values { "seq" "a sequence of real numbers" } { "n" "a number" } }
944 { $description "Outputs the least element of " { $snippet "seq" } "." }
945 { $errors "Throws an error if the sequence is empty." } ;
946
947 HELP: supremum
948 { $values { "seq" "a sequence of real numbers" } { "n" "a number" } }
949 { $description "Outputs the greatest element of " { $snippet "seq" } "." }
950 { $errors "Throws an error if the sequence is empty." } ;
951
952 HELP: unfold
953 { $values { "pred" "a quotation with stack effect " { $snippet "( -- ? )" } } { "quot" "a quotation with stack effect " { $snippet "( -- obj )" } } { "tail" "a quotation" } { "seq" "a sequence" } }
954 { $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." }
955 { $examples
956     "The following example divides a number by two until we reach zero, and accumulates intermediate results:"
957     { $example "USING: kernel math prettyprint sequences ;" "1337 [ dup 0 > ] [ 2/ dup ] [ ] unfold nip ." "{ 668 334 167 83 41 20 10 5 2 1 0 }" }
958     "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 unfold } " call:"
959     { $unchecked-example "USING: kernel prettyprint random sequences ;" "[ 10 random dup 1 > ] [ ] [ drop ] unfold ." "{ 8 2 2 9 }" }
960 } ;
961
962 HELP: sigma
963 { $values { "seq" sequence } { "quot" quotation } { "n" number } }
964 { $description "Like map sum, but without creating an intermediate sequence." }
965 { $example
966     "! Find the sum of the squares [0,99]"
967     "USING: math math.ranges sequences.lib prettyprint ;"
968     "100 [1,b] [ sq ] sigma ."
969     "338350"
970 } ;
971
972 HELP: count
973 { $values { "seq" sequence } { "quot" quotation } { "n" integer } }
974 { $description "Efficiently returns the number of elements that the predicate quotation matches." }
975 { $example
976     "USING: math math.ranges sequences.lib prettyprint ;"
977     "100 [1,b] [ even? ] count ."
978     "50"
979 } ;
980