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