]> gitweb.factorcode.org Git - factor.git/blob - core/sequences/sequences.factor
assocs: Add zip-index, zip-index-as and docs/tests.
[factor.git] / core / sequences / sequences.factor
1 ! Copyright (C) 2005, 2011 Slava Pestov, Daniel Ehrenberg.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors kernel kernel.private math math.order
4 math.private slots.private ;
5 IN: sequences
6
7 MIXIN: sequence
8
9 GENERIC: length ( seq -- n ) flushable
10 GENERIC: set-length ( n seq -- )
11 GENERIC: nth ( n seq -- elt ) flushable
12 GENERIC: set-nth ( elt n seq -- )
13 GENERIC: new-sequence ( len seq -- newseq ) flushable
14 GENERIC: new-resizable ( len seq -- newseq ) flushable
15 GENERIC: like ( seq exemplar -- newseq ) flushable
16 GENERIC: clone-like ( seq exemplar -- newseq ) flushable
17
18 : new-like ( len exemplar quot -- seq )
19     over [ [ new-sequence ] dip call ] dip like ; inline
20
21 M: sequence like drop ; inline
22
23 GENERIC: lengthen ( n seq -- )
24 GENERIC: shorten ( n seq -- )
25
26 M: sequence lengthen 2dup length > [ set-length ] [ 2drop ] if ; inline
27 M: sequence shorten 2dup length < [ set-length ] [ 2drop ] if ; inline
28
29 : empty? ( seq -- ? ) length 0 = ; inline
30
31 : if-empty ( ..a seq quot1: ( ..a -- ..b ) quot2: ( ..a seq -- ..b ) -- ..b )
32     [ dup empty? ] [ [ drop ] prepose ] [ ] tri* if ; inline
33
34 : when-empty ( seq quot -- ) [ ] if-empty ; inline
35
36 : unless-empty ( seq quot -- ) [ ] swap if-empty ; inline
37
38 : delete-all ( seq -- ) 0 swap set-length ;
39
40 : first ( seq -- first ) 0 swap nth ; inline
41 : second ( seq -- second ) 1 swap nth ; inline
42 : third ( seq -- third ) 2 swap nth ; inline
43 : fourth ( seq -- fourth ) 3 swap nth ; inline
44
45 : set-first ( first seq -- ) 0 swap set-nth ; inline
46 : set-second ( second seq -- ) 1 swap set-nth ; inline
47 : set-third ( third seq -- ) 2 swap set-nth ; inline
48 : set-fourth  ( fourth seq -- ) 3 swap set-nth ; inline
49
50 : push ( elt seq -- ) [ length ] [ set-nth ] bi ;
51
52 ERROR: bounds-error index seq ;
53
54 GENERIC# bounds-check? 1 ( n seq -- ? )
55
56 M: integer bounds-check? ( n seq -- ? )
57     dupd length < [ 0 >= ] [ drop f ] if ; inline
58
59 : bounds-check ( n seq -- n seq )
60     2dup bounds-check? [ bounds-error ] unless ; inline
61
62 MIXIN: immutable-sequence
63
64 ERROR: immutable element index sequence ;
65
66 M: immutable-sequence set-nth immutable ;
67
68 INSTANCE: immutable-sequence sequence
69
70 <PRIVATE
71
72 : array-nth ( n array -- elt )
73     swap 2 fixnum+fast slot ; inline
74
75 : set-array-nth ( elt n array -- )
76     swap 2 fixnum+fast set-slot ; inline
77
78 : dispatch ( n array -- ) array-nth call ;
79
80 GENERIC: resize ( n seq -- newseq ) flushable
81
82 ! Unsafe sequence protocol for inner loops
83 GENERIC: nth-unsafe ( n seq -- elt ) flushable
84 GENERIC: set-nth-unsafe ( elt n seq -- )
85
86 M: sequence nth bounds-check nth-unsafe ; inline
87 M: sequence set-nth bounds-check set-nth-unsafe ; inline
88
89 M: sequence nth-unsafe nth ; inline
90 M: sequence set-nth-unsafe set-nth ; inline
91
92 : change-nth-unsafe ( i seq quot -- )
93     [ [ nth-unsafe ] dip call ] 3keep drop set-nth-unsafe ; inline
94
95 PRIVATE>
96
97 ! The f object supports the sequence protocol trivially
98 M: f length drop 0 ; inline
99 M: f nth-unsafe nip ; inline
100 M: f like drop [ f ] when-empty ; inline
101
102 INSTANCE: f immutable-sequence
103
104 ! Integer sequences
105 TUPLE: iota-tuple { n integer read-only } ;
106
107 ERROR: non-negative-integer-expected n ;
108
109 : iota ( n -- iota )
110     dup 0 < [ non-negative-integer-expected ] when
111     iota-tuple boa ; inline
112
113 M: iota-tuple length n>> ; inline
114 M: iota-tuple nth-unsafe drop ; inline
115
116 INSTANCE: iota-tuple immutable-sequence
117
118 <PRIVATE
119
120 : first-unsafe ( seq -- first ) 0 swap nth-unsafe ; inline
121 : second-unsafe ( seq -- second ) 1 swap nth-unsafe ; inline
122 : third-unsafe ( seq -- third ) 2 swap nth-unsafe ; inline
123 : fourth-unsafe ( seq -- fourth ) 3 swap nth-unsafe ; inline
124
125 : first2-unsafe ( seq -- first second )
126     [ first-unsafe ] [ second-unsafe ] bi ; inline
127
128 : first3-unsafe ( seq -- first second third )
129     [ first2-unsafe ] [ third-unsafe ] bi ; inline
130
131 : first4-unsafe ( seq -- first second third fourth )
132     [ first3-unsafe ] [ fourth-unsafe ] bi ; inline
133
134 : exchange-unsafe ( m n seq -- )
135     [ [ nth-unsafe ] curry bi@ ]
136     [ [ set-nth-unsafe ] curry bi@ ] 3bi ; inline
137
138 : (head) ( seq n -- from to seq ) [ 0 ] 2dip swap ; inline
139
140 : (tail) ( seq n -- from to seq ) swap [ length ] keep ; inline
141
142 : from-end ( seq n -- seq n' ) [ dup length ] dip - ; inline
143
144 : (1sequence) ( obj seq -- seq )
145     [ 0 swap set-nth-unsafe ] keep ; inline
146
147 : (2sequence) ( obj1 obj2 seq -- seq )
148     [ 1 swap set-nth-unsafe ] keep (1sequence) ; inline
149
150 : (3sequence) ( obj1 obj2 obj3 seq -- seq )
151     [ 2 swap set-nth-unsafe ] keep (2sequence) ; inline
152
153 : (4sequence) ( obj1 obj2 obj3 obj4 seq -- seq )
154     [ 3 swap set-nth-unsafe ] keep (3sequence) ; inline
155
156 PRIVATE>
157
158 : 1sequence ( obj exemplar -- seq )
159     1 swap [ (1sequence) ] new-like ; inline
160
161 : 2sequence ( obj1 obj2 exemplar -- seq )
162     2 swap [ (2sequence) ] new-like ; inline
163
164 : 3sequence ( obj1 obj2 obj3 exemplar -- seq )
165     3 swap [ (3sequence) ] new-like ; inline
166
167 : 4sequence ( obj1 obj2 obj3 obj4 exemplar -- seq )
168     4 swap [ (4sequence) ] new-like ; inline
169
170 : first2 ( seq -- first second )
171     1 swap bounds-check nip first2-unsafe ; inline
172
173 : first3 ( seq -- first second third )
174     2 swap bounds-check nip first3-unsafe ; inline
175
176 : first4 ( seq -- first second third fourth )
177     3 swap bounds-check nip first4-unsafe ; inline
178
179 : ?nth ( n seq -- elt/f )
180     2dup bounds-check? [ nth-unsafe ] [ 2drop f ] if ; inline
181
182 : ?set-nth ( elt n seq -- )
183     2dup bounds-check? [ set-nth-unsafe ] [ 3drop ] if ; inline
184
185 : ?first ( seq -- elt/f ) 0 swap ?nth ; inline
186 : ?second ( seq -- elt/f ) 1 swap ?nth ; inline
187 : ?last ( seq -- elt/f )
188     [ length 1 - ] keep over 0 <
189     [ 2drop f ] [ nth-unsafe ] if ; inline
190
191 MIXIN: virtual-sequence
192 GENERIC: virtual-exemplar ( seq -- seq' )
193 GENERIC: virtual@ ( n seq -- n' seq' )
194
195 M: virtual-sequence nth virtual@ nth ; inline
196 M: virtual-sequence set-nth virtual@ set-nth ; inline
197 M: virtual-sequence nth-unsafe virtual@ nth-unsafe ; inline
198 M: virtual-sequence set-nth-unsafe virtual@ set-nth-unsafe ; inline
199 M: virtual-sequence like virtual-exemplar like ; inline
200 M: virtual-sequence new-sequence virtual-exemplar new-sequence ; inline
201
202 INSTANCE: virtual-sequence sequence
203
204 ! A reversal of an underlying sequence.
205 TUPLE: reversed { seq read-only } ;
206
207 C: <reversed> reversed
208
209 M: reversed virtual-exemplar seq>> ; inline
210 M: reversed virtual@ seq>> [ length swap - 1 - ] keep ; inline
211 M: reversed length seq>> length ; inline
212
213 INSTANCE: reversed virtual-sequence
214
215 ! A slice of another sequence.
216 TUPLE: slice
217 { from read-only }
218 { to read-only }
219 { seq read-only } ;
220
221 : collapse-slice ( m n slice -- m' n' seq )
222     [ from>> ] [ seq>> ] bi [ [ + ] curry bi@ ] dip ; inline
223
224 ERROR: slice-error from to seq reason ;
225
226 : check-slice-error ( from to seq ? string -- from to seq )
227     [ slice-error ] curry when ; inline
228
229 : check-slice ( from to seq -- from to seq )
230     3dup
231     [ 2drop 0 < "start < 0" check-slice-error ]
232     [ [ drop ] 2dip length > "end > sequence" check-slice-error ]
233     [ drop > "start > end" check-slice-error ]
234     3tri ; inline
235
236 <PRIVATE
237
238 : <slice-unsafe> ( from to seq -- slice )
239     dup slice? [ collapse-slice ] when slice boa ; inline
240
241 PRIVATE>
242
243 : <slice> ( from to seq -- slice )
244     check-slice <slice-unsafe> ; inline
245
246 M: slice virtual-exemplar seq>> ; inline
247
248 M: slice virtual@ [ from>> + ] [ seq>> ] bi ; inline
249
250 M: slice length [ to>> ] [ from>> ] bi - ; inline
251
252 : short ( seq n -- seq n' ) over length min ; inline
253
254 : head-slice ( seq n -- slice ) (head) <slice> ; inline
255
256 : tail-slice ( seq n -- slice ) (tail) <slice> ; inline
257
258 : rest-slice ( seq -- slice ) 1 tail-slice ; inline
259
260 : head-slice* ( seq n -- slice ) from-end head-slice ; inline
261
262 : tail-slice* ( seq n -- slice ) from-end tail-slice ; inline
263
264 : but-last-slice ( seq -- slice ) 1 head-slice* ; inline
265
266 INSTANCE: slice virtual-sequence
267
268 ! One element repeated many times
269 TUPLE: repetition { len read-only } { elt read-only } ;
270
271 : <repetition> ( len elt -- repetition )
272     over 0 < [ non-negative-integer-expected ] when
273     repetition boa ; inline
274
275 M: repetition length len>> ; inline
276 M: repetition nth-unsafe nip elt>> ; inline
277
278 INSTANCE: repetition immutable-sequence
279
280 <PRIVATE
281
282 ERROR: integer-length-expected obj ;
283
284 : check-length ( n -- n )
285     dup integer? [ integer-length-expected ] unless ; inline
286
287 TUPLE: copy-state
288     { src-i read-only }
289     { src read-only }
290     { dst-i read-only }
291     { dst read-only } ;
292
293 C: <copy> copy-state
294
295 : ((copy)) ( n copy -- )
296     [ [ src-i>> + ] [ src>> ] bi nth-unsafe ]
297     [ [ dst-i>> + ] [ dst>> ] bi set-nth-unsafe ] 2bi ; inline
298
299 : (copy) ( n copy -- dst )
300     over 0 <= [ nip dst>> ] [
301         [ 1 - ] dip [ ((copy)) ] [ (copy) ] 2bi
302     ] if ; inline recursive
303
304 : subseq>copy ( from to seq -- n copy )
305     [ over - check-length swap ] dip
306     3dup nip new-sequence 0 swap <copy> ; inline
307
308 : bounds-check-head ( n seq -- n seq )
309     over 0 < [ bounds-error ] when ; inline
310
311 : check-copy ( src n dst -- src n dst )
312     3dup bounds-check-head
313     [ swap length + ] dip lengthen ; inline
314
315 : copy-unsafe ( src i dst -- )
316     #! The check-length call forces partial dispatch
317     [ [ length check-length 0 ] keep ] 2dip <copy> (copy) drop ; inline
318
319 PRIVATE>
320
321 : subseq ( from to seq -- subseq )
322     [ check-slice subseq>copy (copy) ] keep like ;
323
324 : head ( seq n -- headseq ) (head) subseq ;
325
326 : tail ( seq n -- tailseq ) (tail) subseq ;
327
328 : rest ( seq -- tailseq ) 1 tail ;
329
330 : head* ( seq n -- headseq ) from-end head ;
331
332 : tail* ( seq n -- tailseq ) from-end tail ;
333
334 : but-last ( seq -- headseq ) 1 head* ;
335
336 : copy ( src i dst -- ) check-copy copy-unsafe ; inline
337
338 M: sequence clone-like
339     [ dup length ] dip new-sequence [ 0 swap copy-unsafe ] keep ; inline
340
341 M: immutable-sequence clone-like like ; inline
342
343 : push-all ( src dst -- ) [ length ] [ copy ] bi ; inline
344
345 <PRIVATE
346
347 : (append) ( seq1 seq2 accum -- accum )
348     [ [ over length ] dip copy-unsafe ]
349     [ 0 swap copy-unsafe ]
350     [ ] tri ; inline
351
352 PRIVATE>
353
354 : append-as ( seq1 seq2 exemplar -- newseq )
355     [ 2dup [ length ] bi@ + ] dip
356     [ (append) ] new-like ; inline
357
358 : 3append-as ( seq1 seq2 seq3 exemplar -- newseq )
359     [ 3dup [ length ] tri@ + + ] dip [
360         [ [ 2over [ length ] bi@ + ] dip copy-unsafe ]
361         [ (append) ] bi
362     ] new-like ; inline
363
364 : append ( seq1 seq2 -- newseq ) over append-as ;
365
366 : prepend-as ( seq1 seq2 exemplar -- newseq ) swapd append-as ; inline
367
368 : prepend ( seq1 seq2 -- newseq ) over prepend-as ;
369
370 : 3append ( seq1 seq2 seq3 -- newseq ) pick 3append-as ;
371
372 : surround ( seq1 seq2 seq3 -- newseq ) swapd 3append ; inline
373
374 : glue ( seq1 seq2 seq3 -- newseq ) swap 3append ; inline
375
376 : change-nth ( ..a i seq quot: ( ..a elt -- ..b newelt ) -- ..b )
377     [ [ nth ] dip call ] 3keep drop set-nth-unsafe ; inline
378
379 : min-length ( seq1 seq2 -- n ) [ length ] bi@ min ; inline
380
381 : max-length ( seq1 seq2 -- n ) [ length ] bi@ max ; inline
382
383 <PRIVATE
384
385 : ((each)) ( seq -- n quot )
386     [ length ] keep [ nth-unsafe ] curry ; inline
387
388 : (each) ( seq quot -- n quot' )
389     [ ((each)) ] dip compose ; inline
390
391 : (each-index) ( seq quot -- n quot' )
392     [ ((each)) [ keep ] curry ] dip compose ; inline
393
394 : (collect) ( quot into -- quot' )
395     [ [ keep ] dip set-nth-unsafe ] 2curry ; inline
396
397 : collect ( n quot into -- )
398     (collect) each-integer ; inline
399
400 : map-into ( seq quot into -- )
401     [ (each) ] dip collect ; inline
402
403 : 2nth-unsafe ( n seq1 seq2 -- elt1 elt2 )
404     [ nth-unsafe ] bi-curry@ bi ; inline
405
406 : ((2each)) ( seq1 seq2 -- n quot )
407     [ min-length ] 2keep [ 2nth-unsafe ] 2curry ; inline
408
409 : (2each) ( seq1 seq2 quot -- n quot' )
410     [ ((2each)) ] dip compose ; inline
411
412 : 3nth-unsafe ( n seq1 seq2 seq3 -- elt1 elt2 elt3 )
413     [ nth-unsafe ] tri-curry@ tri ; inline
414
415 : (3each) ( seq1 seq2 seq3 quot -- n quot' )
416     [
417         [ [ length ] tri@ min min ]
418         [ [ 3nth-unsafe ] 3curry ] 3bi
419     ] dip compose ; inline
420
421 : finish-find ( i seq -- i elt )
422     over [ dupd nth-unsafe ] [ drop f ] if ; inline
423
424 : (find) ( seq quot quot' -- i elt )
425     pick [ [ (each) ] dip call ] dip finish-find ; inline
426
427 : (find-from) ( n seq quot quot' -- i elt )
428     [ 2dup bounds-check? ] 2dip
429     [ (find) ] 2curry
430     [ 2drop f f ]
431     if ; inline
432
433 : (find-index) ( seq quot quot' -- i elt )
434     pick [ [ (each-index) ] dip call ] dip finish-find ; inline
435
436 : (find-index-from) ( n seq quot quot' -- i elt )
437     [ 2dup bounds-check? ] 2dip
438     [ (find-index) ] 2curry
439     [ 2drop f f ]
440     if ; inline
441
442 : (accumulate) ( seq identity quot -- identity seq quot )
443     swapd [ curry keep ] curry ; inline
444
445 PRIVATE>
446
447 : each ( ... seq quot: ( ... x -- ... ) -- ... )
448     (each) each-integer ; inline
449
450 : reduce ( ... seq identity quot: ( ... prev elt -- ... next ) -- ... result )
451     swapd each ; inline
452
453 : map-integers ( ... len quot: ( ... i -- ... elt ) exemplar -- ... newseq )
454     [ over ] dip [ [ collect ] keep ] new-like ; inline
455
456 : map-as ( ... seq quot: ( ... elt -- ... newelt ) exemplar -- ... newseq )
457     [ (each) ] dip map-integers ; inline
458
459 : map ( ... seq quot: ( ... elt -- ... newelt ) -- ... newseq )
460     over map-as ; inline
461
462 : replicate-as ( ... len quot: ( ... -- ... newelt ) exemplar -- ... newseq )
463     [ [ drop ] prepose ] dip map-integers ; inline
464
465 : replicate ( ... len quot: ( ... -- ... newelt ) -- ... newseq )
466     { } replicate-as ; inline
467
468 : map! ( ... seq quot: ( ... elt -- ... newelt ) -- ... seq )
469     over [ map-into ] keep ; inline
470
471 : accumulate-as ( ... seq identity quot: ( ... prev elt -- ... next ) exemplar -- ... final newseq )
472     [ (accumulate) ] dip map-as ; inline
473
474 : accumulate ( ... seq identity quot: ( ... prev elt -- ... next ) -- ... final newseq )
475     pick accumulate-as ; inline
476
477 : accumulate! ( ... seq identity quot: ( ... prev elt -- ... next ) -- ... final seq )
478     (accumulate) map! ; inline
479
480 : 2each ( ... seq1 seq2 quot: ( ... elt1 elt2 -- ... ) -- ... )
481     (2each) each-integer ; inline
482
483 : 2reduce ( ... seq1 seq2 identity quot: ( ... prev elt1 elt2 -- ... next ) -- ... result )
484     [ -rot ] dip 2each ; inline
485
486 : 2map-as ( ... seq1 seq2 quot: ( ... elt1 elt2 -- ... newelt ) exemplar -- ... newseq )
487     [ (2each) ] dip map-integers ; inline
488
489 : 2map ( ... seq1 seq2 quot: ( ... elt1 elt2 -- ... newelt ) -- ... newseq )
490     pick 2map-as ; inline
491
492 : 2all? ( ... seq1 seq2 quot: ( ... elt1 elt2 -- ... ? ) -- ... ? )
493     (2each) all-integers? ; inline
494
495 : 3each ( ... seq1 seq2 seq3 quot: ( ... elt1 elt2 elt3 -- ... ) -- ... )
496     (3each) each-integer ; inline
497
498 : 3map-as ( ... seq1 seq2 seq3 quot: ( ... elt1 elt2 elt3 -- ... newelt ) exemplar -- ... newseq )
499     [ (3each) ] dip map-integers ; inline
500
501 : 3map ( ... seq1 seq2 seq3 quot: ( ... elt1 elt2 elt3 -- ... newelt ) -- ... newseq )
502     [ pick ] dip swap 3map-as ; inline
503
504 : find-from ( ... n seq quot: ( ... elt -- ... ? ) -- ... i elt )
505     [ (find-integer) ] (find-from) ; inline
506
507 : find ( ... seq quot: ( ... elt -- ... ? ) -- ... i elt )
508     [ find-integer ] (find) ; inline
509
510 : find-last-from ( ... n seq quot: ( ... elt -- ... ? ) -- ... i elt )
511     [ nip find-last-integer ] (find-from) ; inline
512
513 : find-last ( ... seq quot: ( ... elt -- ... ? ) -- ... i elt )
514     [ [ 1 - ] dip find-last-integer ] (find) ; inline
515
516 : find-index-from ( ... n seq quot: ( ... elt i -- ... ? ) -- ... i elt )
517     [ (find-integer) ] (find-index-from) ; inline
518
519 : find-index ( ... seq quot: ( ... elt i -- ... ? ) -- ... i elt )
520     [ find-integer ] (find-index) ; inline
521
522 : all? ( ... seq quot: ( ... elt -- ... ? ) -- ... ? )
523     (each) all-integers? ; inline
524
525 : push-if ( ..a elt quot: ( ..a elt -- ..b ? ) accum -- ..b )
526     [ keep ] dip rot [ push ] [ 2drop ] if ; inline
527
528 <PRIVATE
529
530 : (selector-for) ( quot length exemplar -- selector accum )
531     new-resizable [ [ push-if ] 2curry ] keep ; inline
532
533 PRIVATE>
534
535 : selector-for ( quot exemplar -- selector accum )
536     [ length ] keep (selector-for) ; inline
537
538 : selector ( quot -- selector accum )
539     V{ } selector-for ; inline
540
541 : filter-as ( ... seq quot: ( ... elt -- ... ? ) exemplar -- ... subseq )
542     pick length over [ (selector-for) [ each ] dip ] 2curry dip like ; inline
543
544 : filter ( ... seq quot: ( ... elt -- ... ? ) -- ... subseq )
545     over filter-as ; inline
546
547 : push-either ( ..a elt quot: ( ..a elt -- ..b ? ) accum1 accum2 -- ..b )
548     [ keep swap ] 2dip ? push ; inline
549
550 : 2selector ( quot -- selector accum1 accum2 )
551     V{ } clone V{ } clone [ [ push-either ] 3curry ] 2keep ; inline
552
553 : partition ( ... seq quot: ( ... elt -- ... ? ) -- ... trueseq falseseq )
554     over [ 2selector [ each ] 2dip ] dip [ like ] curry bi@ ; inline
555
556 : collector-for ( quot exemplar -- quot' vec )
557     [ length ] keep new-resizable [ [ push ] curry compose ] keep ; inline
558
559 : collector ( quot -- quot' vec )
560     V{ } collector-for ; inline
561
562 : produce-as ( ..a pred: ( ..a -- ..b ? ) quot: ( ..b -- ..a obj ) exemplar -- ..b seq )
563     dup [ collector-for [ while ] dip ] curry dip like ; inline
564
565 : produce ( ..a pred: ( ..a -- ..b ? ) quot: ( ..b -- ..a obj ) -- ..b seq )
566     { } produce-as ; inline
567
568 : follow ( ... obj quot: ( ... prev -- ... result/f ) -- ... seq )
569     [ dup ] swap [ keep ] curry produce nip ; inline
570
571 : each-index ( ... seq quot: ( ... elt index -- ... ) -- ... )
572     (each-index) each-integer ; inline
573
574 : interleave ( ... seq between quot: ( ... elt -- ... ) -- ... )
575     pick empty? [ 3drop ] [
576         [ [ drop first-unsafe ] dip call ]
577         [ [ bi* ] 2curry [ 1 ] 2dip (each) (each-integer) ]
578         3bi
579     ] if ; inline
580
581 : map-index-as ( ... seq quot: ( ... elt index -- ... newelt ) exemplar -- ... newseq )
582     [ dup length iota ] 2dip 2map-as ; inline
583
584 : map-index ( ... seq quot: ( ... elt index -- ... newelt ) -- ... newseq )
585     { } map-index-as ; inline
586
587 : reduce-index ( ... seq identity quot: ( ... prev elt index -- ... next ) -- ... result )
588     swapd each-index ; inline
589
590 : index ( obj seq -- n )
591     [ = ] with find drop ;
592
593 : index-from ( obj i seq -- n )
594     rot [ = ] curry find-from drop ;
595
596 : last-index ( obj seq -- n )
597     [ = ] with find-last drop ;
598
599 : last-index-from ( obj i seq -- n )
600     rot [ = ] curry find-last-from drop ;
601
602 <PRIVATE
603
604 : (indices) ( elt i obj accum -- )
605     [ swap [ = ] dip ] dip [ push ] 2curry when ; inline
606
607 PRIVATE>
608
609 : indices ( obj seq -- indices )
610     swap V{ } clone
611     [ [ (indices) ] 2curry each-index ] keep ;
612
613 <PRIVATE
614
615 : nths-unsafe ( indices seq -- seq' )
616     [ [ nth-unsafe ] curry ] keep map-as ;
617
618 PRIVATE>
619
620 : nths ( indices seq -- seq' )
621     [ [ nth ] curry ] keep map-as ;
622
623 : any? ( ... seq quot: ( ... elt -- ... ? ) -- ... ? )
624     find drop >boolean ; inline
625
626 : member? ( elt seq -- ? )
627     [ = ] with any? ;
628
629 : member-eq? ( elt seq -- ? )
630     [ eq? ] with any? ;
631
632 : remove ( elt seq -- newseq )
633     [ = not ] with filter ;
634
635 : remove-eq ( elt seq -- newseq )
636     [ eq? not ] with filter ;
637
638 : sift ( seq -- newseq )
639     [ ] filter ;
640
641 : harvest ( seq -- newseq )
642     [ empty? not ] filter ;
643
644 <PRIVATE
645
646 : mismatch-unsafe ( n seq1 seq2 -- i )
647     [ 2nth-unsafe = not ] 2curry find-integer ; inline
648
649 PRIVATE>
650
651 : mismatch ( seq1 seq2 -- i )
652     [ min-length ] 2keep mismatch-unsafe ; inline
653
654 M: sequence <=>
655     [ mismatch ] 2keep pick
656     [ 2nth-unsafe <=> ] [ [ length ] compare nip ] if ;
657
658 : sequence= ( seq1 seq2 -- ? )
659     2dup [ length ] bi@ dupd =
660     [ -rot mismatch-unsafe not ] [ 3drop f ] if ; inline
661
662 ERROR: assert-sequence got expected ;
663
664 : assert-sequence= ( a b -- )
665     2dup sequence= [ 2drop ] [ assert-sequence ] if ;
666
667 <PRIVATE
668
669 : sequence-hashcode-step ( oldhash newpart -- newhash )
670     integer>fixnum swap [
671         [ -2 fixnum-shift-fast ] [ 5 fixnum-shift-fast ] bi
672         fixnum+fast fixnum+fast
673     ] keep fixnum-bitxor ; inline
674
675 PRIVATE>
676
677 : sequence-hashcode ( n seq -- x )
678     [ 0 ] 2dip [ hashcode* sequence-hashcode-step ] with each ; inline
679
680 M: reversed equal? over reversed? [ sequence= ] [ 2drop f ] if ;
681
682 M: slice equal? over slice? [ sequence= ] [ 2drop f ] if ;
683
684 : move ( to from seq -- )
685     2over =
686     [ 3drop ] [ [ nth swap ] [ set-nth ] bi ] if ; inline
687
688 <PRIVATE
689
690 : move-unsafe ( to from seq -- )
691     2over =
692     [ 3drop ] [ [ nth-unsafe swap ] [ set-nth-unsafe ] bi ] if ; inline
693
694 : (filter!) ( ... quot: ( ... elt -- ... ? ) store scan seq -- ... )
695     2dup length < [
696         [ move-unsafe ] 3keep
697         [ nth-unsafe pick call [ 1 + ] when ] 2keep
698         [ 1 + ] dip
699         (filter!)
700     ] [ nip set-length drop ] if ; inline recursive
701
702 PRIVATE>
703
704 : filter! ( ... seq quot: ( ... elt -- ... ? ) -- ... seq )
705     swap [ [ 0 0 ] dip (filter!) ] keep ; inline
706
707 : remove! ( elt seq -- seq )
708     [ = not ] with filter! ;
709
710 : remove-eq! ( elt seq -- seq )
711     [ eq? not ] with filter! ;
712
713 : prefix ( seq elt -- newseq )
714     over [ over length 1 + ] dip [
715         (1sequence) [ 1 swap copy-unsafe ] keep
716     ] new-like ;
717
718 : suffix ( seq elt -- newseq )
719     over [ over length 1 + ] dip [
720         [ [ over length ] dip set-nth-unsafe ] keep
721         [ 0 swap copy-unsafe ] keep
722     ] new-like ;
723
724 : suffix! ( seq elt -- seq ) over push ; inline
725
726 : append! ( seq1 seq2 -- seq1 ) over push-all ; inline
727
728 : last ( seq -- elt )
729     [ length 1 - ] keep
730     over 0 < [ bounds-error ] [ nth-unsafe ] if ; inline
731
732 <PRIVATE
733
734 : last-unsafe ( seq -- elt )
735     [ length 1 - ] [ nth-unsafe ] bi ; inline
736
737 PRIVATE>
738
739 : set-last ( elt seq -- )
740     [ length 1 - ] keep
741     over 0 < [ bounds-error ] [ set-nth-unsafe ] if ; inline
742
743 : pop* ( seq -- ) [ length 1 - ] [ shorten ] bi ;
744
745 <PRIVATE
746
747 : move-backward ( shift from to seq -- )
748     2over = [
749         4drop
750     ] [
751         [ [ 2over + pick ] dip move-unsafe [ 1 + ] dip ] keep
752         move-backward
753     ] if ;
754
755 : move-forward ( shift from to seq -- )
756     2over = [
757         4drop
758     ] [
759         [ [ pick [ dup dup ] dip + swap ] dip move-unsafe 1 - ] keep
760         move-forward
761     ] if ;
762
763 : (open-slice) ( shift from to seq ? -- )
764     [
765         [ [ 1 - ] bi@ ] dip move-forward
766     ] [
767         [ over - ] 2dip move-backward
768     ] if ;
769
770 : open-slice ( shift from seq -- )
771     pick 0 = [
772         3drop
773     ] [
774         pick over length + over
775         [ pick 0 > [ [ length ] keep ] dip (open-slice) ] 2dip
776         set-length
777     ] if ;
778
779 PRIVATE>
780
781 : delete-slice ( from to seq -- )
782     check-slice [ over [ - ] dip ] dip open-slice ;
783
784 : remove-nth! ( n seq -- seq )
785     [ [ dup 1 + ] dip delete-slice ] keep ;
786
787 : snip ( from to seq -- head tail )
788     [ swap head ] [ swap tail ] bi-curry bi* ; inline
789
790 : snip-slice ( from to seq -- head tail )
791     [ swap head-slice ] [ swap tail-slice ] bi-curry bi* ; inline
792
793 : replace-slice ( new from to seq -- seq' )
794     snip-slice surround ;
795
796 : remove-nth ( n seq -- seq' )
797     [ [ dup 1 + ] dip snip-slice ] keep append-as ;
798
799 : pop ( seq -- elt )
800     [ length 1 - ] keep over 0 >=
801     [ [ nth-unsafe ] [ shorten ] 2bi ]
802     [ bounds-error ] if ;
803
804 : exchange ( m n seq -- )
805     [ nip bounds-check 2drop ]
806     [ bounds-check 3drop ]
807     [ exchange-unsafe ]
808     3tri ;
809
810 : midpoint@ ( seq -- n ) length 2/ ; inline
811
812 : reverse! ( seq -- seq )
813     [
814         [ midpoint@ ] [ length ] [ ] tri
815         [ [ over - 1 - ] dip exchange-unsafe ] 2curry
816         each-integer
817     ] keep ;
818
819 : reverse ( seq -- newseq )
820      [
821         dup [ length ] keep new-sequence
822         [ 0 swap copy-unsafe ] keep reverse!
823     ] keep like ;
824
825 GENERIC: sum-lengths ( seq -- n )
826
827 M: object sum-lengths
828     0 [ length + ] reduce ;
829
830 M: repetition sum-lengths
831     [ len>> ] [ elt>> length ] bi * ;
832
833 : concat-as ( seq exemplar -- newseq )
834     swap [ { } ] [
835         [ sum-lengths over new-resizable ] keep
836         [ append! ] each
837     ] if-empty swap like ;
838
839 : concat ( seq -- newseq )
840     [ { } ] [ dup first concat-as ] if-empty ;
841
842 <PRIVATE
843
844 : joined-length ( seq glue -- n )
845     [ [ sum-lengths ] [ length 1 [-] ] bi ] dip length * + ;
846
847 PRIVATE>
848
849 : join-as ( seq glue exemplar -- newseq )
850     over empty? [ nip concat-as ] [
851         [
852             2dup joined-length over new-resizable [
853                 [ [ push-all ] 2curry ]
854                 [ nip [ push-all ] curry ] 2bi
855                 interleave
856             ] keep
857         ] dip like
858     ] if ;
859
860 : join ( seq glue -- newseq )
861     dup join-as ; inline
862
863 : padding ( ... seq n elt quot: ( ... seq1 seq2 -- ... newseq ) -- ... newseq )
864     [
865         [ over length [-] dup 0 = [ drop ] ] dip
866         [ <repetition> ] curry
867     ] dip compose if ; inline
868
869 : pad-head ( seq n elt -- padded )
870     [ swap dup append-as ] padding ;
871
872 : pad-tail ( seq n elt -- padded )
873     [ append ] padding ;
874
875 : shorter? ( seq1 seq2 -- ? ) [ length ] bi@ < ; inline
876 : longer? ( seq1 seq2 -- ? ) [ length ] bi@ > ; inline
877 : shorter ( seq1 seq2 -- seq ) [ [ length ] bi@ <= ] 2keep ? ; inline
878 : longer ( seq1 seq2 -- seq ) [ [ length ] bi@ >= ] 2keep ? ; inline
879
880 : head? ( seq begin -- ? )
881     2dup shorter? [
882         2drop f
883     ] [
884         [ length [ head-slice ] keep swap ] keep
885         mismatch-unsafe not
886     ] if ;
887
888 : tail? ( seq end -- ? )
889     2dup shorter? [
890         2drop f
891     ] [
892         [ length [ tail-slice* ] keep swap ] keep
893         mismatch-unsafe not
894     ] if ;
895
896 : cut-slice ( seq n -- before-slice after-slice )
897     [ head-slice ] [ tail-slice ] 2bi ; inline
898
899 : insert-nth ( elt n seq -- seq' )
900     swap cut-slice [ swap suffix ] dip append ;
901
902 : halves ( seq -- first-slice second-slice )
903     dup midpoint@ cut-slice ; inline
904
905 <PRIVATE
906
907 : nth2-unsafe ( n seq -- a b )
908     [ nth-unsafe ] [ [ 1 + ] dip nth-unsafe ] 2bi ; inline
909
910 : nth3-unsafe ( n seq -- a b c )
911     [ nth2-unsafe ] [ [ 2 + ] dip nth-unsafe ] 2bi ; inline
912
913 : (binary-reduce) ( seq start quot: ( elt1 elt2 -- newelt ) from length -- value )
914     #! We can't use case here since combinators depends on
915     #! sequences
916     dup 4 < [
917         integer>fixnum {
918             [ 2drop nip ]
919             [ 2nip swap nth-unsafe ]
920             [ -rot [ drop swap nth2-unsafe ] dip call ]
921             [ -rot [ drop swap nth3-unsafe ] dip bi@ ]
922         } dispatch
923     ] [
924         [ 2/ ] [ over - ] bi [ 2dup + ] dip
925         [ (binary-reduce) ] [ 2curry ] curry 2bi@
926         pick [ 3bi ] dip call
927     ] if ; inline recursive
928
929 PRIVATE>
930
931 : binary-reduce ( seq start quot: ( elt1 elt2 -- newelt ) -- value )
932     pick length 0 max 0 swap (binary-reduce) ; inline
933
934 : cut ( seq n -- before after )
935     [ head ] [ tail ] 2bi ;
936
937 : cut* ( seq n -- before after )
938     [ head* ] [ tail* ] 2bi ;
939
940 <PRIVATE
941
942 : (start) ( subseq seq n length -- subseq seq ? )
943     [
944         [ 3dup ] dip [ + swap nth-unsafe ] keep rot nth-unsafe =
945     ] all-integers? nip ; inline
946
947 PRIVATE>
948
949 : start* ( subseq seq n -- i )
950     pick length [ pick length swap - 1 + ] keep
951     [ (start) ] curry (find-integer) 2nip ;
952
953 : start ( subseq seq -- i ) 0 start* ; inline
954
955 : subseq? ( subseq seq -- ? ) start >boolean ;
956
957 : drop-prefix ( seq1 seq2 -- slice1 slice2 )
958     2dup mismatch [ 2dup min-length ] unless*
959     [ tail-slice ] curry bi@ ;
960
961 : unclip ( seq -- rest first )
962     [ rest ] [ first-unsafe ] bi ;
963
964 : unclip-last ( seq -- butlast last )
965     [ but-last ] [ last-unsafe ] bi ;
966
967 : unclip-slice ( seq -- rest-slice first )
968     [ rest-slice ] [ first-unsafe ] bi ; inline
969
970 : map-reduce ( ..a seq map-quot: ( ..a elt -- ..b intermediate ) reduce-quot: ( ..b prev intermediate -- ..a next ) -- ..a result )
971     [ [ dup first ] dip [ call ] keep ] dip compose
972     swapd [ 1 ] 2dip (each) (each-integer) ; inline
973
974 : 2map-reduce ( ..a seq1 seq2 map-quot: ( ..a elt1 elt2 -- ..b intermediate ) reduce-quot: ( ..b prev intermediate -- ..a next ) -- ..a result )
975     [ [ 2dup [ first ] bi@ ] dip [ call ] keep ] dip compose
976     [ -rot ] dip [ 1 ] 3dip (2each) (each-integer) ; inline
977
978 <PRIVATE
979
980 : (map-find) ( seq quot find-quot -- result elt )
981     [ [ f ] 2dip [ [ nip ] dip call dup ] curry ] dip call
982     [ [ drop f ] unless ] dip ; inline
983
984 PRIVATE>
985
986 : map-find ( ... seq quot: ( ... elt -- ... result/f ) -- ... result elt )
987     [ find ] (map-find) ; inline
988
989 : map-find-last ( ... seq quot: ( ... elt -- ... result/f ) -- ... result elt )
990     [ find-last ] (map-find) ; inline
991
992 : unclip-last-slice ( seq -- butlast-slice last )
993     [ but-last-slice ] [ last-unsafe ] bi ; inline
994
995 <PRIVATE
996
997 : (trim-head) ( seq quot -- seq n )
998     over [ [ not ] compose find drop ] dip swap
999     [ dup length ] unless* ; inline
1000
1001 : (trim-tail) ( seq quot -- seq n )
1002     over [ [ not ] compose find-last drop ?1+ ] dip
1003     swap ; inline
1004
1005 PRIVATE>
1006
1007 : trim-head-slice ( ... seq quot: ( ... elt -- ... ? ) -- ... slice )
1008     (trim-head) tail-slice ; inline
1009
1010 : trim-head ( ... seq quot: ( ... elt -- ... ? ) -- ... newseq )
1011     (trim-head) tail ; inline
1012
1013 : trim-tail-slice ( ... seq quot: ( ... elt -- ... ? ) -- ... slice )
1014     (trim-tail) head-slice ; inline
1015
1016 : trim-tail ( ... seq quot: ( ... elt -- ... ? ) -- ... newseq )
1017     (trim-tail) head ; inline
1018
1019 : trim-slice ( ... seq quot: ( ... elt -- ... ? ) -- ... slice )
1020     [ trim-head-slice ] [ trim-tail-slice ] bi ; inline
1021
1022 : trim ( ... seq quot: ( ... elt -- ... ? ) -- ... newseq )
1023     [ trim-slice ] [ drop ] 2bi like ; inline
1024
1025 GENERIC: sum ( seq -- n )
1026 M: object sum 0 [ + ] binary-reduce ; inline
1027 M: iota-tuple sum length dup 1 - * 2/ ; inline
1028 M: repetition sum [ elt>> ] [ len>> ] bi * ; inline
1029
1030 : product ( seq -- n ) 1 [ * ] binary-reduce ;
1031
1032 : infimum ( seq -- n ) [ ] [ min ] map-reduce ;
1033
1034 : supremum ( seq -- n ) [ ] [ max ] map-reduce ;
1035
1036 : map-sum ( ... seq quot: ( ... elt -- ... n ) -- ... n )
1037     [ 0 ] 2dip [ dip + ] curry [ swap ] prepose each ; inline
1038
1039 : count ( ... seq quot: ( ... elt -- ... ? ) -- ... n )
1040     [ 1 0 ? ] compose map-sum ; inline
1041
1042 : cartesian-each ( ... seq1 seq2 quot: ( ... elt1 elt2 -- ... ) -- ... )
1043     [ with each ] 2curry each ; inline
1044
1045 : cartesian-map ( ... seq1 seq2 quot: ( ... elt1 elt2 -- ... newelt ) -- ... newseq )
1046     [ with map ] 2curry map ; inline
1047
1048 : cartesian-product ( seq1 seq2 -- newseq )
1049     [ { } 2sequence ] cartesian-map ;
1050
1051 : each-from ( ... seq quot: ( ... x -- ... ) i -- ... )
1052     -rot (each) (each-integer) ; inline
1053
1054 <PRIVATE
1055
1056 : select-by ( ... seq quot: ( ... elt -- ... x ) compare: ( obj1 obj2 -- ? ) -- ... elt )
1057     [
1058         [ keep swap ] curry [ [ first ] dip call ] 2keep
1059         [ curry 2dip pick over ] curry
1060     ] [
1061         [ [ 2drop ] [ [ 2drop ] 2dip ] if ] compose
1062     ] bi* compose 1 each-from drop ; inline
1063
1064 PRIVATE>
1065
1066 : supremum-by ( ... seq quot: ( ... elt -- ... x ) -- ... elt )
1067     [ after? ] select-by ; inline
1068
1069 : infimum-by ( ... seq quot: ( ... elt -- ... x ) -- ... elt )
1070     [ before? ] select-by ; inline
1071
1072 : shortest ( seqs -- elt ) [ length ] infimum-by ;
1073
1074 : longest ( seqs -- elt ) [ length ] supremum-by ;
1075
1076 ! We hand-optimize flip to such a degree because type hints
1077 ! cannot express that an array is an array of arrays yet, and
1078 ! this word happens to be performance-critical since the compiler
1079 ! itself uses it. Optimizing it like this reduced compile time.
1080 <PRIVATE
1081
1082 : generic-flip ( matrix -- newmatrix )
1083     [
1084         [ first-unsafe length 1 ] keep
1085         [ length min ] (each) (each-integer) iota
1086     ] keep
1087     [ [ nth-unsafe ] with { } map-as ] curry { } map-as ; inline
1088
1089 USE: arrays
1090
1091 : array-length ( array -- len )
1092     { array } declare length>> ; inline
1093
1094 : array-flip ( matrix -- newmatrix )
1095     { array } declare
1096     [
1097         [ first-unsafe array-length 1 ] keep
1098         [ array-length min ] (each) (each-integer) iota
1099     ] keep
1100     [ [ { array } declare array-nth ] with { } map-as ] curry { } map-as ;
1101
1102 PRIVATE>
1103
1104 : flip ( matrix -- newmatrix )
1105     dup empty? [
1106         dup array? [
1107             dup [ array? ] all?
1108             [ array-flip ] [ generic-flip ] if
1109         ] [ generic-flip ] if
1110     ] unless ;