]> gitweb.factorcode.org Git - factor.git/blob - core/sequences/sequences.factor
Factor source files should not be executable
[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-exemplar ( 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-exemplar like ; inline
193 M: virtual-sequence new-sequence virtual-exemplar 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-exemplar 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-exemplar 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     dup integer? [ integer-length-expected ] unless ; inline
272
273 TUPLE: copy-state
274     { src-i read-only }
275     { src read-only }
276     { dst-i read-only }
277     { dst read-only } ;
278
279 C: <copy> copy-state
280
281 : ((copy)) ( n copy -- )
282     [ [ src-i>> + ] [ src>> ] bi nth-unsafe ]
283     [ [ dst-i>> + ] [ dst>> ] bi set-nth-unsafe ] 2bi ; inline
284
285 : (copy) ( n copy -- dst )
286     over 0 <= [ nip dst>> ] [ [ 1 - ] dip [ ((copy)) ] [ (copy) ] 2bi ] if ;
287     inline recursive
288
289 : subseq>copy ( from to seq -- n copy )
290     [ over - check-length swap ] dip
291     3dup nip new-sequence 0 swap <copy> ; inline
292
293 : check-copy ( src n dst -- src n dst )
294     3dup over 0 < [ bounds-error ] when
295     [ swap length + ] dip lengthen ; inline
296
297 PRIVATE>
298
299 : subseq ( from to seq -- subseq )
300     [ check-slice subseq>copy (copy) ] keep like ;
301
302 : head ( seq n -- headseq ) (head) subseq ;
303
304 : tail ( seq n -- tailseq ) (tail) subseq ;
305
306 : rest ( seq -- tailseq ) 1 tail ;
307
308 : head* ( seq n -- headseq ) from-end head ;
309
310 : tail* ( seq n -- tailseq ) from-end tail ;
311
312 : but-last ( seq -- headseq ) 1 head* ;
313
314 : copy ( src i dst -- )
315     #! The check-length call forces partial dispatch
316     [ [ length check-length 0 ] keep ] 2dip
317     check-copy <copy> (copy) drop ; inline
318
319 M: sequence clone-like
320     [ dup length ] dip new-sequence [ 0 swap copy ] keep ; inline
321
322 M: immutable-sequence clone-like like ; inline
323
324 : push-all ( src dest -- ) [ length ] [ copy ] bi ;
325
326 <PRIVATE
327
328 : (append) ( seq1 seq2 accum -- accum )
329     [ [ over length ] dip copy ]
330     [ 0 swap copy ]
331     [ ] tri ; inline
332
333 PRIVATE>
334
335 : append-as ( seq1 seq2 exemplar -- newseq )
336     [ over length over length + ] dip
337     [ (append) ] new-like ; inline
338
339 : 3append-as ( seq1 seq2 seq3 exemplar -- newseq )
340     [ 3dup [ length ] tri@ + + ] dip [
341         [ [ 2over [ length ] bi@ + ] dip copy ]
342         [ (append) ] bi
343     ] new-like ; inline
344
345 : append ( seq1 seq2 -- newseq ) over append-as ;
346
347 : prepend ( seq1 seq2 -- newseq ) swap append ; inline
348
349 : 3append ( seq1 seq2 seq3 -- newseq ) pick 3append-as ;
350
351 : surround ( seq1 seq2 seq3 -- newseq ) swapd 3append ; inline
352
353 : glue ( seq1 seq2 seq3 -- newseq ) swap 3append ; inline
354
355 : change-nth ( i seq quot -- )
356     [ [ nth ] dip call ] 3keep drop set-nth ; inline
357
358 : min-length ( seq1 seq2 -- n ) [ length ] bi@ min ; inline
359
360 : max-length ( seq1 seq2 -- n ) [ length ] bi@ max ; inline
361
362 <PRIVATE
363
364 : ((each)) ( seq -- n quot )
365     [ length ] keep [ nth-unsafe ] curry ; inline
366
367 : (each) ( seq quot -- n quot' )
368     [ ((each)) ] dip compose ; inline
369
370 : (each-index) ( seq quot -- n quot' )
371     [ ((each)) [ keep ] curry ] dip compose ; inline
372
373 : (collect) ( quot into -- quot' )
374     [ [ keep ] dip set-nth-unsafe ] 2curry ; inline
375
376 : collect ( n quot into -- )
377     (collect) each-integer ; inline
378
379 : map-into ( seq quot into -- )
380     [ (each) ] dip collect ; inline
381
382 : 2nth-unsafe ( n seq1 seq2 -- elt1 elt2 )
383     [ nth-unsafe ] bi-curry@ bi ; inline
384
385 : (2each) ( seq1 seq2 quot -- n quot' )
386     [
387         [ min-length ] 2keep
388         [ 2nth-unsafe ] 2curry
389     ] dip compose ; inline
390
391 : 3nth-unsafe ( n seq1 seq2 seq3 -- elt1 elt2 elt3 )
392     [ nth-unsafe ] tri-curry@ tri ; inline
393
394 : (3each) ( seq1 seq2 seq3 quot -- n quot' )
395     [
396         [ [ length ] tri@ min min ]
397         [ [ 3nth-unsafe ] 3curry ] 3bi
398     ] dip compose ; inline
399
400 : finish-find ( i seq -- i elt )
401     over [ dupd nth-unsafe ] [ drop f ] if ; inline
402
403 : (find) ( seq quot quot' -- i elt )
404     pick [ [ (each) ] dip call ] dip finish-find ; inline
405
406 : (find-from) ( n seq quot quot' -- i elt )
407     [ 2dup bounds-check? ] 2dip
408     [ (find) ] 2curry
409     [ 2drop f f ]
410     if ; inline
411
412 PRIVATE>
413
414 : each ( seq quot -- )
415     (each) each-integer ; inline
416
417 : reduce ( seq identity quot -- result )
418     swapd each ; inline
419
420 : map-integers ( len quot exemplar -- newseq )
421     [ over ] dip [ [ collect ] keep ] new-like ; inline
422
423 : map-as ( seq quot exemplar -- newseq )
424     [ (each) ] dip map-integers ; inline
425
426 : map ( seq quot -- newseq )
427     over map-as ; inline
428
429 : replicate ( seq quot -- newseq )
430     [ drop ] prepose map ; inline
431
432 : replicate-as ( seq quot exemplar -- newseq )
433     [ [ drop ] prepose ] dip map-as ; inline
434
435 : map! ( seq quot -- seq )
436     over [ map-into ] keep ; inline
437
438 : (accumulate) ( seq identity quot -- seq identity quot )
439     [ swap ] dip [ curry keep ] curry ; inline
440
441 : accumulate-as ( seq identity quot exemplar -- final newseq )
442     [ (accumulate) ] dip map-as ; inline
443
444 : accumulate ( seq identity quot -- final newseq )
445     { } accumulate-as ; inline
446
447 : accumulate! ( seq identity quot -- final seq )
448     (accumulate) map! ; inline
449
450 : 2each ( seq1 seq2 quot -- )
451     (2each) each-integer ; inline
452
453 : 2reverse-each ( seq1 seq2 quot -- )
454     [ [ <reversed> ] bi@ ] dip 2each ; inline
455
456 : 2reduce ( seq1 seq2 identity quot -- result )
457     [ -rot ] dip 2each ; inline
458
459 : 2map-as ( seq1 seq2 quot exemplar -- newseq )
460     [ (2each) ] dip map-integers ; inline
461
462 : 2map ( seq1 seq2 quot -- newseq )
463     pick 2map-as ; inline
464
465 : 2all? ( seq1 seq2 quot -- ? )
466     (2each) all-integers? ; inline
467
468 : 3each ( seq1 seq2 seq3 quot -- )
469     (3each) each ; inline
470
471 : 3map-as ( seq1 seq2 seq3 quot exemplar -- newseq )
472     [ (3each) ] dip map-integers ; inline
473
474 : 3map ( seq1 seq2 seq3 quot -- newseq )
475     [ pick ] dip swap 3map-as ; inline
476
477 : find-from ( n seq quot -- i elt )
478     [ (find-integer) ] (find-from) ; inline
479
480 : find ( seq quot -- i elt )
481     [ find-integer ] (find) ; inline
482
483 : find-last-from ( n seq quot -- i elt )
484     [ nip find-last-integer ] (find-from) ; inline
485
486 : find-last ( seq quot -- i elt )
487     [ [ 1 - ] dip find-last-integer ] (find) ; inline
488
489 : all? ( seq quot -- ? )
490     (each) all-integers? ; inline
491
492 : push-if ( elt quot accum -- )
493     [ keep ] dip rot [ push ] [ 2drop ] if ; inline
494
495 : pusher-for ( quot exemplar -- quot accum )
496     [ length ] keep new-resizable [ [ push-if ] 2curry ] keep ; inline
497
498 : pusher ( quot -- quot accum )
499     V{ } pusher-for ; inline
500
501 : filter-as ( seq quot exemplar -- subseq )
502     dup [ pusher-for [ each ] dip ] curry dip like ; inline
503
504 : filter ( seq quot -- subseq )
505     over filter-as ; inline
506
507 : push-either ( elt quot accum1 accum2 -- )
508     [ keep swap ] 2dip ? push ; inline
509
510 : 2pusher ( quot -- quot accum1 accum2 )
511     V{ } clone V{ } clone [ [ push-either ] 3curry ] 2keep ; inline
512
513 : partition ( seq quot -- trueseq falseseq )
514     over [ 2pusher [ each ] 2dip ] dip [ like ] curry bi@ ; inline
515
516 : accumulator-for ( quot exemplar -- quot' vec )
517     [ length ] keep new-resizable [ [ push ] curry compose ] keep ; inline
518
519 : accumulator ( quot -- quot' vec )
520     V{ } accumulator-for ; inline
521
522 : produce-as ( pred quot exemplar -- seq )
523     dup [ accumulator-for [ while ] dip ] curry dip like ; inline
524
525 : produce ( pred quot -- seq )
526     { } produce-as ; inline
527
528 : follow ( obj quot -- seq )
529     [ dup ] swap [ keep ] curry produce nip ; inline
530
531 : each-index ( seq quot -- )
532     (each-index) each-integer ; inline
533
534 : interleave ( seq between quot -- )
535     pick empty? [ 3drop ] [
536         [ [ drop first-unsafe ] dip call ]
537         [ [ rest-slice ] 2dip [ bi* ] 2curry each ]
538         3bi
539     ] if ; inline
540
541 : map-index ( seq quot -- newseq )
542     [ dup length iota ] dip 2map ; inline
543
544 : reduce-index ( seq identity quot -- )
545     swapd each-index ; inline
546
547 : index ( obj seq -- n )
548     [ = ] with find drop ;
549
550 : index-from ( obj i seq -- n )
551     rot [ = ] curry find-from drop ;
552
553 : last-index ( obj seq -- n )
554     [ = ] with find-last drop ;
555
556 : last-index-from ( obj i seq -- n )
557     rot [ = ] curry find-last-from drop ;
558
559 <PRIVATE
560
561 : (indices) ( elt i obj accum -- )
562     [ swap [ = ] dip ] dip [ push ] 2curry when ; inline
563
564 PRIVATE>
565
566 : indices ( obj seq -- indices )
567     swap V{ } clone
568     [ [ (indices) ] 2curry each-index ] keep ;
569
570 : nths ( indices seq -- seq' )
571     [ nth ] curry map ;
572
573 : any? ( seq quot -- ? )
574     find drop >boolean ; inline
575
576 : member? ( elt seq -- ? )
577     [ = ] with any? ;
578
579 : member-eq? ( elt seq -- ? )
580     [ eq? ] with any? ;
581
582 : remove ( elt seq -- newseq )
583     [ = not ] with filter ;
584
585 : remove-eq ( elt seq -- newseq )
586     [ eq? not ] with filter ;
587
588 : sift ( seq -- newseq )
589     [ ] filter ;
590
591 : harvest ( seq -- newseq )
592     [ empty? not ] filter ;
593
594 : mismatch ( seq1 seq2 -- i )
595     [ min-length iota ] 2keep
596     [ 2nth-unsafe = not ] 2curry
597     find drop ; inline
598
599 M: sequence <=>
600     2dup mismatch
601     [ -rot 2nth-unsafe <=> ] [ [ length ] compare ] if* ;
602
603 : sequence= ( seq1 seq2 -- ? )
604     2dup [ length ] bi@ =
605     [ mismatch not ] [ 2drop f ] if ; inline
606
607 ERROR: assert-sequence got expected ;
608
609 : assert-sequence= ( a b -- )
610     2dup sequence= [ 2drop ] [ assert-sequence ] if ;
611
612 : sequence-hashcode-step ( oldhash newpart -- newhash )
613     >fixnum swap [
614         [ -2 fixnum-shift-fast ] [ 5 fixnum-shift-fast ] bi
615         fixnum+fast fixnum+fast
616     ] keep fixnum-bitxor ; inline
617
618 : sequence-hashcode ( n seq -- x )
619     [ 0 ] 2dip [ hashcode* sequence-hashcode-step ] with each ; inline
620
621 M: reversed equal? over reversed? [ sequence= ] [ 2drop f ] if ;
622
623 M: slice equal? over slice? [ sequence= ] [ 2drop f ] if ;
624
625 : move ( to from seq -- )
626     2over =
627     [ 3drop ] [ [ nth swap ] [ set-nth ] bi ] if ; inline
628
629 <PRIVATE
630
631 : (filter!) ( quot: ( elt -- ? ) store scan seq -- )
632     2dup length < [
633         [ move ] 3keep
634         [ nth-unsafe pick call [ 1 + ] when ] 2keep
635         [ 1 + ] dip
636         (filter!)
637     ] [ nip set-length drop ] if ; inline recursive
638
639 PRIVATE>
640
641 : filter! ( seq quot -- seq )
642     swap [ [ 0 0 ] dip (filter!) ] keep ; inline
643
644 : remove! ( elt seq -- seq )
645     [ = not ] with filter! ;
646
647 : remove-eq! ( elt seq -- seq )
648     [ eq? not ] with filter! ;
649
650 : prefix ( seq elt -- newseq )
651     over [ over length 1 + ] dip [
652         [ 0 swap set-nth-unsafe ] keep
653         [ 1 swap copy ] keep
654     ] new-like ;
655
656 : suffix ( seq elt -- newseq )
657     over [ over length 1 + ] dip [
658         [ [ over length ] dip set-nth-unsafe ] keep
659         [ 0 swap copy ] keep
660     ] new-like ;
661
662 : suffix! ( seq elt -- seq ) over push ; inline
663
664 : append! ( seq1 seq2 -- seq1 ) over push-all ; inline
665
666 : last ( seq -- elt ) [ length 1 - ] [ nth ] bi ;
667
668 : set-last ( elt seq -- ) [ length 1 - ] keep set-nth ;
669
670 : pop* ( seq -- ) [ length 1 - ] [ shorten ] bi ;
671
672 <PRIVATE
673
674 : move-backward ( shift from to seq -- )
675     2over = [
676         2drop 2drop
677     ] [
678         [ [ 2over + pick ] dip move [ 1 + ] dip ] keep
679         move-backward
680     ] if ;
681
682 : move-forward ( shift from to seq -- )
683     2over = [
684         2drop 2drop
685     ] [
686         [ [ pick [ dup dup ] dip + swap ] dip move 1 - ] keep
687         move-forward
688     ] if ;
689
690 : (open-slice) ( shift from to seq ? -- )
691     [
692         [ [ 1 - ] bi@ ] dip move-forward
693     ] [
694         [ over - ] 2dip move-backward
695     ] if ;
696
697 : open-slice ( shift from seq -- )
698     pick 0 = [
699         3drop
700     ] [
701         pick over length + over
702         [ pick 0 > [ [ length ] keep ] dip (open-slice) ] 2dip
703         set-length
704     ] if ;
705
706 PRIVATE>
707
708 : delete-slice ( from to seq -- )
709     check-slice [ over [ - ] dip ] dip open-slice ;
710
711 : remove-nth! ( n seq -- seq )
712     [ [ dup 1 + ] dip delete-slice ] keep ;
713
714 : snip ( from to seq -- head tail )
715     [ swap head ] [ swap tail ] bi-curry bi* ; inline
716
717 : snip-slice ( from to seq -- head tail )
718     [ swap head-slice ] [ swap tail-slice ] bi-curry bi* ; inline
719
720 : replace-slice ( new from to seq -- seq' )
721     snip-slice surround ;
722
723 : remove-nth ( n seq -- seq' )
724     [ [ { } ] dip dup 1 + ] dip replace-slice ;
725
726 : pop ( seq -- elt )
727     [ length 1 - ] [ [ nth ] [ shorten ] 2bi ] bi ;
728
729 : exchange ( m n seq -- )
730     [ nip bounds-check 2drop ]
731     [ bounds-check 3drop ]
732     [ exchange-unsafe ]
733     3tri ;
734
735 : reverse! ( seq -- seq )
736     [
737         [ length 2/ iota ] [ length ] [ ] tri
738         [ [ over - 1 - ] dip exchange-unsafe ] 2curry each
739     ] keep ;
740
741 : reverse ( seq -- newseq )
742     [
743         dup [ length ] keep new-sequence
744         [ 0 swap copy ] keep reverse!
745     ] keep like ;
746
747 : sum-lengths ( seq -- n )
748     0 [ length + ] reduce ;
749
750 : concat-as ( seq exemplar -- newseq )
751     swap [ { } ] [
752         [ sum-lengths over new-resizable ] keep
753         [ append! ] each
754     ] if-empty swap like ;
755
756 : concat ( seq -- newseq )
757     [ { } ] [ dup first concat-as ] if-empty ;
758
759 <PRIVATE
760
761 : joined-length ( seq glue -- n )
762     [ [ sum-lengths ] [ length 1 [-] ] bi ] dip length * + ;
763
764 PRIVATE>
765
766 : join ( seq glue -- newseq )
767     dup empty? [ concat-as ] [
768         [
769             2dup joined-length over new-resizable [
770                 [ [ push-all ] 2curry ] [ [ nip push-all ] 2curry ] 2bi
771                 interleave
772             ] keep
773         ] keep like
774     ] if ;
775
776 : padding ( seq n elt quot -- newseq )
777     [
778         [ over length [-] dup 0 = [ drop ] ] dip
779         [ <repetition> ] curry
780     ] dip compose if ; inline
781
782 : pad-head ( seq n elt -- padded )
783     [ swap dup append-as ] padding ;
784
785 : pad-tail ( seq n elt -- padded )
786     [ append ] padding ;
787
788 : shorter? ( seq1 seq2 -- ? ) [ length ] bi@ < ;
789
790 : head? ( seq begin -- ? )
791     2dup shorter? [
792         2drop f
793     ] [
794         [ nip ] [ length head-slice ] 2bi sequence=
795     ] if ;
796
797 : tail? ( seq end -- ? )
798     2dup shorter? [
799         2drop f
800     ] [
801         [ nip ] [ length tail-slice* ] 2bi sequence=
802     ] if ;
803
804 : cut-slice ( seq n -- before-slice after-slice )
805     [ head-slice ] [ tail-slice ] 2bi ;
806
807 : insert-nth ( elt n seq -- seq' )
808     swap cut-slice [ swap suffix ] dip append ;
809
810 : midpoint@ ( seq -- n ) length 2/ ; inline
811
812 : halves ( seq -- first-slice second-slice )
813     dup midpoint@ cut-slice ;
814
815 : binary-reduce ( seq start quot: ( elt1 elt2 -- newelt ) -- value )
816     #! We can't use case here since combinators depends on
817     #! sequences
818     pick length dup 0 3 between? [
819         >fixnum {
820             [ drop nip ]
821             [ 2drop first ]
822             [ [ drop first2 ] dip call ]
823             [ [ drop first3 ] dip bi@ ]
824         } dispatch
825     ] [
826         drop
827         [ halves ] 2dip
828         [ [ binary-reduce ] 2curry bi@ ] keep
829         call
830     ] if ; inline recursive
831
832 : cut ( seq n -- before after )
833     [ head ] [ tail ] 2bi ;
834
835 : cut* ( seq n -- before after )
836     [ head* ] [ tail* ] 2bi ;
837
838 <PRIVATE
839
840 : (start) ( subseq seq n -- subseq seq ? )
841     pick length iota [
842         [ 3dup ] dip [ + swap nth-unsafe ] keep rot nth-unsafe =
843     ] all? nip ; inline
844
845 PRIVATE>
846
847 : start* ( subseq seq n -- i )
848     pick length pick length swap - 1 + iota
849     [ (start) ] find-from
850     swap [ 3drop ] dip ;
851
852 : start ( subseq seq -- i ) 0 start* ; inline
853
854 : subseq? ( subseq seq -- ? ) start >boolean ;
855
856 : drop-prefix ( seq1 seq2 -- slice1 slice2 )
857     2dup mismatch [ 2dup min-length ] unless*
858     [ tail-slice ] curry bi@ ;
859
860 : unclip ( seq -- rest first )
861     [ rest ] [ first-unsafe ] bi ;
862
863 : unclip-last ( seq -- butlast last )
864     [ but-last ] [ last ] bi ;
865
866 : unclip-slice ( seq -- rest-slice first )
867     [ rest-slice ] [ first-unsafe ] bi ; inline
868
869 : 2unclip-slice ( seq1 seq2 -- rest-slice1 rest-slice2 first1 first2 )
870     [ unclip-slice ] bi@ swapd ; inline
871
872 : map-reduce ( seq map-quot reduce-quot -- result )
873     [ [ unclip-slice ] dip [ call ] keep ] dip
874     compose reduce ; inline
875
876 : 2map-reduce ( seq1 seq2 map-quot reduce-quot -- result )
877     [ [ 2unclip-slice ] dip [ call ] keep ] dip
878     compose 2reduce ; inline
879
880 <PRIVATE
881
882 : (map-find) ( seq quot find-quot -- result elt )
883     [ [ f ] 2dip [ [ nip ] dip call dup ] curry ] dip call
884     [ [ drop f ] unless ] dip ; inline
885
886 PRIVATE>
887
888 : map-find ( seq quot -- result elt )
889     [ find ] (map-find) ; inline
890
891 : map-find-last ( seq quot -- result elt )
892     [ find-last ] (map-find) ; inline
893
894 : unclip-last-slice ( seq -- butlast-slice last )
895     [ but-last-slice ] [ last ] bi ; inline
896
897 : <flat-slice> ( seq -- slice )
898     dup slice? [ { } like ] when
899     [ drop 0 ] [ length ] [ ] tri <slice> ;
900     inline
901
902 <PRIVATE
903     
904 : (trim-head) ( seq quot -- seq n )
905     over [ [ not ] compose find drop ] dip
906     [ length or ] keep swap ; inline
907
908 : (trim-tail) ( seq quot -- seq n )
909     over [ [ not ] compose find-last drop ?1+ ] dip
910     swap ; inline
911
912 PRIVATE>
913
914 : trim-head-slice ( seq quot -- slice )
915     (trim-head) tail-slice ; inline
916
917 : trim-head ( seq quot -- newseq )
918     (trim-head) tail ; inline
919
920 : trim-tail-slice ( seq quot -- slice )
921     (trim-tail) head-slice ; inline
922
923 : trim-tail ( seq quot -- newseq )
924     (trim-tail) head ; inline
925
926 : trim-slice ( seq quot -- slice )
927     [ trim-head-slice ] [ trim-tail-slice ] bi ; inline
928
929 : trim ( seq quot -- newseq )
930     [ trim-slice ] [ drop ] 2bi like ; inline
931
932 : sum ( seq -- n ) 0 [ + ] binary-reduce ;
933
934 : product ( seq -- n ) 1 [ * ] binary-reduce ;
935
936 : infimum ( seq -- n ) [ ] [ min ] map-reduce ;
937
938 : supremum ( seq -- n ) [ ] [ max ] map-reduce ;
939
940 : map-sum ( seq quot -- n )
941     [ 0 ] 2dip [ dip + ] curry [ swap ] prepose each ; inline
942
943 : count ( seq quot -- n ) [ 1 0 ? ] compose map-sum ; inline
944
945 ! We hand-optimize flip to such a degree because type hints
946 ! cannot express that an array is an array of arrays yet, and
947 ! this word happens to be performance-critical since the compiler
948 ! itself uses it. Optimizing it like this reduced compile time.
949 <PRIVATE
950
951 : generic-flip ( matrix -- newmatrix )
952     [ dup first length [ length min ] reduce iota ] keep
953     [ [ nth-unsafe ] with { } map-as ] curry { } map-as ; inline
954
955 USE: arrays
956
957 : array-length ( array -- len )
958     { array } declare length>> ; inline
959
960 : array-flip ( matrix -- newmatrix )
961     { array } declare
962     [ dup first array-length [ array-length min ] reduce iota ] keep
963     [ [ { array } declare array-nth ] with { } map-as ] curry { } map-as ;
964
965 PRIVATE>
966
967 : flip ( matrix -- newmatrix )
968     dup empty? [
969         dup array? [
970             dup [ array? ] all?
971             [ array-flip ] [ generic-flip ] if
972         ] [ generic-flip ] if
973     ] unless ;