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