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 { V{ 0 4 16 36 64 } } [ 10 <iota> [ even? ] [ sq ] filter-map ] unit-test
117 { { 2 6 10 14 18 } } [ 10 <iota> [ odd? ] [ 2 * ] { } filter-map-as ] unit-test
119 { 8 } [ 3 <iota> dup [ 1 + * ] 2map-sum ] unit-test
120 { 4 } [ "hello" "jello" [ = ] 2count ] unit-test
122 { { } } [ { } round-robin ] unit-test
123 { "ADEBFC" } [ { "ABC" "D" "EF" } round-robin >string ] unit-test
125 { { } } [ "ABC" [ ] { } trim-as ] unit-test
126 { "ABC" } [ { 32 65 66 67 32 } [ ascii:blank? ] "" trim-as ] unit-test
128 { t } [ "ABC" dup [ ascii:blank? ] ?trim [ identity-hashcode ] same? ] unit-test
129 { "ABC" } [ " ABC " [ ascii:blank? ] ?trim ] unit-test
131 { t } [ "ABC" dup [ ascii:blank? ] ?trim-head [ identity-hashcode ] same? ] unit-test
132 { t } [ "ABC" dup [ ascii:blank? ] ?trim-tail [ identity-hashcode ] same? ] unit-test
133 { "ABC " } [ " ABC " [ ascii:blank? ] ?trim-head ] unit-test
134 { " ABC" } [ " ABC " [ ascii:blank? ] ?trim-tail ] unit-test
136 { "" } [ "" "" "" unsurround ] unit-test
137 { "" } [ " " " " " " unsurround ] unit-test
138 { "foo.com" } [ "http://foo.com" "http://" "/" unsurround ] unit-test
140 { t } [ { 1 3 5 7 } [ even? ] none? ] unit-test
141 { f } [ { 1 2 3 4 } [ even? ] none? ] unit-test
142 { t } [ { } [ even? ] none? ] unit-test
144 { f } [ { 1 2 3 4 } [ even? ] one? ] unit-test
145 { t } [ { 1 2 3 } [ even? ] one? ] unit-test
146 { f } [ { } [ even? ] one? ] unit-test
148 { { { 5 8 0 } { 6 9 1 } { 7 10 2 } } } [ { 5 6 7 } { 8 9 10 } [ 3array ] 2map-index ] unit-test
150 { { } } [ { } <evens> >array ] unit-test
151 { { 0 2 } } [ 4 <iota> <evens> >array ] unit-test
152 { { 0 2 4 } } [ 5 <iota> <evens> >array ] unit-test
153 { "bbddff" } [ "abcdef" <evens> [ 1 + ] map! seq>> ] unit-test
155 { { } } [ { } <odds> >array ] unit-test
156 { { 1 3 } } [ 5 <iota> <odds> >array ] unit-test
157 { { 1 3 5 } } [ 6 <iota> <odds> >array ] unit-test
158 { "acceeg" } [ "abcdef" <odds> [ 1 + ] map! seq>> ] unit-test
160 { 1 } [ { 1 7 3 7 6 3 7 } arg-max ] unit-test
161 { 2 } [ { 0 1 99 } arg-max ] unit-test
162 { 0 } [ { 1 7 3 7 6 3 7 } arg-min ] unit-test
163 { V{ 0 4 } } [ { 5 3 2 10 5 } [ 5 = ] arg-where ] unit-test
164 { { 2 1 0 4 3 } } [ { 5 3 2 10 5 } arg-sort ] unit-test
166 { 10 } [ { 4 3 2 1 } [ 10 * ] map-infimum ] unit-test
167 { 40 } [ { 4 3 2 1 } [ 10 * ] map-supremum ] unit-test
169 { t } [ { 1 2 3 4 5 } 1 first= ] unit-test
170 { t } [ { 1 2 3 4 5 } 2 second= ] unit-test
171 { t } [ { 1 2 3 4 5 } 3 third= ] unit-test
172 { t } [ { 1 2 3 4 5 } 4 fourth= ] unit-test
173 { t } [ { 1 2 3 4 5 } 5 last= ] unit-test
174 { t } [ 4 { 1 2 3 4 5 } 5 nth= ] unit-test
176 { t } [ { 1 2 3 4 5 } [ 1 = ] first? ] unit-test
177 { t } [ { 1 2 3 4 5 } [ 2 = ] second? ] unit-test
178 { t } [ { 1 2 3 4 5 } [ 3 = ] third? ] unit-test
179 { t } [ { 1 2 3 4 5 } [ 4 = ] fourth? ] unit-test
180 { t } [ { 1 2 3 4 5 } [ 5 = ] last? ] unit-test
181 { t } [ 4 { 1 2 3 4 5 } [ 5 = ] nth? ] unit-test
183 { { 97 115 100 102 } } [
184 "asdf" [ [ read1 ] loop>array ] with-string-reader
187 { V{ 97 115 100 102 } } [
188 "asdf" [ [ read1 ] V{ } loop>sequence ] with-string-reader
191 { "" } [ { } "" reverse-as ] unit-test
192 { "ABC" } [ { 67 66 65 } "" reverse-as ] unit-test
194 { V{ 1 } } [ 1 0 V{ } [ insert-nth! ] keep ] unit-test
195 { V{ 1 2 3 4 } } [ 2 1 V{ 1 3 4 } [ insert-nth! ] keep ] unit-test
197 { "abc" } [ B{ 97 98 99 100 101 102 103 } 3 "" head-as ] unit-test
198 { "abcd" } [ B{ 97 98 99 100 101 102 103 } 3 "" head*-as ] unit-test
199 { "defg" } [ B{ 97 98 99 100 101 102 103 } 3 "" tail-as ] unit-test
200 { "efg" } [ B{ 97 98 99 100 101 102 103 } 3 "" tail*-as ] unit-test
202 { { 1 0 0 1 0 0 0 1 0 0 } }
203 [ 1 { 0 3 7 } 10 0 <array> [ set-nths ] keep ] unit-test
205 { { 1 0 0 1 0 0 0 1 0 0 } }
206 [ 1 { 0 3 7 } 10 0 <array> [ set-nths-unsafe ] keep ] unit-test
208 { t 3 3 } [ 10 <iota> [ [ odd? ] [ 1 > ] bi* and ] map-find-index ] unit-test
209 { f f f } [ 10 <iota> [ [ odd? ] [ 9 > ] bi* and ] map-find-index ] unit-test
211 { "abcdef" } [ f f "abcdef" subseq* ] unit-test
212 { "abcdef" } [ 0 f "abcdef" subseq* ] unit-test
213 { "ab" } [ f 2 "abcdef" subseq* ] unit-test
214 { "cdef" } [ 2 f "abcdef" subseq* ] unit-test
215 { "cd" } [ -4 -2 "abcdef" subseq* ] unit-test
217 { "foo" "" } [ "foo" [ ascii:blank? ] cut-when ] unit-test
218 { "foo" " " } [ "foo " [ ascii:blank? ] cut-when ] unit-test
219 { "" " foo" } [ " foo" [ ascii:blank? ] cut-when ] unit-test
220 { "foo" " bar" } [ "foo bar" [ ascii:blank? ] cut-when ] unit-test
222 { { 4 0 3 1 2 } } [ { 0 4 1 3 2 } 5 <iota> [ nth* ] curry map ] unit-test
224 { 1 "beef" } [ { "chicken" "beef" "moose" } [ length ] infimum-by* ] unit-test
225 { 0 "chicken" } [ { "chicken" "beef" "moose" } [ length ] supremum-by* ] unit-test
226 { 2 "moose" } [ { "chicken" "beef" "moose" } [ first ] supremum-by* ] unit-test
227 { f } [ f ?supremum ] unit-test
228 { f } [ { } ?supremum ] unit-test
229 { f } [ { f } ?supremum ] unit-test
230 { 3 } [ { 1 f 3 2 } ?supremum ] unit-test
231 { 3 } [ { 1 3 2 } ?supremum ] unit-test
232 { f } [ f ?infimum ] unit-test
233 { f } [ { } ?infimum ] unit-test
234 { f } [ { f } ?infimum ] unit-test
235 { 1 } [ { 1 f 3 2 } ?infimum ] unit-test
236 { 1 } [ { 1 3 2 } ?infimum ] unit-test
238 { 3/10 } [ 10 <iota> [ 3 < ] count* ] unit-test
240 { { 0 } } [ "ABABA" "ABA" start-all ] unit-test
241 { { 0 2 } } [ "ABABA" "ABA" start-all* ] unit-test
242 { { 0 3 } } [ "ABAABA" "ABA" start-all ] unit-test
243 { 1 } [ "ABABA" "ABA" count-subseq ] unit-test
244 { 2 } [ "ABABA" "ABA" count-subseq* ] unit-test
246 { 120000 } [ { 10 20 30 40 50 60 } 1 [ * ] 3 reduce-from ] unit-test
248 { 0 } [ { } [ + ] 0reduce ] unit-test
249 { 107 } [ { 100 1 2 4 } [ + ] 0reduce ] unit-test
251 { { } } [ { } [ + ] 0accumulate ] unit-test
252 { { 100 101 103 107 } } [ { 100 1 2 4 } [ + ] 0accumulate ] unit-test
254 { { 0 1 2 3 } } [ 8 <iota> [ 4 < ] take-while >array ] unit-test
255 { { } } [ { 15 16 } [ 4 < ] take-while >array ] unit-test
256 { { 0 1 2 } } [ 3 <iota> [ 4 < ] take-while >array ] unit-test
258 { { 4 5 6 7 } } [ 8 <iota> [ 4 < ] drop-while >array ] unit-test
259 { { 15 16 } } [ { 15 16 } [ 4 < ] drop-while >array ] unit-test
260 { { } } [ 3 <iota> [ 4 < ] drop-while >array ] unit-test
262 { { } } [ { } ", " interleaved ] unit-test
263 { { 1 } } [ { 1 } ", " interleaved ] unit-test
264 { { 1 ", " 2 } } [ { 1 2 } ", " interleaved ] unit-test
265 { "" } [ "" CHAR: _ interleaved ] unit-test
266 { "a" } [ "a" CHAR: _ interleaved ] unit-test
267 { "a_b" } [ "ab" CHAR: _ interleaved ] unit-test
268 { "a_b_c" } [ "abc" CHAR: _ interleaved ] unit-test
269 { "a_b_c_d" } [ "abcd" CHAR: _ interleaved ] unit-test
271 { 0 } [ { 1 2 3 4 } [ 5 > ] count-head ] unit-test
272 { 2 } [ { 1 2 3 4 } [ 3 < ] count-head ] unit-test
273 { 4 } [ { 1 2 3 4 } [ 5 < ] count-head ] unit-test
275 { 0 } [ { 1 2 3 4 } [ 5 > ] count-tail ] unit-test
276 { 2 } [ { 1 2 3 4 } [ 2 > ] count-tail ] unit-test
277 { 4 } [ { 1 2 3 4 } [ 5 < ] count-tail ] unit-test
279 { SBUF" aco" SBUF" ftr" } [ SBUF" factor" dup [ even? ] extract! ] unit-test
281 { 25 5 1 } [ { 4 5 6 } [ sq ] [ 20 > ] find-pred ] unit-test
282 { f f f } [ { 4 5 6 } [ sq ] [ 200 > ] find-pred ] unit-test
284 { -1/0. } [ { } max-subarray-sum ] unit-test
285 { -2 } [ { -3 -2 } max-subarray-sum ] unit-test
286 { 7 } [ { 1 2 3 -4 5 } max-subarray-sum ] unit-test
287 { 6 } [ { 1 2 3 -4 1 1 } max-subarray-sum ] unit-test
289 { { 9 7 5 } } [ -1 -6 -2 10 <iota> <step-slice> >array ] unit-test
290 { { 9 7 } } [ -1 -5 -2 10 <iota> <step-slice> >array ] unit-test
291 { { 9 7 } } [ -1 -4 -2 10 <iota> <step-slice> >array ] unit-test
292 { { 9 } } [ -1 -3 -2 10 <iota> <step-slice> >array ] unit-test
293 { { } } [ -4 10 -2 10 <iota> <step-slice> >array ] unit-test
294 { { 6 8 } } [ -4 15 2 10 <iota> <step-slice> >array ] unit-test
295 { { 1 3 } } [ 1 4 2 10 <iota> <step-slice> >array ] unit-test
296 { { 1 3 } } [ 1 5 2 10 <iota> <step-slice> >array ] unit-test
297 { { 1 3 5 } } [ 1 6 2 10 <iota> <step-slice> >array ] unit-test
299 { { 102 306 1530 } } [
300 { 2 3 5 } [ swap [ * ] [ 100 + ] if* ] map-with-previous
304 { } [ nip ] map-with-previous
307 { { -1 2 -3 4 -5 } } [ { 1 2 3 4 5 } [ odd? ] [ neg ] map-if ] unit-test
309 { { { 100 0 } { 200 1 } { 300 2 } { 400 3 } } } [
310 { 100 200 300 400 } <zip-index> >array
314 { } [ - . ] each-prior
318 1000 { } [ - . ] each-prior-from
322 { 5 16 42 103 } [ - . ] each-prior
326 1 { 5 16 42 103 } [ - . ] each-prior-from
334 { V{ 5 11 26 61 } } [
335 V{ 5 16 42 103 } [ - ] map-prior
339 2 V{ 5 16 42 103 } [ - ] map-prior-from
343 2 { 5 16 42 103 } [ - ] V{ } map-prior-from-as
347 { } [ - ] V{ } map-prior-as
351 V{ 5 16 42 103 } [ - ] { } map-prior-as
354 { f } [ 0 CHAR: a "foo" nth-index ] unit-test
355 { 0 } [ 0 CHAR: a "abba" nth-index ] unit-test
356 { 3 } [ 1 CHAR: a "abba" nth-index ] unit-test
357 { f } [ 2 CHAR: a "abba" nth-index ] unit-test
359 { { -995 11 26 61 } } [
360 1000 V{ 5 16 42 103 } [ - ] { } map-prior-identity-as
365 [ 2dup 2array all-eq? [ * ] [ 2drop f ] if ]
366 V{ } 2nested-filter-as
370 { 1 2 3 } { 1 2 3 } { 1 2 3 }
371 [ 3dup 3array all-eq? [ * * ] [ 3drop f ] if ]
372 V{ } 3nested-filter-as
375 { V{ 0 2 0 3 6 4 12 0 5 10 15 20 } } [
378 [ 2dup [ odd? ] bi@ or [ * ] [ 2drop f ] if ]