1 USING: accessors arrays ascii grouping io io.streams.string
2 kernel make math prettyprint ranges sequences sequences.extras
5 { V{ { 0 104 } { 2 108 } { 3 108 } } } [ "hello" [ even? ] find-all ] unit-test
7 { { "a" "b" "c" "d" "ab" "bc" "cd" "abc" "bcd" "abcd" } } [ "abcd" all-subseqs ] unit-test
9 { { "a" "ab" "abc" "abcd" "b" "bc" "bcd" "c" "cd" "d" } }
10 [ [ "abcd" [ , ] each-subseq ] { } make ] unit-test
12 { B{ 115 } } [ 1 2 "asdf" B{ } subseq-as ] unit-test
14 { "" } [ "abc" "def" longest-subseq ] unit-test
15 { "abcd" } [ "abcd" "abcde" longest-subseq ] unit-test
16 { "foo" } [ "foo" "foobar" longest-subseq ] unit-test
17 { "foo" } [ "foobar" "foo" longest-subseq ] unit-test
19 { "" "" } [ "" "" CHAR: ? pad-longest ] unit-test
20 { "abc" "def" } [ "abc" "def" CHAR: ? pad-longest ] unit-test
21 { " " "abc" } [ "" "abc" CHAR: \s pad-longest ] unit-test
22 { "abc" " " } [ "abc" "" CHAR: \s pad-longest ] unit-test
23 { "abc..." "foobar" } [ "abc" "foobar" CHAR: . pad-longest ] unit-test
37 "ABC" 8 <iota> [ CHAR: - pad-center ] with map
41 { 0 0 0 0 } { 1 3 } over [ 1 + ] change-nths
45 { 1 2 3 } [ even? ] selector* [ each ] dip
48 { { 1 3 5 } } [ { 1 2 3 4 5 6 } [ nip even? ] filter-index ] unit-test
50 { V{ 1 3 5 } } [ { 1 2 3 4 5 6 } [ nip even? ] V{ } filter-index-as ] unit-test
52 { { 1 3 5 } } [ { 1 2 3 4 5 6 } even-indices ] unit-test
54 { { 2 4 6 } } [ { 1 2 3 4 5 6 } odd-indices ] unit-test
57 [ "a b \t \n \r c d \n e " [ ascii:blank? ] " " compact ] unit-test
60 [ " a b c d e " [ ascii:blank? ] " " collapse ] unit-test
62 { { "hello," " " "world!" " " " " } }
63 [ "hello, world! " [ ascii:blank? ] slice-when [ >string ] map ] unit-test
66 [ "abc" sequence>slice slice? ] unit-test
69 [ "abc" sequence>slice >string ] unit-test
71 { t } [ "abcdef" [ 0 3 rot <slice> ] [ 2 4 rot <slice> ] bi slices-overlap? ] unit-test
72 { t } [ "abcdef" [ 0 3 rot <slice> ] [ 1 2 rot <slice> ] bi slices-overlap? ] unit-test
73 { f } [ "abcdef" [ 0 3 rot <slice> ] [ 3 6 rot <slice> ] bi slices-overlap? ] unit-test
74 { t } [ "abcdef" [ 0 3 rot <slice> ] [ 2 4 rot <slice> ] bi slices-touch? ] unit-test
75 { t } [ "abcdef" [ 0 3 rot <slice> ] [ 1 2 rot <slice> ] bi slices-touch? ] unit-test
76 { t } [ "abcdef" [ 0 3 rot <slice> ] [ 3 6 rot <slice> ] bi slices-touch? ] unit-test
77 { f } [ "abcdef" [ 0 3 rot <slice> ] [ 4 6 rot <slice> ] bi slices-touch? ] unit-test
80 "abcdef" [ 0 3 rot <slice> ] [ 3 6 rot <slice> ] bi merge-slices >string
84 "abcdef" [ 3 6 rot <slice> ] [ 0 3 rot <slice> ] bi merge-slices >string
88 "abcdef" [ 0 3 rot <slice> ] [ 0 3 rot <slice> ] bi merge-slices >string
92 { "hello" "hello" } [ "hello" dup 0 rotate ] unit-test
93 { "hello" "llohe" } [ "hello" dup 2 rotate ] unit-test
94 { "hello" "lohel" } [ "hello" dup 13 rotate ] unit-test
95 { "hello" "ohell" } [ "hello" dup -1 rotate ] unit-test
96 { "hello" "lohel" } [ "hello" dup -12 rotate ] unit-test
98 { "hello" } [ "hello" dup 0 rotate! ] unit-test
99 { "llohe" } [ "hello" dup 2 rotate! ] unit-test
100 { "lohel" } [ "hello" dup 13 rotate! ] unit-test
101 { "ohell" } [ "hello" dup -1 rotate! ] unit-test
102 { "lohel" } [ "hello" dup -12 rotate! ] unit-test
104 { { } } [ { } [ ] map-concat ] unit-test
105 { V{ 0 0 1 0 1 2 } } [ 4 <iota> [ <iota> ] map-concat ] unit-test
106 { "abc" } [ "abc" [ 1string ] map-concat ] unit-test
107 { "abc" } [ { 97 98 99 } [ 1string ] map-concat ] unit-test
108 { { 97 98 99 } } [ "abc" [ 1string ] { } map-concat-as ] unit-test
109 { "baz" { "foobaz" "barbaz" } }
110 [ "baz" { { "foo" } { "bar" } } [ [ over append ] map ] map-concat ] unit-test
112 { { } } [ { } [ ] [ even? ] map-filter ] unit-test
113 { "bcde" } [ "abcd" [ 1 + ] [ drop t ] map-filter ] unit-test
114 { { 0 4 16 36 64 } } [ 10 <iota> [ sq ] [ even? ] { } map-filter-as ] unit-test
116 { 120000 } [ { 10 20 30 40 50 60 } 1 [ * ] 3 reduce-from ] unit-test
119 { 1 2 3 } { 4 5 6 } 0 [ + + ] 0 2reduce-from
123 { 1 2 3 } { 4 5 6 } 0 [ + + ] 1 2reduce-from
126 { V{ 0 4 16 36 64 } } [ 10 <iota> [ even? ] [ sq ] filter-map ] unit-test
127 { V{ 0 4 16 36 64 } } [ 10 <iota> [ even? ] [ sq ] filter-map ] unit-test
128 { V{ 2 6 10 14 18 } } [ 10 <iota> [ odd? ] [ 2 * ] V{ } filter-map-as ] unit-test
129 { { 2 6 10 14 18 } } [ 10 <iota> [ odd? ] [ 2 * ] { } filter-map-as ] unit-test
131 { V{ 1 9 25 49 81 } } [ 10 <iota> [ even? ] [ sq ] reject-map ] unit-test
132 { V{ 1 9 25 49 81 } } [ 10 <iota> [ even? ] [ sq ] reject-map ] unit-test
133 { V{ 0 4 8 12 16 } } [ 10 <iota> [ odd? ] [ 2 * ] V{ } reject-map-as ] unit-test
134 { { 0 4 8 12 16 } } [ 10 <iota> [ odd? ] [ 2 * ] { } reject-map-as ] unit-test
136 { V{ 0 4 16 36 64 } } [ 10 <iota> [ dup even? [ sq t ] [ f ] if ] filter-map* ] unit-test
137 { V{ 0 4 16 36 64 } } [ 10 <iota> [ sq dup even? ] filter-map* ] unit-test
138 { V{ 2 6 10 14 18 } } [ 10 <iota> [ dup odd? [ 2 * t ] [ f ] if ] V{ } filter-map-as* ] unit-test
139 { { 2 6 10 14 18 } } [ 10 <iota> [ dup odd? [ 2 * t ] [ f ] if ] { } filter-map-as* ] unit-test
141 { V{ 1 9 25 49 81 } } [ 10 <iota> [ dup even? [ t ] [ sq f ] if ] reject-map* ] unit-test
142 { V{ 1 9 25 49 81 } } [ 10 <iota> [ sq dup even? ] reject-map* ] unit-test
143 { V{ 0 4 8 12 16 } } [ 10 <iota> [ dup odd? [ t ] [ 2 * f ] if ] V{ } reject-map-as* ] unit-test
144 { { 0 4 8 12 16 } } [ 10 <iota> [ dup odd? [ t ] [ 2 * f ] if ] { } reject-map-as* ] unit-test
146 { 8 } [ 3 <iota> dup [ 1 + * ] 2map-sum ] unit-test
147 { 4 } [ "hello" "jello" [ = ] 2count ] unit-test
149 { { } } [ { } round-robin ] unit-test
150 { "ADEBFC" } [ { "ABC" "D" "EF" } round-robin >string ] unit-test
152 { { } } [ "ABC" [ ] { } trim-as ] unit-test
153 { "ABC" } [ { 32 65 66 67 32 } [ ascii:blank? ] "" trim-as ] unit-test
155 { t } [ "ABC" dup [ ascii:blank? ] ?trim [ identity-hashcode ] same? ] unit-test
156 { "ABC" } [ " ABC " [ ascii:blank? ] ?trim ] unit-test
158 { t } [ "ABC" dup [ ascii:blank? ] ?trim-head [ identity-hashcode ] same? ] unit-test
159 { t } [ "ABC" dup [ ascii:blank? ] ?trim-tail [ identity-hashcode ] same? ] unit-test
160 { "ABC " } [ " ABC " [ ascii:blank? ] ?trim-head ] unit-test
161 { " ABC" } [ " ABC " [ ascii:blank? ] ?trim-tail ] unit-test
163 { "" } [ "" "" "" unsurround ] unit-test
164 { "" } [ " " " " " " unsurround ] unit-test
165 { "foo.com" } [ "http://foo.com" "http://" "/" unsurround ] unit-test
167 { t } [ { 1 3 5 7 } [ even? ] none? ] unit-test
168 { f } [ { 1 2 3 4 } [ even? ] none? ] unit-test
169 { t } [ { } [ even? ] none? ] unit-test
171 { f } [ { 1 2 3 4 } [ even? ] one? ] unit-test
172 { t } [ { 1 2 3 } [ even? ] one? ] unit-test
173 { f } [ { } [ even? ] one? ] unit-test
175 { { { 5 8 0 } { 6 9 1 } { 7 10 2 } } } [ { 5 6 7 } { 8 9 10 } [ 3array ] 2map-index ] unit-test
177 { { } } [ { } <evens> >array ] unit-test
178 { { 0 2 } } [ 4 <iota> <evens> >array ] unit-test
179 { { 0 2 4 } } [ 5 <iota> <evens> >array ] unit-test
180 { "bbddff" } [ "abcdef" <evens> [ 1 + ] map! seq>> ] unit-test
182 { { } } [ { } <odds> >array ] unit-test
183 { { 1 3 } } [ 5 <iota> <odds> >array ] unit-test
184 { { 1 3 5 } } [ 6 <iota> <odds> >array ] unit-test
185 { "acceeg" } [ "abcdef" <odds> [ 1 + ] map! seq>> ] unit-test
187 { 1 } [ { 1 7 3 7 6 3 7 } arg-max ] unit-test
188 { 2 } [ { 0 1 99 } arg-max ] unit-test
189 { 0 } [ { 1 7 3 7 6 3 7 } arg-min ] unit-test
190 { V{ 0 4 } } [ { 5 3 2 10 5 } [ 5 = ] arg-where ] unit-test
191 { { 2 1 0 4 3 } } [ { 5 3 2 10 5 } arg-sort ] unit-test
193 { 10 } [ { 4 3 2 1 } [ 10 * ] map-infimum ] unit-test
194 { 40 } [ { 4 3 2 1 } [ 10 * ] map-supremum ] unit-test
196 { t } [ { 1 2 3 4 5 } 1 first= ] unit-test
197 { t } [ { 1 2 3 4 5 } 2 second= ] unit-test
198 { t } [ { 1 2 3 4 5 } 3 third= ] unit-test
199 { t } [ { 1 2 3 4 5 } 4 fourth= ] unit-test
200 { t } [ { 1 2 3 4 5 } 5 last= ] unit-test
201 { t } [ 4 { 1 2 3 4 5 } 5 nth= ] unit-test
203 { t } [ { 1 2 3 4 5 } [ 1 = ] first? ] unit-test
204 { t } [ { 1 2 3 4 5 } [ 2 = ] second? ] unit-test
205 { t } [ { 1 2 3 4 5 } [ 3 = ] third? ] unit-test
206 { t } [ { 1 2 3 4 5 } [ 4 = ] fourth? ] unit-test
207 { t } [ { 1 2 3 4 5 } [ 5 = ] last? ] unit-test
208 { t } [ 4 { 1 2 3 4 5 } [ 5 = ] nth? ] unit-test
210 { { 97 115 100 102 } } [
211 "asdf" [ [ read1 ] loop>array ] with-string-reader
214 { V{ 97 115 100 102 } } [
215 "asdf" [ [ read1 ] V{ } loop>sequence ] with-string-reader
218 { "" } [ { } "" reverse-as ] unit-test
219 { "ABC" } [ { 67 66 65 } "" reverse-as ] unit-test
221 { V{ 1 } } [ 1 0 V{ } [ insert-nth! ] keep ] unit-test
222 { V{ 1 2 3 4 } } [ 2 1 V{ 1 3 4 } [ insert-nth! ] keep ] unit-test
224 { V{ 1 3 } } [ V{ 1 2 3 } 1 2 delete-slice-of ] unit-test
225 { V{ 1 2 } } [ V{ 1 2 3 } 2 remove-nth-of! ] unit-test
228 T{ slice { to 1 } { seq V{ 1 2 3 4 5 } } }
229 T{ slice { from 2 } { to 5 } { seq V{ 1 2 3 4 5 } } }
231 V{ 1 2 3 4 5 } 1 2 snip-slice-of
234 { V{ 1 } V{ 3 4 5 } } [
235 V{ 1 2 3 4 5 } 1 2 snip-of
238 { "abc" } [ B{ 97 98 99 100 101 102 103 } 3 "" head-as ] unit-test
239 { "abcd" } [ B{ 97 98 99 100 101 102 103 } 3 "" head*-as ] unit-test
240 { "defg" } [ B{ 97 98 99 100 101 102 103 } 3 "" tail-as ] unit-test
241 { "efg" } [ B{ 97 98 99 100 101 102 103 } 3 "" tail*-as ] unit-test
243 { { 1 0 0 1 0 0 0 1 0 0 } }
244 [ 1 { 0 3 7 } 10 0 <array> [ set-nths ] keep ] unit-test
246 { { 1 0 0 1 0 0 0 1 0 0 } }
247 [ 1 { 0 3 7 } 10 0 <array> [ set-nths-unsafe ] keep ] unit-test
249 { t 3 3 } [ 10 <iota> [ [ odd? ] [ 1 > ] bi* and ] map-find-index ] unit-test
250 { f f f } [ 10 <iota> [ [ odd? ] [ 9 > ] bi* and ] map-find-index ] unit-test
253 [ { 10 20 30 } [ sq ] 1 map-from ] unit-test
256 [ { 10 20 30 } [ sq ] 1 V{ } map-from-as ] unit-test
258 { "abcdef" } [ f f "abcdef" subseq* ] unit-test
259 { "abcdef" } [ 0 f "abcdef" subseq* ] unit-test
260 { "ab" } [ f 2 "abcdef" subseq* ] unit-test
261 { "cdef" } [ 2 f "abcdef" subseq* ] unit-test
262 { "cd" } [ -4 -2 "abcdef" subseq* ] unit-test
264 { "foo" "" } [ "foo" [ ascii:blank? ] cut-when ] unit-test
265 { "foo" " " } [ "foo " [ ascii:blank? ] cut-when ] unit-test
266 { "" " foo" } [ " foo" [ ascii:blank? ] cut-when ] unit-test
267 { "foo" " bar" } [ "foo bar" [ ascii:blank? ] cut-when ] unit-test
269 { { 4 0 3 1 2 } } [ { 0 4 1 3 2 } 5 <iota> [ nth* ] curry map ] unit-test
271 { 1 "beef" } [ { "chicken" "beef" "moose" } [ length ] infimum-by* ] unit-test
272 { 0 "chicken" } [ { "chicken" "beef" "moose" } [ length ] supremum-by* ] unit-test
273 { 2 "moose" } [ { "chicken" "beef" "moose" } [ first ] supremum-by* ] unit-test
274 { f } [ f ?supremum ] unit-test
275 { f } [ { } ?supremum ] unit-test
276 { f } [ { f } ?supremum ] unit-test
277 { 3 } [ { 1 f 3 2 } ?supremum ] unit-test
278 { 3 } [ { 1 3 2 } ?supremum ] unit-test
279 { f } [ f ?infimum ] unit-test
280 { f } [ { } ?infimum ] unit-test
281 { f } [ { f } ?infimum ] unit-test
282 { 1 } [ { 1 f 3 2 } ?infimum ] unit-test
283 { 1 } [ { 1 3 2 } ?infimum ] unit-test
285 { 3/10 } [ 10 <iota> [ 3 < ] percent-of ] unit-test
287 { { 0 } } [ "ABABA" "ABA" start-all ] unit-test
288 { { 0 2 } } [ "ABABA" "ABA" start-all* ] unit-test
289 { { 0 3 } } [ "ABAABA" "ABA" start-all ] unit-test
290 { 1 } [ "ABABA" "ABA" count-subseq ] unit-test
291 { 2 } [ "ABABA" "ABA" count-subseq* ] unit-test
293 { 0 } [ { } [ + ] 0reduce ] unit-test
294 { 107 } [ { 100 1 2 4 } [ + ] 0reduce ] unit-test
295 { 0 } [ { 100 1 2 4 } [ * ] 0reduce ] unit-test
297 { f } [ { } [ + ] 1reduce ] unit-test
298 { 107 } [ { 100 1 2 4 } [ + ] 1reduce ] unit-test
299 { 800 } [ { 100 1 2 4 } [ * ] 1reduce ] unit-test
301 { 800 } [ { 100 1 2 4 } [ * ] 1 reduce-of ] unit-test
302 { 800 { 1 100 100 200 } } [ { 100 1 2 4 } [ * ] 1 accumulate-of ] unit-test
304 { { } } [ { } [ + ] 0accumulate ] unit-test
305 { { 100 101 103 107 } } [ { 100 1 2 4 } [ + ] 0accumulate ] unit-test
309 { 0 0 1 1 2 3 4 2 3 4 5 }
311 { 2 7 1 8 1 7 1 8 2 8 4 } [ even? ] occurrence-count-by
315 H{ { 8 3 } { 1 3 } { 2 2 } { 4 1 } { 7 2 } }
316 { 0 0 0 0 1 1 2 1 1 2 0 }
318 { 2 7 1 8 1 7 1 8 2 8 4 } [ ] occurrence-count-by
323 { 97 1 } { 98 1 } { 99 1 } { 100 1 } { 101 1 } { 102 1 }
324 { 103 1 } { 104 1 } { 105 1 } { 106 1 } { 107 1 } { 108 1 }
325 { 109 1 } { 110 1 } { 111 1 } { 112 1 }
327 { 1 2 0 3 3 3 3 3 3 3 3 3 3 3 3 3 }
329 "cab" "abcdefghijklmnop" progressive-index
332 { H{ { 97 5 } } { 0 1 2 3 3 } } [
333 "aaa" "aaaaa" progressive-index
336 { H{ { 97 5 } { 98 5 } } { 0 3 1 4 2 5 5 5 5 5 } } [
337 "aaabb" "ababababab" progressive-index
340 { { 0 1 2 3 } } [ 8 <iota> [ 4 < ] take-while >array ] unit-test
341 { { } } [ { 15 16 } [ 4 < ] take-while >array ] unit-test
342 { { 0 1 2 } } [ 3 <iota> [ 4 < ] take-while >array ] unit-test
344 { { 4 5 6 7 } } [ 8 <iota> [ 4 < ] drop-while >array ] unit-test
345 { { 15 16 } } [ { 15 16 } [ 4 < ] drop-while >array ] unit-test
346 { { } } [ 3 <iota> [ 4 < ] drop-while >array ] unit-test
348 { { } } [ { } ", " interleaved ] unit-test
349 { { 1 } } [ { 1 } ", " interleaved ] unit-test
350 { { 1 ", " 2 } } [ { 1 2 } ", " interleaved ] unit-test
351 { "" } [ "" CHAR: _ interleaved ] unit-test
352 { "a" } [ "a" CHAR: _ interleaved ] unit-test
353 { "a_b" } [ "ab" CHAR: _ interleaved ] unit-test
354 { "a_b_c" } [ "abc" CHAR: _ interleaved ] unit-test
355 { "a_b_c_d" } [ "abcd" CHAR: _ interleaved ] unit-test
357 { 0 } [ { 1 2 3 4 } [ 5 > ] count-head ] unit-test
358 { 2 } [ { 1 2 3 4 } [ 3 < ] count-head ] unit-test
359 { 4 } [ { 1 2 3 4 } [ 5 < ] count-head ] unit-test
361 { 0 } [ { 1 2 3 4 } [ 5 > ] count-tail ] unit-test
362 { 2 } [ { 1 2 3 4 } [ 2 > ] count-tail ] unit-test
363 { 4 } [ { 1 2 3 4 } [ 5 < ] count-tail ] unit-test
365 { SBUF" aco" SBUF" ftr" } [ SBUF" factor" dup [ even? ] extract! ] unit-test
367 { 25 5 1 } [ { 4 5 6 } [ sq ] [ 20 > ] find-pred ] unit-test
368 { f f f } [ { 4 5 6 } [ sq ] [ 200 > ] find-pred ] unit-test
370 { -1/0. } [ { } max-subarray-sum ] unit-test
371 { -2 } [ { -3 -2 } max-subarray-sum ] unit-test
372 { 7 } [ { 1 2 3 -4 5 } max-subarray-sum ] unit-test
373 { 6 } [ { 1 2 3 -4 1 1 } max-subarray-sum ] unit-test
375 { { 9 7 5 } } [ -1 -6 -2 10 <iota> <step-slice> >array ] unit-test
376 { { 9 7 } } [ -1 -5 -2 10 <iota> <step-slice> >array ] unit-test
377 { { 9 7 } } [ -1 -4 -2 10 <iota> <step-slice> >array ] unit-test
378 { { 9 } } [ -1 -3 -2 10 <iota> <step-slice> >array ] unit-test
379 { { } } [ -4 10 -2 10 <iota> <step-slice> >array ] unit-test
380 { { 6 8 } } [ -4 15 2 10 <iota> <step-slice> >array ] unit-test
381 { { 1 3 } } [ 1 4 2 10 <iota> <step-slice> >array ] unit-test
382 { { 1 3 } } [ 1 5 2 10 <iota> <step-slice> >array ] unit-test
383 { { 1 3 5 } } [ 1 6 2 10 <iota> <step-slice> >array ] unit-test
385 { { 102 306 1530 } } [
386 { 2 3 5 } [ swap [ * ] [ 100 + ] if* ] map-with-previous
390 { } [ nip ] map-with-previous
393 { { -1 2 -3 4 -5 } } [ { 1 2 3 4 5 } [ odd? ] [ neg ] map-if ] unit-test
395 { { { 100 0 } { 200 1 } { 300 2 } { 400 3 } } } [
396 { 100 200 300 400 } <zip-index> >array
400 { } [ - . ] each-prior
404 1000 { } [ - . ] each-prior-from
408 { 5 16 42 103 } [ - . ] each-prior
412 1 { 5 16 42 103 } [ - . ] each-prior-from
420 { V{ 5 11 26 61 } } [
421 V{ 5 16 42 103 } [ - ] map-prior
425 2 V{ 5 16 42 103 } [ - ] map-prior-from
429 2 { 5 16 42 103 } [ - ] V{ } map-prior-from-as
433 { } [ - ] V{ } map-prior-as
437 V{ 5 16 42 103 } [ - ] { } map-prior-as
440 { f } [ 0 CHAR: a "foo" nth-index ] unit-test
441 { 0 } [ 0 CHAR: a "abba" nth-index ] unit-test
442 { 3 } [ 1 CHAR: a "abba" nth-index ] unit-test
443 { f } [ 2 CHAR: a "abba" nth-index ] unit-test
445 { { -995 11 26 61 } } [
446 1000 V{ 5 16 42 103 } [ - ] { } map-prior-identity-as
451 [ 2dup 2array all-eq? [ * ] [ 2drop f ] if ]
452 V{ } 2nested-filter-as
456 { 1 2 3 } { 1 2 3 } { 1 2 3 }
457 [ 3dup 3array all-eq? [ * * ] [ 3drop f ] if ]
458 V{ } 3nested-filter-as
461 { V{ 0 2 0 3 6 4 12 0 5 10 15 20 } } [
464 [ 2dup [ odd? ] bi@ or [ * ] [ 2drop f ] if ]