]> gitweb.factorcode.org Git - factor.git/blob - core/sequences/sequences-docs.factor
Fixing basis -> extra dependencies
[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: if-empty
319 { $values { "seq" sequence } { "quot1" quotation } { "quot2" quotation } }
320 { $description "Makes an implicit check if the sequence is empty. An empty sequence is dropped and " { $snippet "quot1" } " is called. Otherwise, if the sequence has any elements, " { $snippet "quot2" } " is called on it." }
321 { $example
322     "USING: kernel prettyprint sequences sequences.lib ;"
323     "{ 1 2 3 } [ \"empty sequence\" ] [ sum ] if-empty ."
324     "6"
325 } ;
326
327 HELP: delete-all
328 { $values { "seq" "a resizable sequence" } }
329 { $description "Resizes the sequence to zero length, removing all elements. Not all sequences are resizable." }
330 { $errors "Throws a " { $link bounds-error } " if the new length is negative, or if the sequence is not resizable." }
331 { $side-effects "seq" } ;
332
333 HELP: resize
334 { $values { "n" "a non-negative integer" } { "seq" sequence } { "newseq" "a new sequence" } }
335 { $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." }
336 { $notes "This generic word is only implemented for strings and arrays." } ;
337
338 HELP: first
339 { $values { "seq" sequence } { "first" "the first element of the sequence" } }
340 { $description "Outputs the first element of the sequence." }
341 { $errors "Throws an error if the sequence is empty." } ;
342
343 HELP: second
344 { $values { "seq" sequence } { "second" "the second element of the sequence" } }
345 { $description "Outputs the second element of the sequence." }
346 { $errors "Throws an error if the sequence contains less than two elements." } ;
347
348 HELP: third
349 { $values { "seq" sequence } { "third" "the third element of the sequence" } }
350 { $description "Outputs the third element of the sequence." }
351 { $errors "Throws an error if the sequence contains less than three elements." } ;
352
353 HELP: fourth
354 { $values { "seq" sequence } { "fourth" "the fourth element of the sequence" } }
355 { $description "Outputs the fourth element of the sequence." }
356 { $errors "Throws an error if the sequence contains less than four elements." } ;
357
358 HELP: push
359 { $values { "elt" object } { "seq" "a resizable mutable sequence" } }
360 { $description "Adds an element at the end of the sequence. The sequence length is adjusted accordingly." }
361 { $errors "Throws an error if " { $snippet "seq" } " is not resizable, or if the type of " { $snippet "elt" } " is not permitted in " { $snippet "seq" } "." }
362 { $side-effects "seq" } ;
363
364 HELP: bounds-check?
365 { $values { "n" "an integer" } { "seq" sequence } { "?" "a boolean" } }
366 { $description "Tests if the index is within the bounds of the sequence." } ;
367
368 HELP: bounds-error
369 { $values { "n" "a positive integer" } { "seq" sequence } }
370 { $description "Throws a " { $link bounds-error } "." }
371 { $error-description "Thrown by " { $link nth } ", " { $link set-nth } " and " { $link set-length } " if the given index lies beyond the bounds of the sequence." } ;
372
373 HELP: bounds-check
374 { $values { "n" "a positive integer" } { "seq" sequence } }
375 { $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." } ;
376
377 HELP: ?nth
378 { $values { "n" "an integer" } { "seq" sequence } { "elt/f" "an object or " { $link f } } }
379 { $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 } "." } ;
380
381 HELP: nth-unsafe
382 { $values { "n" "an integer" } { "seq" sequence } { "elt" object } }
383 { $contract "Unsafe variant of " { $link nth } " that does not perform bounds checks." } ;
384
385 HELP: set-nth-unsafe
386 { $values { "elt" object } { "n" "an integer" } { "seq" sequence } }
387 { $contract "Unsafe variant of " { $link set-nth } " that does not perform bounds checks." } ;
388
389 HELP: exchange-unsafe
390 { $values { "m" "a non-negative integer" } { "n" "a non-negative integer" } { "seq" "a mutable sequence" } }
391 { $description "Unsafe variant of " { $link exchange } " that does not perform bounds checks." } ;
392
393 HELP: first2-unsafe
394 { $values { "seq" sequence } { "first" "the first element" } { "second" "the second element" } }
395 { $contract "Unsafe variant of " { $link first2 } " that does not perform bounds checks." } ;
396
397 HELP: first3-unsafe
398 { $values { "seq" sequence } { "first" "the first element" } { "second" "the second element" } { "third" "the third element" } }
399 { $contract "Unsafe variant of " { $link first3 } " that does not perform bounds checks." } ;
400
401 HELP: first4-unsafe
402 { $values { "seq" sequence } { "first" "the first element" } { "second" "the second element" } { "third" "the third element" } { "fourth" "the fourth element" } }
403 { $contract "Unsafe variant of " { $link first4 } " that does not perform bounds checks." } ;
404
405 HELP: 2sequence
406 { $values { "obj1" object } { "obj2" object } { "exemplar" sequence } { "seq" sequence } }
407 { $description "Creates a two-element sequence of the same type as " { $snippet "exemplar" } "." } ;
408
409 HELP: 3sequence
410 { $values { "obj1" object } { "obj2" object } { "obj3" object } { "exemplar" sequence } { "seq" sequence } }
411 { $description "Creates a three-element sequence of the same type as " { $snippet "exemplar" } "." } ;
412
413 HELP: 4sequence
414 { $values { "obj1" object } { "obj2" object } { "exemplar" sequence } { "obj3" object } { "obj4" object } { "seq" sequence } }
415 { $description "Creates a four-element sequence of the same type as " { $snippet "exemplar" } "." } ;
416
417 HELP: first2
418 { $values { "seq" sequence } { "first" "the first element" } { "second" "the second element" } }
419 { $description "Pushes the first two elements of a sequence." }
420 { $errors "Throws an error if the sequence has less than two elements." } ;
421
422 HELP: first3
423 { $values { "seq" sequence } { "first" "the first element" } { "second" "the second element" } { "third" "the third element" } }
424 { $description "Pushes the first three elements of a sequence." }
425 { $errors "Throws an error if the sequence has less than three elements." } ;
426
427 HELP: first4
428 { $values { "seq" sequence } { "first" "the first element" } { "second" "the second element" } { "third" "the third element" } { "fourth" "the fourth element" } }
429 { $description "Pushes the first four elements of a sequence." }
430 { $errors "Throws an error if the sequence has less than four elements." } ;
431
432 HELP: array-capacity
433 { $values { "array" "an array" } { "n" "a non-negative fixnum" } }
434 { $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 } "." }
435 { $description "Low-level array length accessor." }
436 { $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." } ;
437
438 HELP: array-nth
439 { $values { "n" "a non-negative fixnum" } { "array" "an array" }  { "elt" object } }
440 { $description "Low-level array element accessor." }
441 { $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." } ;
442
443 HELP: set-array-nth
444 { $values { "elt" object } { "n" "a non-negative fixnum" } { "array" "an array" }  }
445 { $description "Low-level array element mutator." }
446 { $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." } ;
447
448 HELP: collect
449 { $values { "n" "a non-negative integer" } { "quot" "a quotation with stack effect " { $snippet "( n -- value )" } } { "into" "a sequence of length at least " { $snippet "n" } } }
450 { $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." } ;
451
452 HELP: each
453 { $values { "seq" sequence } { "quot" "a quotation with stack effect " { $snippet "( elt -- )" } } }
454 { $description "Applies the quotation to each element of the sequence in order." } ;
455
456 HELP: reduce
457 { $values { "seq" sequence } { "identity" object } { "quot" "a quotation with stack effect " { $snippet "( prev elt -- next )" } } { "result" "the final result" } }
458 { $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." }
459 { $examples
460     { $example "USING: math prettyprint sequences ;" "{ 1 5 3 } 0 [ + ] reduce ." "9" }
461 } ;
462
463 HELP: accumulate
464 { $values { "identity" object } { "seq" sequence } { "quot" "a quotation with stack effect " { $snippet "( prev elt -- next )" } } { "final" "the final result" } { "newseq" "a new sequence" } }
465 { $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."
466 $nl
467 "When given the empty sequence, outputs an empty sequence together with the " { $snippet "identity" } "." }
468 { $examples
469     { $example "USING: math prettyprint sequences ;" "{ 2 2 2 2 2 } 0 [ + ] accumulate . ." "{ 0 2 4 6 8 }\n10" }
470 } ;
471
472 HELP: map
473 { $values { "seq" sequence } { "quot" "a quotation with stack effect " { $snippet "( old -- new )" } } { "newseq" "a new sequence" } }
474 { $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." } ;
475
476 HELP: map-as
477 { $values { "seq" sequence } { "quot" "a quotation with stack effect " { $snippet "( old -- new )" } } { "newseq" "a new sequence" } { "exemplar" sequence } }
478 { $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" } "." }
479 { $examples
480     "The following example converts a string into an array of one-element strings:"
481     { $example "USING: prettyprint strings sequences ;" "\"Hello\" [ 1string ] { } map-as ." "{ \"H\" \"e\" \"l\" \"l\" \"o\" }" }
482     "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."
483 } ;
484
485 HELP: change-nth
486 { $values { "i" "a non-negative integer" } { "seq" "a mutable sequence" } { "quot" "a quotation with stack effect " { $snippet "( elt -- newelt )" } } }
487 { $description "Applies the quotation to the " { $snippet "i" } "th element of the sequence, storing the result back into the sequence." }
488 { $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" } "." }
489 { $side-effects "seq" } ;
490
491 HELP: change-each
492 { $values { "seq" "a mutable sequence" } { "quot" "a quotation with stack effect " { $snippet "( old -- new )" } } }
493 { $description "Applies the quotation to each element yielding a new element, storing the new elements back in the original sequence." }
494 { $errors "Throws an error if the sequence is immutable, or the sequence cannot hold elements of the type output by " { $snippet "quot" } "." }
495 { $side-effects "seq" } ;
496
497 HELP: min-length
498 { $values { "seq1" sequence } { "seq2" sequence } { "n" "a non-negative integer" } }
499 { $description "Outputs the minimum of the lengths of the two sequences." } ;
500
501 HELP: max-length
502 { $values { "seq1" sequence } { "seq2" sequence } { "n" "a non-negative integer" } }
503 { $description "Outputs the maximum of the lengths of the two sequences." } ;
504
505 HELP: 2each
506 { $values { "seq1" sequence } { "seq2" sequence } { "quot" "a quotation with stack effect " { $snippet "( elt1 elt2 -- )" } } }
507 { $description "Applies the quotation to pairs of elements from " { $snippet "seq1" } " and " { $snippet "seq2" } "." } ;
508
509 HELP: 2reduce
510 { $values { "seq1" sequence }
511           { "seq2" sequence }
512           { "identity" object }
513           { "quot" "a quotation with stack effect "
514                    { $snippet "( prev elt1 elt2 -- next )" } }
515           { "result" "the final result" } }
516 { $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" } "." } ;
517
518 HELP: 2map
519 { $values { "seq1" sequence } { "seq2" sequence } { "quot" "a quotation with stack effect " { $snippet "( elt1 elt2 -- new )" } } { "newseq" "a new sequence" } }
520 { $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" } "." } ;
521
522 HELP: 2map-as
523 { $values { "seq1" sequence } { "seq2" sequence } { "quot" "a quotation with stack effect " { $snippet "( elt1 elt2 -- new )" } } { "exemplar" sequence } { "newseq" "a new sequence" } }
524 { $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" } "." } ;
525
526 HELP: 2all?
527 { $values { "seq1" sequence } { "seq2" sequence } { "quot" "a quotation with stack effect " { $snippet "( elt1 elt2 -- ? )" } } { "?" "a boolean" } }
528 { $description "Tests the predicate pairwise against elements of " { $snippet "seq1" } " and " { $snippet "seq2" } "." } ;
529
530 HELP: find
531 { $values { "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 "A simpler variant of " { $link find-from } " where the starting index is 0." } ;
537
538 HELP: find-from
539 { $values { "n" "a starting index" }
540           { "seq" sequence }
541           { "quot" "a quotation with stack effect "
542                    { $snippet "( elt -- ? )" } }
543           { "i" "the index of the first match, or f" }
544           { "elt" "the first matching element, or " { $link f } } }
545 { $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." } ;
546
547 HELP: find-last
548 { $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 } } }
549 { $description "A simpler variant of " { $link find-last-from } " where the starting index is one less than the length of the sequence." } ;
550
551 HELP: find-last-from
552 { $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 } } }
553 { $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." } ;
554
555 HELP: contains?
556 { $values { "seq" sequence } { "quot" "a quotation with stack effect " { $snippet "( elt -- ? )" } } { "?" "a boolean" } }
557 { $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 } "." } ;
558
559 HELP: all?
560 { $values { "seq" sequence } { "quot" "a quotation with stack effect " { $snippet "( elt -- ? )" } } { "?" "a boolean" } }
561 { $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 } "." } ;
562
563 HELP: push-if
564 { $values { "elt" object } { "quot" "a quotation with stack effect " { $snippet "( elt -- ? )" } } { "accum" "a resizable mutable sequence" } }
565 { $description "Adds the element at the end of the sequence if the quotation yields a true value." } 
566 { $notes "This word is a factor of " { $link filter } "." } ;
567
568 HELP: filter
569 { $values { "seq" sequence } { "quot" "a quotation with stack effect " { $snippet "( elt -- ? )" } } { "subseq" "a new sequence" } }
570 { $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." } ;
571
572 HELP: monotonic?
573 { $values { "seq" sequence } { "quot" "a quotation with stack effect " { $snippet "( elt elt -- ? )" } } { "?" "a boolean" } }
574 { $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." }
575 { $examples
576     "Testing if a sequence is non-decreasing:"
577     { $example "USING: math prettyprint sequences ;" "{ 1 1 2 } [ <= ] monotonic? ." "t" }
578     "Testing if a sequence is decreasing:"
579     { $example "USING: math prettyprint sequences ;" "{ 9 8 6 7 } [ < ] monotonic? ." "f" }
580 } ;
581
582 { monotonic? all-eq? all-equal? } related-words
583
584 HELP: interleave
585 { $values { "seq" sequence } { "between" "a quotation" } { "quot" "a quotation with stack effect " { $snippet "( elt -- )" } } }
586 { $description "Applies " { $snippet "quot" } " to each element in turn, also invoking " { $snippet "between" } " in-between each pair of elements." }
587 { $example "USING: io sequences ;" "{ \"a\" \"b\" \"c\" } [ \"X\" write ] [ write ] interleave" "aXbXc" } ;
588
589 HELP: cache-nth
590 { $values { "i" "a non-negative integer" } { "seq" "a mutable sequence" } { "quot" "a quotation with stack effect " { $snippet "( i -- elt )" } } { "elt" object } }
591 { $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." }
592 { $side-effects "seq" } ;
593
594 HELP: index
595 { $values { "obj" object } { "seq" sequence } { "n" "an index" } }
596 { $description "Outputs the index of the first element in the sequence equal to " { $snippet "obj" } ". If no element is found, outputs " { $link f } "." } ;
597
598 HELP: index-from
599 { $values { "obj" object } { "i" "a start index" } { "seq" sequence } { "n" "an index" } }
600 { $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 } "." } ;
601
602 HELP: last-index
603 { $values { "obj" object } { "seq" sequence } { "n" "an index" } }
604 { $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 } "." } ;
605
606 HELP: last-index-from
607 { $values { "obj" object } { "i" "a start index" } { "seq" sequence } { "n" "an index" } }
608 { $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 } "." } ;
609
610 HELP: member?
611 { $values { "obj" object } { "seq" sequence } { "?" "a boolean" } }
612 { $description "Tests if the sequence contains an element equal to the object." } ;
613
614 HELP: memq?
615 { $values { "obj" object } { "seq" sequence } { "?" "a boolean" } }
616 { $description "Tests if the sequence contains the object." }
617 { $examples
618     "This word uses identity comparison, so the following will most likely print " { $link f } ":"
619     { $example "USING: prettyprint sequences ;" "\"hello\" { \"hello\" } memq? ." "f" }
620 } ;
621
622 HELP: remove
623 { $values { "obj" object } { "seq" sequence } { "newseq" "a new sequence" } }
624 { $description "Outputs a new sequence containing all elements of the input sequence except those equal to the given element." } ;
625
626 HELP: move
627 { $values { "from" "an index in " { $snippet "seq" } } { "to" "an index in " { $snippet "seq" } } { "seq" "a mutable sequence" } }
628 { $description "Sets the element with index " { $snippet "m" } " to the element with index " { $snippet "n" } "." }
629 { $side-effects "seq" } ;
630
631 HELP: delete
632 { $values { "elt" object } { "seq" "a resizable mutable sequence" } }
633 { $description "Removes all elements equal to " { $snippet "elt" } " from " { $snippet "seq" } "." }
634 { $side-effects "seq" } ;
635
636 HELP: delete-nth
637 { $values { "n" "a non-negative integer" } { "seq" "a resizable mutable sequence" } }
638 { $description "Removes the " { $snippet "n" } "th element from the sequence, shifting all other elements down and reducing its length by one." }
639 { $side-effects "seq" } ;
640
641 HELP: delete-slice
642 { $values { "from" "a non-negative integer" } { "to" "a non-negative integer" } { "seq" "a resizable mutable sequence" } }
643 { $description "Removes a range of elements beginning at index " { $snippet "from" } " and ending before index " { $snippet "to" } "." }
644 { $side-effects "seq" } ;
645
646 HELP: replace-slice
647 { $values { "new" sequence } { "seq" "a mutable sequence" } { "from" "a non-negative integer" } { "to" "a non-negative integer" } }
648 { $description "Replaces a range of elements beginning at index " { $snippet "from" } " and ending before index " { $snippet "to" } " with a new sequence." }
649 { $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." }
650 { $errors "Throws an error if " { $snippet "new" } " contains elements whose types are not permissible in " { $snippet "seq" } "." }
651 { $side-effects "seq" } ;
652
653 { push prefix suffix } related-words
654
655 HELP: suffix
656 { $values { "seq" sequence } { "elt" object } { "newseq" sequence } }
657 { $description "Outputs a new sequence obtained by adding " { $snippet "elt" } " at the end of " { $snippet "seq" } "." }
658 { $errors "Throws an error if the type of " { $snippet "elt" } " is not permitted in sequences of the same class as " { $snippet "seq1" } "." }
659 { $examples
660     { $example "USING: prettyprint sequences ;" "{ 1 2 3 } 4 suffix ." "{ 1 2 3 4 }" }
661 } ;
662
663 HELP: prefix
664 { $values { "seq" sequence } { "elt" object } { "newseq" sequence } }
665 { $description "Outputs a new sequence obtained by adding " { $snippet "elt" } " at the beginning of " { $snippet "seq" } "." }
666 { $errors "Throws an error if the type of " { $snippet "elt" } " is not permitted in sequences of the same class as " { $snippet "seq1" } "." } 
667 { $examples
668 { $example "USING: prettyprint sequences ;" "{ 1 2 3 } 0 prefix ." "{ 0 1 2 3 }" }
669 } ;
670
671 HELP: sum-lengths
672 { $values { "seq" "a sequence of sequences" } { "n" integer } }
673 { $description "Outputs the sum of the lengths of all sequences in " { $snippet "seq" } "." } ;
674
675 HELP: concat
676 { $values { "seq" sequence } { "newseq" sequence } }
677 { $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" } "." }
678 { $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" } "." } ;
679
680 HELP: join
681 { $values { "seq" sequence } { "glue" sequence } { "newseq" sequence } }
682 { $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" } "." }
683 { $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" } "." } ;
684
685 { join concat } related-words
686
687 HELP: peek
688 { $values { "seq" sequence } { "elt" object } }
689 { $description "Outputs the last element of a sequence." }
690 { $errors "Throws an error if the sequence is empty." } ;
691
692 { peek pop pop* } related-words
693
694 HELP: pop*
695 { $values { "seq" "a resizable mutable sequence" } }
696 { $description "Removes the last element and shortens the sequence." }
697 { $side-effects "seq" }
698 { $errors "Throws an error if the sequence is empty." } ;
699
700 HELP: pop
701 { $values { "seq" "a resizable mutable sequence" } { "elt" object } }
702 { $description "Outputs the last element after removing it and shortening the sequence." }
703 { $side-effects "seq" }
704 { $errors "Throws an error if the sequence is empty." } ;
705
706 HELP: all-equal?
707 { $values { "seq" sequence } { "?" "a boolean" } }
708 { $description "Tests if all elements in the sequence are equal. Yields true with an empty sequence." } ;
709
710 HELP: all-eq?
711 { $values { "seq" sequence } { "?" "a boolean" } }
712 { $description "Tests if all elements in the sequence are the same identical object. Yields true with an empty sequence." } ;
713
714 HELP: mismatch
715 { $values { "seq1" sequence } { "seq2" sequence } { "i" "an index" } }
716 { $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." } ;
717
718 HELP: flip
719 { $values { "matrix" "a sequence of equal-length sequences" } { "newmatrix" "a sequence of equal-length sequences" } }
720 { $description "Transposes the matrix; that is, rows become columns and columns become rows." }
721 { $examples { $example "USING: prettyprint sequences ;" "{ { 1 2 3 } { 4 5 6 } } flip ." "{ { 1 4 } { 2 5 } { 3 6 } }" } } ;
722
723 HELP: exchange
724 { $values { "m" "a non-negative integer" } { "n" "a non-negative integer" } { "seq" "a mutable sequence" } }
725 { $description "Exchanges the " { $snippet "m" } "th and " { $snippet "n" } "th elements of " { $snippet "seq" } "." } ;
726
727 HELP: reverse-here
728 { $values { "seq" "a mutable sequence" } }
729 { $description "Reverses a sequence in-place." }
730 { $side-effects "seq" } ;
731
732 HELP: padding
733 { $values { "seq" sequence } { "n" "a non-negative integer" } { "elt" object } { "quot" "a quotation with stack effect " { $snippet "( seq1 seq2 -- newseq )" } } { "newseq" "a new sequence" } }
734 { $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." } ;
735
736 HELP: pad-left
737 { $values { "seq" sequence } { "n" "a non-negative integer" } { "elt" object } { "padded" "a new sequence" } }
738 { $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" } "." }
739 { $examples { $example "USING: io sequences ;" "{ \"ab\" \"quux\" } [ 5 CHAR: - pad-left print ] each" "---ab\n-quux" } } ;
740
741 HELP: pad-right
742 { $values { "seq" sequence } { "n" "a non-negative integer" } { "elt" object } { "padded" "a new sequence" } }
743 { $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" } "." }
744 { $examples { $example "USING: io sequences ;" "{ \"ab\" \"quux\" } [ 5 CHAR: - pad-right print ] each" "ab---\nquux-" } } ;
745
746 HELP: sequence=
747 { $values { "seq1" sequence } { "seq2" sequence } { "?" "a boolean" } }
748 { $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." } ;
749
750 HELP: reversed
751 { $class-description "A virtual sequence which presents a reversed view of an underlying sequence. New instances can be created by calling " { $link <reversed> } "." } ;
752
753 HELP: reverse
754 { $values { "seq" sequence } { "newseq" "a new sequence" } }
755 { $description "Outputs a new sequence having the same elements as " { $snippet "seq" } " but in reverse order." } ;
756
757 { reverse <reversed> } related-words
758
759 HELP: <reversed> ( seq -- reversed )
760 { $values { "seq" sequence } { "reversed" "a new sequence" } }
761 { $description "Creates an instance of the " { $link reversed } " virtual sequence." } ;
762
763 HELP: slice-error
764 { $values { "str" "a reason" } }
765 { $description "Throws a " { $link slice-error } "." }
766 { $error-description "Thrown by " { $link <slice> } " if one of the following invalid conditions holds:"
767     { $list
768         "The start index is negative"
769         "The end index is greater than the length of the sequence"
770         "The start index is greater than the end index"
771     }
772 } ;
773
774 HELP: slice
775 { $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> } "."
776 $nl
777 "Slices are mutable if the underlying sequence is mutable, and mutating a slice changes the underlying sequence. However, slices cannot be resized after creation." } ;
778
779 HELP: check-slice
780 { $values { "from" "a non-negative integer" } { "to" "a non-negative integer" } { "seq" sequence } }
781 { $description "Ensures that " { $snippet "m" } " is less than or equal to " { $snippet "m" } ", and that both indices are within bounds for " { $snippet "seq" } "." }
782 { $errors "Throws a " { $link slice-error } " if the preconditions are not met." } ;
783
784 HELP: collapse-slice
785 { $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 } }
786 { $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." }
787 ;
788
789 HELP: <flat-slice>
790 { $values { "seq" sequence } { "slice" slice } }
791 { $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" } "." }
792 { $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." } ;
793
794 HELP: <slice>
795 { $values { "from" "a non-negative integer" } { "to" "a non-negative integer" } { "seq" sequence } { "slice" slice } }
796 { $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" } "." }
797 { $errors "Throws an error if " { $snippet "m" } " or " { $snippet "n" } " is out of bounds." }
798 { $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." } ;
799
800 { <slice> subseq } related-words
801
802 HELP: repetition
803 { $class-description "A virtual sequence consisting of " { $snippet "elt" } " repeated " { $snippet "len" } " times. Repetitions are created by calling " { $link <repetition> } "." } ;
804
805 HELP: <repetition> ( len elt -- repetition )
806 { $values { "len" "a non-negative integer" } { "elt" object } { "repetition" repetition } }
807 { $description "Creates a new " { $link repetition } "." }
808 { $examples
809     { $example "USING: arrays prettyprint sequences ;" "10 \"X\" <repetition> >array ." "{ \"X\" \"X\" \"X\" \"X\" \"X\" \"X\" \"X\" \"X\" \"X\" \"X\" }" }
810     { $example "USING: prettyprint sequences ;" "10 \"X\" <repetition> concat ." "\"XXXXXXXXXX\"" }
811 } ;
812 HELP: copy
813 { $values { "src" sequence } { "i" "an index in " { $snippet "dest" } } { "dst" "a mutable sequence" } }
814 { $description "Copies all elements of " { $snippet "src" } " to " { $snippet "dest" } ", with destination indices starting from " { $snippet "i" } ". Grows " { $snippet "to" } " first if necessary." }
815 { $side-effects "dest" }
816 { $errors "An error is thrown if " { $snippet "to" } " is not resizable, and not large enough to hold the copied elements." } ;
817
818 HELP: push-all
819 { $values { "src" sequence } { "dest" "a resizable mutable sequence" } }
820 { $description "Appends " { $snippet "src" } " to the end of " { $snippet "dest" } "." }
821 { $side-effects "dest" }
822 { $errors "Throws an error if " { $snippet "src" } " contains elements not permitted in " { $snippet "dest" } "." } ;
823
824 HELP: append
825 { $values { "seq1" sequence } { "seq2" sequence } { "newseq" sequence } }
826 { $description "Outputs a new sequence of the same type as " { $snippet "seq1" } " consisting of the elements of " { $snippet "seq1" } " followed by " { $snippet "seq2" } "." }
827 { $errors "Throws an error if " { $snippet "seq2" } " contains elements not permitted in sequences of the same class as " { $snippet "seq1" } "." } ;
828
829 HELP: 3append
830 { $values { "seq1" sequence } { "seq2" sequence } { "seq3" sequence } { "newseq" sequence } }
831 { $description "Outputs a new sequence consisting of the elements of " { $snippet "seq1" } ", " { $snippet "seq2" } " and " { $snippet "seq3" } " in turn." }
832 { $errors "Throws an error if " { $snippet "seq2" } " or " { $snippet "seq3" } " contain elements not permitted in sequences of the same class as " { $snippet "seq1" } "." } ;
833
834 HELP: subseq
835 { $values { "from" "a non-negative integer" } { "to" "a non-negative integer" } { "seq" sequence } { "subseq" "a new sequence" } }
836 { $description "Outputs a new sequence consisting of all elements starting from and including " { $snippet "from" } ", and up to but not including " { $snippet "to" } "." }
837 { $errors "Throws an error if " { $snippet "from" } " or " { $snippet "to" } " is out of bounds." } ;
838
839 HELP: clone-like
840 { $values { "seq" sequence } { "exemplar" sequence } { "newseq" "a new sequence" } }
841 { $description "Outputs a newly-allocated sequence with the same elements as " { $snippet "seq" } " but of the same type as " { $snippet "exemplar" } "." }
842 { $notes "Unlike " { $link like } ", this word always creates a new sequence which never shares storage with the original." } ;
843
844 HELP: head-slice
845 { $values { "seq" sequence } { "n" "a non-negative integer" } { "slice" "a slice" } }
846 { $description "Outputs a virtual sequence sharing storage with the first " { $snippet "n" } " elements of the input sequence." }
847 { $errors "Throws an error if the index is out of bounds." } ;
848
849 HELP: tail-slice
850 { $values { "seq" sequence } { "n" "a non-negative integer" } { "slice" "a slice" } }
851 { $description "Outputs a virtual sequence sharing storage with all elements from the " { $snippet "n" } "th index until the end of the input sequence." }
852 { $errors "Throws an error if the index is out of bounds." } ;
853
854 HELP: but-last-slice
855 { $values { "seq" sequence } { "slice" "a slice" } }
856 { $description "Outputs a virtual sequence sharing storage with all but the last element of the input sequence." }
857 { $errors "Throws an error on an empty sequence." } ;
858
859 HELP: rest-slice
860 { $values { "seq" sequence } { "slice" "a slice" } }
861 { $description "Outputs a virtual sequence sharing storage with all elements from the 1st index until the end of the input sequence." }
862 { $notes "Equivalent to " { $snippet "1 tail" } }
863 { $errors "Throws an error on an empty sequence." } ;
864
865 HELP: head-slice*
866 { $values { "seq" sequence } { "n" "a non-negative integer" } { "slice" "a slice" } }
867 { $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." }
868 { $errors "Throws an error if the index is out of bounds." } ;
869
870 HELP: tail-slice*
871 { $values { "seq" sequence } { "n" "a non-negative integer" } { "slice" "a slice" } }
872 { $description "Outputs a virtual sequence sharing storage with the last " { $snippet "n" } " elements of the input sequence." }
873 { $errors "Throws an error if the index is out of bounds." } ;
874
875 HELP: head
876 { $values { "seq" sequence } { "n" "a non-negative integer" } { "headseq" "a new sequence" } }
877 { $description "Outputs a new sequence consisting of the first " { $snippet "n" } " elements of the input sequence." }
878 { $errors "Throws an error if the index is out of bounds." } ;
879
880 HELP: tail
881 { $values { "seq" sequence } { "n" "a non-negative integer" } { "tailseq" "a new sequence" } }
882 { $description "Outputs a new sequence consisting of the input sequence with the first n items removed." }
883 { $errors "Throws an error if the index is out of bounds." } ;
884
885 HELP: but-last
886 { $values { "seq" sequence } { "headseq" "a new sequence" } }
887 { $description "Outputs a new sequence consisting of the input sequence with the last item removed." }
888 { $errors "Throws an error on an empty sequence." } ;
889
890 HELP: rest
891 { $values { "seq" sequence } { "tailseq" "a new sequence" } }
892 { $description "Outputs a new sequence consisting of the input sequence with the first item removed." }
893 { $errors "Throws an error on an empty sequence." } ;
894
895 HELP: head*
896 { $values { "seq" sequence } { "n" "a non-negative integer" } { "headseq" "a new sequence" } }
897 { $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." }
898 { $errors "Throws an error if the index is out of bounds." } ;
899
900 HELP: tail*
901 { $values { "seq" sequence } { "n" "a non-negative integer" } { "tailseq" "a new sequence" } }
902 { $description "Outputs a new sequence consisting of the last " { $snippet "n" } " elements of the input sequence." }
903 { $errors "Throws an error if the index is out of bounds." } ;
904
905 HELP: shorter?
906 { $values { "seq1" sequence } { "seq2" sequence } { "?" "a boolean" } }
907 { $description "Tets if the length of " { $snippet "seq1" } " is smaller than the length of " { $snippet "seq2" } "." } ;
908
909 HELP: head?
910 { $values { "seq" sequence } { "begin" sequence } { "?" "a boolean" } }
911 { $description "Tests if " { $snippet "seq" } " starts with " { $snippet "begin" } ". If " { $snippet "begin" } " is longer than " { $snippet "seq" } ", this word outputs " { $link f } "." } ;
912
913 HELP: tail?
914 { $values { "seq" sequence } { "end" sequence } { "?" "a boolean" } }
915 { $description "Tests if " { $snippet "seq" } " ends with " { $snippet "end" } ". If " { $snippet "end" } " is longer than " { $snippet "seq" } ", this word outputs " { $link f } "." } ;
916
917 { delete-nth remove delete } related-words
918
919 HELP: cut-slice
920 { $values { "seq" sequence } { "n" "a non-negative integer" } { "before" sequence } { "after" "a slice" } }
921 { $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." }
922 { $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." } ;
923
924 HELP: cut
925 { $values { "seq" sequence } { "n" "a non-negative integer" } { "before" sequence } { "after" sequence } }
926 { $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" } "." }
927 { $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." } ;
928
929 HELP: cut*
930 { $values { "seq" sequence } { "n" "a non-negative integer" } { "before" sequence } { "after" sequence } }
931 { $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" } "." } ;
932
933 HELP: start*
934 { $values { "subseq" sequence } { "seq" sequence } { "n" "a start index" } { "i" "a start index" } }
935 { $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 } "." } ;
936
937 HELP: start
938 { $values { "subseq" sequence } { "seq" sequence } { "i" "a start index" } }
939 { $description "Outputs the start index of the first contiguous subsequence equal to " { $snippet "subseq" } ", or " { $link f } " if no matching subsequence is found." } ;
940
941 HELP: subseq?
942 { $values { "subseq" sequence } { "seq" sequence } { "?" "a boolean" } }
943 { $description "Tests if " { $snippet "seq" } " contains the elements of " { $snippet "subseq" } " as a contiguous subsequence." } ;
944
945 HELP: drop-prefix
946 { $values { "seq1" sequence } { "seq2" sequence } { "slice1" "a slice" } { "slice2" "a slice" } }
947 { $description "Outputs a pair of virtual sequences with the common prefix of " { $snippet "seq1" } " and " { $snippet "seq2" } " removed." } ;
948
949 HELP: unclip
950 { $values { "seq" sequence } { "rest" sequence } { "first" object } }
951 { $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." }
952 { $examples
953     { $example "USING: prettyprint sequences ;" "{ 1 2 3 } unclip suffix ." "{ 2 3 1 }" }
954 } ;
955
956 HELP: unclip-slice
957 { $values { "seq" sequence } { "rest" slice } { "first" object } }
958 { $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." } ;
959
960 HELP: sum
961 { $values { "seq" "a sequence of numbers" } { "n" "a number" } }
962 { $description "Outputs the sum of all elements of " { $snippet "seq" } ". Outputs zero given an empty sequence." } ;
963
964 HELP: product
965 { $values { "seq" "a sequence of numbers" } { "n" "a number" } }
966 { $description "Outputs the product of all elements of " { $snippet "seq" } ". Outputs one given an empty sequence." } ;
967
968 HELP: infimum
969 { $values { "seq" "a sequence of real numbers" } { "n" "a number" } }
970 { $description "Outputs the least element of " { $snippet "seq" } "." }
971 { $errors "Throws an error if the sequence is empty." } ;
972
973 HELP: supremum
974 { $values { "seq" "a sequence of real numbers" } { "n" "a number" } }
975 { $description "Outputs the greatest element of " { $snippet "seq" } "." }
976 { $errors "Throws an error if the sequence is empty." } ;
977
978 HELP: produce
979 { $values { "pred" "a quotation with stack effect " { $snippet "( -- ? )" } } { "quot" "a quotation with stack effect " { $snippet "( -- obj )" } } { "tail" "a quotation" } { "seq" "a sequence" } }
980 { $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." }
981 { $examples
982     "The following example divides a number by two until we reach zero, and accumulates intermediate results:"
983     { $example "USING: kernel math prettyprint sequences ;" "1337 [ dup 0 > ] [ 2/ dup ] [ ] produce nip ." "{ 668 334 167 83 41 20 10 5 2 1 0 }" }
984     "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:"
985     { $unchecked-example "USING: kernel prettyprint random sequences ;" "[ 10 random dup 1 > ] [ ] [ drop ] produce ." "{ 8 2 2 9 }" }
986 } ;
987
988 HELP: sigma
989 { $values { "seq" sequence } { "quot" quotation } { "n" number } }
990 { $description "Like map sum, but without creating an intermediate sequence." }
991 { $example
992     "! Find the sum of the squares [0,99]"
993     "USING: math math.ranges sequences prettyprint ;"
994     "100 [1,b] [ sq ] sigma ."
995     "338350"
996 } ;
997
998 HELP: count
999 { $values { "seq" sequence } { "quot" quotation } { "n" integer } }
1000 { $description "Efficiently returns the number of elements that the predicate quotation matches." }
1001 { $example
1002     "USING: math math.ranges sequences prettyprint ;"
1003     "100 [1,b] [ even? ] count ."
1004     "50"
1005 } ;
1006