]> gitweb.factorcode.org Git - factor.git/blob - extra/sequences/extras/extras-docs.factor
d52928eea3669956990831c1c093cdbe76ab3723
[factor.git] / extra / sequences / extras / extras-docs.factor
1 USING: help.markup help.syntax kernel math sequences ;
2 IN: sequences.extras
3
4 HELP: 2count
5 { $values
6     { "seq1" sequence }
7     { "seq2" sequence }
8     { "quot" { $quotation ( ... elt1 elt2 -- ... ? ) } }
9     { "n" integer } }
10 { $description "Efficiently counts how many pairwise elements of " { $snippet "seq1" } " and " { $snippet "seq2" } " that the predicate quotation matches." }
11 { $examples
12     { $example "USING: kernel prettyprint sequences.extras ;" "{ 1 2 3 } { 3 2 1 } [ = ] 2count ." "1" } }
13 { $see-also count } ;
14
15 HELP: 2each-index
16 { $values
17     { "seq1" sequence }
18     { "seq2" sequence }
19     { "quot" { $quotation ( ... elt1 elt2 index -- ... ) } } }
20 { $description "Applies " { $snippet "quot" } " to each pair of elements from " { $snippet "seq1" } " and " { $snippet "seq2" } ", providing the index of the elements at the top of the stack." }
21 { $see-also 2each each-index } ;
22
23 HELP: 2map!
24 { $values
25     { "seq1" sequence }
26     { "seq2" sequence }
27     { "quot" { $quotation ( ... elt1 elt2 -- ... newelt ) } } }
28 { $description "Applies the quotation to each pair of elements from " { $snippet "seq1" } " and " { $snippet "seq2" } ", yielding a new element, and storing it back into " { $snippet "seq1" } ".  Returns " { $snippet "seq1" } "." }
29 { $see-also 2map map! } ;
30
31 HELP: 2map-index
32 { $values
33     { "seq1" sequence }
34     { "seq2" sequence }
35     { "quot" { $quotation ( ... elt1 elt2 index -- ... newelt ) } }
36     { "newseq" sequence } }
37 { $description "Calls the quotation with each pair of elements of the two sequences and their index on the stack, with the index on the top of the stack.  Collects the outputs of the quotation and outputs them into a new sequence of the same type as the first sequence." }
38 { $see-also 2map map-index } ;
39
40 HELP: count*
41 { $values
42     { "seq" sequence }
43     { "quot" { $quotation ( ... elt -- ... ? ) } }
44     { "%" rational } }
45 { $description "Outputs the fraction of elements in the sequence for which the predicate quotation matches." }
46 { $examples { $example "USING: math math.ranges prettyprint sequences.extras ;" "100 [1,b] [ even? ] count* ." "1/2" } } ;
47
48 HELP: collapse
49 { $values
50     { "seq" sequence }
51     { "quot" { $quotation ( ... elt -- ... ? ) } }
52     { "elt" object }
53     { "seq'" sequence } }
54 { $description "Generate a new sequence where all runs of elements for which the predicate returns true are replaced by a single instance of " { $snippet "elt" } "." }
55 { $see-also compact }
56 { $examples
57     "Collapse multiple spaces in a string down to a single space"
58     { $example "USING: kernel prettyprint sequences.extras ;" "\"   Hello,    crazy    world   \" [ CHAR: \\s = ] \" \" collapse ." "\" Hello, crazy world \"" } } ;
59
60 HELP: compact
61 { $values
62     { "seq" sequence }
63     { "quot" { $quotation ( ... elt -- ... ? ) } }
64     { "elt" object }
65     { "seq'" sequence } }
66 { $description "Generate a new sequence where all runs of elements for which the predicate returns true are replaced by a single instance of " { $snippet "elt" } ".  Runs at the beginning or end of the sequence for which the predicate returns true are removed." }
67 { $see-also collapse }
68 { $examples
69     "Collapse multiple spaces in a string down to a single space"
70     { $example "USING: kernel prettyprint sequences.extras ;" "\"   Hello,    crazy    world   \" [ CHAR: \\s = ] \" \" compact ." "\"Hello, crazy world\"" } } ;
71
72 HELP: <evens>
73 { $values { "seq" sequence } { "evens" evens } }
74 { $description "Create a virtual sequence whose elements consist of the even-indexed elements from the original sequence." }
75 { $notes "Because sequences are zero-indexed, this collection includes the first, third, fifth, etc. elements of the actual sequence which can be counterintuitive." }
76 { $see-also <odds> } ;
77
78 HELP: find-all
79 { $values
80     { "seq" sequence }
81     { "quot" { $quotation ( ... elt -- ... ? ) } }
82     { "elts" "the indices of the matching elements" } }
83 { $description "Similar to " { $link find } ", but finds all of the indices and elements that match the provided quotation, not just the first." }
84 { $notes "The result is provided as an array of arrays, whose first value is the index and whose second value is the element." } ;
85
86 HELP: first=
87 { $values
88     { "seq" sequence }
89     { "elt" object }
90     { "?" boolean } }
91 { $description "Checks whether the first element of " { $snippet "seq" } " is equal to " { $snippet "elt" } "." } ;
92
93 HELP: first?
94 { $values
95     { "seq" sequence }
96     { "quot" { $quotation ( ... elt -- ... ? ) } }
97     { "?" boolean } }
98 { $description "Tests whether the first element of " { $snippet "seq" } " satisfies the provided predicate." } ;
99
100 HELP: fourth=
101 { $values
102     { "seq" sequence }
103     { "elt" object }
104     { "?" boolean } }
105 { $description "Checks whether the fourth element of " { $snippet "seq" } " is equal to " { $snippet "elt" } "." } ;
106
107 HELP: fourth?
108 { $values
109     { "seq" sequence }
110     { "quot" { $quotation ( ... elt -- ... ? ) } }
111     { "?" boolean } }
112 { $description "Tests whether the fourth element of " { $snippet "seq" } " satisfies the provided predicate." } ;
113
114 HELP: <odds>
115 { $values { "seq" sequence } { "odds" odds } }
116 { $description "Create a virtual sequence whose elements consist of the odd-indexed elements from the original sequence." }
117 { $notes "Because sequences are zero-indexed, this collection includes the second, fourth, sixth, etc. elements of the actual sequence which can be counterintuitive." }
118 { $see-also <evens> } ;
119
120 HELP: >resizable
121 { $values { "seq" sequence } { "accum" sequence } }
122 { $description "Converts a sequence into the nearest resizable equivalent, preserving its contents." } ;
123
124 HELP: second=
125 { $values
126     { "seq" sequence }
127     { "elt" object }
128     { "?" boolean } }
129 { $description "Checks whether the second element of " { $snippet "seq" } " is equal to " { $snippet "elt" } "." } ;
130
131 HELP: second?
132 { $values
133     { "seq" sequence }
134     { "quot" { $quotation ( ... elt -- ... ? ) } }
135     { "?" boolean } }
136 { $description "Tests whether the second element of " { $snippet "seq" } " satisfies the provided predicate." } ;
137
138 HELP: subseq*
139 { $values
140      { "from" integer } { "to" integer } { "seq" sequence } { "subseq" sequence } }
141 { $description "Outputs a new sequence using positions relative to one or both ends of the sequence. Positive values describes offsets relative to the start of the sequence, negative values relative to the end. Values of " { $link f } " for " { $snippet "from" } " indicate the beginning of the sequence, while an " { $link f } " for " { $snippet "to" } " indicates the end of the sequence." }
142 { $notes "Both " { $snippet "from" } " and " { $snippet "to" } " can be safely set to values outside the length of the sequence. Also, " { $snippet "from" } " can safely reference a smaller or greater index position than " { $snippet "to" } "." }
143 { $examples
144     "Using a negative relative index:"
145     { $example "USING: prettyprint sequences.extras ; 2 -1 \"abcdefg\" subseq* ."
146                "\"cdef\""
147     }
148     "Using optional indices:"
149     { $example "USING: prettyprint sequences.extras ; f -4 \"abcdefg\" subseq* ."
150                "\"abc\""
151     }
152     "Using larger-than-necessary indices:"
153     { $example "USING: prettyprint sequences.extras ; 0 10 \"abcdefg\" subseq* ."
154                "\"abcdefg\""
155     }
156     "Trimming from either end of the sequence."
157     { $example "USING: prettyprint sequences.extras ; 1 -1 \"abcdefg\" subseq* ."
158                "\"bcdef\""
159     }
160 } ;
161
162 HELP: third=
163 { $values
164     { "seq" sequence }
165     { "elt" object }
166     { "?" boolean } }
167 { $description "Checks whether the third element of " { $snippet "seq" } " is equal to " { $snippet "elt" } "." } ;
168
169 HELP: third?
170 { $values
171     { "seq" sequence }
172     { "quot" { $quotation ( ... elt -- ... ? ) } }
173     { "?" boolean } }
174 { $description "Tests whether the third element of " { $snippet "seq" } " satisfies the provided predicate." } ;
175
176 HELP: unsurround
177 { $values
178     { "newseq" sequence }
179     { "seq2" sequence }
180     { "seq3" sequence }
181     { "seq1" sequence } }
182 { $description "Reverses the result of a " { $link surround } " call, stripping off the prefix " { $snippet "seq2" } " and suffix " { $snippet "seq3" } " to restore the original sequence " { $snippet "seq" } "." }
183 { $see-also surround } ;
184
185 HELP: start-all
186 { $values
187      { "subseq" sequence } { "seq" sequence } { "indices" sequence } }
188 { $description "Outputs the starting indices of the non-overlapping occurrences of " { $snippet "subseq" } " in " { $snippet "seq" } "." }
189 { $examples
190     { $example "USING: prettyprint sequences.extras ; \"ABA\" \"ABABA\" start-all ."
191                "{ 0 }"
192     }
193     { $example "USING: prettyprint sequences.extras ; \"ABA\" \"ABAABA\" start-all ."
194       "{ 0 3 }"
195     }
196  } ;
197
198 HELP: start-all*
199 { $values
200     { "subseq" sequence } { "seq" sequence } { "indices" sequence } }
201 { $description "Outputs the starting indices of the possibly overlapping occurrences of " { $snippet "subseq" } " in " { $snippet "seq" } "." }
202 { $examples
203     { $example "USING: prettyprint sequences.extras ; \"ABA\" \"ABABA\" start-all* ."
204                "{ 0 2 }"
205     } } ;
206
207 HELP: count-subseq
208 { $values
209     { "subseq" sequence } { "seq" sequence } { "n" integer } }
210 { $description "Outputs the number of non-overlapping occurrences of " { $snippet "subseq" } " in " { $snippet "seq" } "." }
211 { $examples
212     { $example "USING: prettyprint sequences.extras ; \"ABA\" \"ABABA\" count-subseq ."
213                "1"
214     } } ;
215
216
217 HELP: count-subseq*
218 { $values
219     { "subseq" sequence } { "seq" sequence } { "n" integer } }
220 { $description "Outputs the number of possibly overlapping occurrences of " { $snippet "subseq" } " in " { $snippet "seq" } "." }
221 { $examples
222     { $example "USING: prettyprint sequences.extras ; \"ABA\" \"ABABA\" count-subseq* ."
223                "2"
224     } } ;
225
226 { start-all start-all* count-subseq count-subseq* } related-words