]> gitweb.factorcode.org Git - factor.git/blob - core/sequences/sequences.factor
db configurations factored out through db.info
[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 ;
22
23 GENERIC: lengthen ( n seq -- )
24 GENERIC: shorten ( n seq -- )
25
26 M: sequence lengthen 2dup length > [ set-length ] [ 2drop ] if ;
27
28 M: sequence shorten 2dup length < [ set-length ] [ 2drop ] if ;
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 ;
86 M: sequence set-nth bounds-check set-nth-unsafe ;
87
88 M: sequence nth-unsafe nth ;
89 M: sequence set-nth-unsafe set-nth ;
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 ;
96 M: f nth-unsafe nip ;
97 M: f like drop [ f ] when-empty ;
98
99 INSTANCE: f immutable-sequence
100
101 ! Integers support the sequence protocol
102 M: integer length ;
103 M: integer nth-unsafe drop ;
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>> ;
117 M: iota nth-unsafe drop ;
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 ; flushable
174
175 : first3 ( seq -- first second third )
176     2 swap bounds-check nip first3-unsafe ; flushable
177
178 : first4 ( seq -- first second third fourth )
179     3 swap bounds-check nip first4-unsafe ; flushable
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 ;
189 M: virtual-sequence set-nth virtual@ set-nth ;
190 M: virtual-sequence nth-unsafe virtual@ nth-unsafe ;
191 M: virtual-sequence set-nth-unsafe virtual@ set-nth-unsafe ;
192 M: virtual-sequence like virtual-seq like ;
193 M: virtual-sequence new-sequence virtual-seq new-sequence ;
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>> ;
203
204 M: reversed virtual@ seq>> [ length swap - 1 - ] keep ;
205
206 M: reversed length seq>> length ;
207
208 INSTANCE: reversed virtual-sequence
209
210 ! A slice of another sequence.
211 TUPLE: slice
212 { from read-only }
213 { to read-only }
214 { seq read-only } ;
215
216 : collapse-slice ( m n slice -- m' n' seq )
217     [ from>> ] [ seq>> ] bi [ [ + ] curry bi@ ] dip ; inline
218
219 TUPLE: slice-error from to seq reason ;
220
221 : slice-error ( from to seq ? string -- from to seq )
222     [ \ slice-error boa throw ] curry when ; inline
223
224 : check-slice ( from to seq -- from to seq )
225     3dup
226     [ 2drop 0 < "start < 0" slice-error ]
227     [ [ drop ] 2dip length > "end > sequence" slice-error ]
228     [ drop > "start > end" slice-error ]
229     3tri ; inline
230
231 : <slice> ( from to seq -- slice )
232     dup slice? [ collapse-slice ] when
233     check-slice
234     slice boa ; inline
235
236 M: slice virtual-seq seq>> ;
237
238 M: slice virtual@ [ from>> + ] [ seq>> ] bi ;
239
240 M: slice length [ to>> ] [ from>> ] bi - ;
241
242 : short ( seq n -- seq n' ) over length min ; inline
243
244 : head-slice ( seq n -- slice ) (head) <slice> ; inline
245
246 : tail-slice ( seq n -- slice ) (tail) <slice> ; inline
247
248 : rest-slice ( seq -- slice ) 1 tail-slice ; inline
249
250 : head-slice* ( seq n -- slice ) from-end head-slice ; inline
251
252 : tail-slice* ( seq n -- slice ) from-end tail-slice ; inline
253
254 : but-last-slice ( seq -- slice ) 1 head-slice* ; inline
255
256 INSTANCE: slice virtual-sequence
257
258 ! One element repeated many times
259 TUPLE: repetition { len read-only } { elt read-only } ;
260
261 C: <repetition> repetition
262
263 M: repetition length len>> ;
264 M: repetition nth-unsafe nip elt>> ;
265
266 INSTANCE: repetition immutable-sequence
267
268 <PRIVATE
269
270 : check-length ( n -- n )
271     #! Ricing.
272     dup integer? [ "length not an integer" throw ] 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 ;
318
319 M: immutable-sequence clone-like like ;
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-as ( seq quot exemplar -- newseq )
418     [ over length ] dip [ [ map-into ] keep ] new-like ; inline
419
420 : map ( seq quot -- newseq )
421     over map-as ; inline
422
423 : replicate ( seq quot -- newseq )
424     [ drop ] prepose map ; inline
425
426 : replicate-as ( seq quot exemplar -- newseq )
427     [ [ drop ] prepose ] dip map-as ; inline
428
429 : change-each ( seq quot -- )
430     over map-into ; inline
431
432 : accumulate ( seq identity quot -- final newseq )
433     swapd [ [ call ] [ 2drop ] 3bi ] curry { } map-as ; inline
434
435 : 2each ( seq1 seq2 quot -- )
436     (2each) each-integer ; inline
437
438 : 2reverse-each ( seq1 seq2 quot -- )
439     [ [ <reversed> ] bi@ ] dip 2each ; inline
440
441 : 2reduce ( seq1 seq2 identity quot -- result )
442     [ -rot ] dip 2each ; inline
443
444 : 2map-as ( seq1 seq2 quot exemplar -- newseq )
445     [ (2each) ] dip map-as ; inline
446
447 : 2map ( seq1 seq2 quot -- newseq )
448     pick 2map-as ; inline
449
450 : 2all? ( seq1 seq2 quot -- ? )
451     (2each) all-integers? ; inline
452
453 : 3each ( seq1 seq2 seq3 quot -- )
454     (3each) each ; inline
455
456 : 3map-as ( seq1 seq2 seq3 quot exemplar -- newseq )
457     [ (3each) ] dip map-as ; inline
458
459 : 3map ( seq1 seq2 seq3 quot -- newseq )
460     [ pick ] dip swap 3map-as ; inline
461
462 : find-from ( n seq quot -- i elt )
463     [ (find-integer) ] (find-from) ; inline
464
465 : find ( seq quot -- i elt )
466     [ find-integer ] (find) ; inline
467
468 : find-last-from ( n seq quot -- i elt )
469     [ nip find-last-integer ] (find-from) ; inline
470
471 : find-last ( seq quot -- i elt )
472     [ [ 1 - ] dip find-last-integer ] (find) ; inline
473
474 : all? ( seq quot -- ? )
475     (each) all-integers? ; inline
476
477 : push-if ( elt quot accum -- )
478     [ keep ] dip rot [ push ] [ 2drop ] if ; inline
479
480 : pusher ( quot -- quot accum )
481     V{ } clone [ [ push-if ] 2curry ] keep ; inline
482
483 : filter ( seq quot -- subseq )
484     over [ pusher [ each ] dip ] dip like ; inline
485
486 : push-either ( elt quot accum1 accum2 -- )
487     [ keep swap ] 2dip ? push ; inline
488
489 : 2pusher ( quot -- quot accum1 accum2 )
490     V{ } clone V{ } clone [ [ push-either ] 3curry ] 2keep ; inline
491
492 : partition ( seq quot -- trueseq falseseq )
493     over [ 2pusher [ each ] 2dip ] dip [ like ] curry bi@ ; inline
494
495 : accumulator ( quot -- quot' vec )
496     V{ } clone [ [ push ] curry compose ] keep ; inline
497
498 : produce-as ( pred quot exemplar -- seq )
499     [ accumulator [ while ] dip ] dip like ; inline
500
501 : produce ( pred quot -- seq )
502     { } produce-as ; inline
503
504 : follow ( obj quot -- seq )
505     [ dup ] swap [ keep ] curry produce nip ; inline
506
507 : each-index ( seq quot -- )
508     (each-index) each-integer ; inline
509
510 : interleave ( seq between quot -- )
511     pick empty? [ 3drop ] [
512         [ [ drop first-unsafe ] dip call ]
513         [ [ rest-slice ] 2dip [ bi* ] 2curry each ]
514         3bi
515     ] if ; inline
516
517 : map-index ( seq quot -- newseq )
518     [ dup length iota ] dip 2map ; inline
519
520 : reduce-index ( seq identity quot -- )
521     swapd each-index ; inline
522
523 : index ( obj seq -- n )
524     [ = ] with find drop ;
525
526 : index-from ( obj i seq -- n )
527     rot [ = ] curry find-from drop ;
528
529 : last-index ( obj seq -- n )
530     [ = ] with find-last drop ;
531
532 : last-index-from ( obj i seq -- n )
533     rot [ = ] curry find-last-from drop ;
534
535 : (indices) ( elt i obj accum -- )
536     [ swap [ = ] dip ] dip [ push ] 2curry when ; inline
537
538 : indices ( obj seq -- indices )
539     swap V{ } clone
540     [ [ (indices) ] 2curry each-index ] keep ;
541
542 : nths ( indices seq -- seq' )
543     [ nth ] curry map ;
544
545 : any? ( seq quot -- ? )
546     find drop >boolean ; inline
547
548 : member? ( elt seq -- ? )
549     [ = ] with any? ;
550
551 : memq? ( elt seq -- ? )
552     [ eq? ] with any? ;
553
554 : remove ( elt seq -- newseq )
555     [ = not ] with filter ;
556
557 : remq ( elt seq -- newseq )
558     [ eq? not ] with filter ;
559
560 : sift ( seq -- newseq )
561     [ ] filter ;
562
563 : harvest ( seq -- newseq )
564     [ empty? not ] filter ;
565
566 : mismatch ( seq1 seq2 -- i )
567     [ min-length iota ] 2keep
568     [ 2nth-unsafe = not ] 2curry
569     find drop ; inline
570
571 M: sequence <=>
572     2dup mismatch
573     [ -rot 2nth-unsafe <=> ] [ [ length ] compare ] if* ;
574
575 : sequence= ( seq1 seq2 -- ? )
576     2dup [ length ] bi@ =
577     [ mismatch not ] [ 2drop f ] if ; inline
578
579 ERROR: assert-sequence got expected ;
580
581 : assert-sequence= ( a b -- )
582     2dup sequence= [ 2drop ] [ assert-sequence ] if ;
583
584 : sequence-hashcode-step ( oldhash newpart -- newhash )
585     >fixnum swap [
586         [ -2 fixnum-shift-fast ] [ 5 fixnum-shift-fast ] bi
587         fixnum+fast fixnum+fast
588     ] keep fixnum-bitxor ; inline
589
590 : sequence-hashcode ( n seq -- x )
591     [ 0 ] 2dip [ hashcode* sequence-hashcode-step ] with each ; inline
592
593 M: reversed equal? over reversed? [ sequence= ] [ 2drop f ] if ;
594
595 M: slice equal? over slice? [ sequence= ] [ 2drop f ] if ;
596
597 : move ( to from seq -- )
598     2over =
599     [ 3drop ] [ [ nth swap ] [ set-nth ] bi ] if ; inline
600
601 <PRIVATE
602
603 : (filter-here) ( quot: ( elt -- ? ) store scan seq -- )
604     2dup length < [
605         [ move ] 3keep
606         [ nth-unsafe pick call [ 1 + ] when ] 2keep
607         [ 1 + ] dip
608         (filter-here)
609     ] [ nip set-length drop ] if ; inline recursive
610
611 PRIVATE>
612
613 : filter-here ( seq quot -- )
614     swap [ 0 0 ] dip (filter-here) ; inline
615
616 : delete ( elt seq -- )
617     [ = not ] with filter-here ;
618
619 : delq ( elt seq -- )
620     [ eq? not ] with filter-here ;
621
622 : prefix ( seq elt -- newseq )
623     over [ over length 1 + ] dip [
624         [ 0 swap set-nth-unsafe ] keep
625         [ 1 swap copy ] keep
626     ] new-like ;
627
628 : suffix ( seq elt -- newseq )
629     over [ over length 1 + ] dip [
630         [ [ over length ] dip set-nth-unsafe ] keep
631         [ 0 swap copy ] keep
632     ] new-like ;
633
634 : last ( seq -- elt ) [ length 1 - ] [ nth ] bi ;
635
636 : pop* ( seq -- ) [ length 1 - ] [ shorten ] bi ;
637
638 <PRIVATE
639
640 : move-backward ( shift from to seq -- )
641     2over = [
642         2drop 2drop
643     ] [
644         [ [ 2over + pick ] dip move [ 1 + ] dip ] keep
645         move-backward
646     ] if ;
647
648 : move-forward ( shift from to seq -- )
649     2over = [
650         2drop 2drop
651     ] [
652         [ [ pick [ dup dup ] dip + swap ] dip move 1 - ] keep
653         move-forward
654     ] if ;
655
656 : (open-slice) ( shift from to seq ? -- )
657     [
658         [ [ 1 - ] bi@ ] dip move-forward
659     ] [
660         [ over - ] 2dip move-backward
661     ] if ;
662
663 : open-slice ( shift from seq -- )
664     pick 0 = [
665         3drop
666     ] [
667         pick over length + over
668         [ pick 0 > [ [ length ] keep ] dip (open-slice) ] 2dip
669         set-length
670     ] if ;
671
672 PRIVATE>
673
674 : delete-slice ( from to seq -- )
675     check-slice [ over [ - ] dip ] dip open-slice ;
676
677 : delete-nth ( n seq -- )
678     [ dup 1 + ] dip delete-slice ;
679
680 : snip ( from to seq -- head tail )
681     [ swap head ] [ swap tail ] bi-curry bi* ; inline
682
683 : snip-slice ( from to seq -- head tail )
684     [ swap head-slice ] [ swap tail-slice ] bi-curry bi* ; inline
685
686 : replace-slice ( new from to seq -- seq' )
687     snip-slice surround ;
688
689 : remove-nth ( n seq -- seq' )
690     [ [ { } ] dip dup 1 + ] dip replace-slice ;
691
692 : pop ( seq -- elt )
693     [ length 1 - ] [ [ nth ] [ shorten ] 2bi ] bi ;
694
695 : exchange ( m n seq -- )
696     [ nip bounds-check 2drop ]
697     [ bounds-check 3drop ]
698     [ exchange-unsafe ]
699     3tri ;
700
701 : reverse-here ( seq -- )
702     [ length 2/ ] [ length ] [ ] tri
703     [ [ over - 1 - ] dip exchange-unsafe ] 2curry each ;
704
705 : reverse ( seq -- newseq )
706     [
707         dup [ length ] keep new-sequence
708         [ 0 swap copy ] keep
709         [ reverse-here ] keep
710     ] keep like ;
711
712 : sum-lengths ( seq -- n )
713     0 [ length + ] reduce ;
714
715 : concat-as ( seq exemplar -- newseq )
716     swap [ { } ] [
717         [ sum-lengths over new-resizable ] keep
718         [ over push-all ] each
719     ] if-empty swap like ;
720
721 : concat ( seq -- newseq )
722     [ { } ] [ dup first concat-as ] if-empty ;
723
724 <PRIVATE
725
726 : joined-length ( seq glue -- n )
727     [ [ sum-lengths ] [ length 1 [-] ] bi ] dip length * + ;
728
729 PRIVATE>
730
731 : join ( seq glue -- newseq )
732     dup empty? [ concat-as ] [
733         [
734             2dup joined-length over new-resizable [
735                 [ [ push-all ] 2curry ] [ [ nip push-all ] 2curry ] 2bi
736                 interleave
737             ] keep
738         ] keep like
739     ] if ;
740
741 : padding ( seq n elt quot -- newseq )
742     [
743         [ over length [-] dup 0 = [ drop ] ] dip
744         [ <repetition> ] curry
745     ] dip compose if ; inline
746
747 : pad-head ( seq n elt -- padded )
748     [ swap dup append-as ] padding ;
749
750 : pad-tail ( seq n elt -- padded )
751     [ append ] padding ;
752
753 : shorter? ( seq1 seq2 -- ? ) [ length ] bi@ < ;
754
755 : head? ( seq begin -- ? )
756     2dup shorter? [
757         2drop f
758     ] [
759         [ nip ] [ length head-slice ] 2bi sequence=
760     ] if ;
761
762 : tail? ( seq end -- ? )
763     2dup shorter? [
764         2drop f
765     ] [
766         [ nip ] [ length tail-slice* ] 2bi sequence=
767     ] if ;
768
769 : cut-slice ( seq n -- before-slice after-slice )
770     [ head-slice ] [ tail-slice ] 2bi ;
771
772 : insert-nth ( elt n seq -- seq' )
773     swap cut-slice [ swap suffix ] dip append ;
774
775 : midpoint@ ( seq -- n ) length 2/ ; inline
776
777 : halves ( seq -- first-slice second-slice )
778     dup midpoint@ cut-slice ;
779
780 : binary-reduce ( seq start quot: ( elt1 elt2 -- newelt ) -- value )
781     #! We can't use case here since combinators depends on
782     #! sequences
783     pick length dup 0 3 between? [
784         >fixnum {
785             [ drop nip ]
786             [ 2drop first ]
787             [ [ drop first2 ] dip call ]
788             [ [ drop first3 ] dip bi@ ]
789         } dispatch
790     ] [
791         drop
792         [ halves ] 2dip
793         [ [ binary-reduce ] 2curry bi@ ] keep
794         call
795     ] if ; inline recursive
796
797 : cut ( seq n -- before after )
798     [ head ] [ tail ] 2bi ;
799
800 : cut* ( seq n -- before after )
801     [ head* ] [ tail* ] 2bi ;
802
803 <PRIVATE
804
805 : (start) ( subseq seq n -- subseq seq ? )
806     pick length [
807         [ 3dup ] dip [ + swap nth-unsafe ] keep rot nth-unsafe =
808     ] all? nip ; inline
809
810 PRIVATE>
811
812 : start* ( subseq seq n -- i )
813     pick length pick length swap - 1 +
814     [ (start) ] find-from
815     swap [ 3drop ] dip ;
816
817 : start ( subseq seq -- i ) 0 start* ; inline
818
819 : subseq? ( subseq seq -- ? ) start >boolean ;
820
821 : drop-prefix ( seq1 seq2 -- slice1 slice2 )
822     2dup mismatch [ 2dup min-length ] unless*
823     [ tail-slice ] curry bi@ ;
824
825 : unclip ( seq -- rest first )
826     [ rest ] [ first-unsafe ] bi ;
827
828 : unclip-last ( seq -- butlast last )
829     [ but-last ] [ last ] bi ;
830
831 : unclip-slice ( seq -- rest-slice first )
832     [ rest-slice ] [ first-unsafe ] bi ; inline
833
834 : 2unclip-slice ( seq1 seq2 -- rest-slice1 rest-slice2 first1 first2 )
835     [ unclip-slice ] bi@ swapd ; inline
836
837 : map-reduce ( seq map-quot reduce-quot -- result )
838     [ [ unclip-slice ] dip [ call ] keep ] dip
839     compose reduce ; inline
840
841 : 2map-reduce ( seq1 seq2 map-quot reduce-quot -- result )
842     [ [ 2unclip-slice ] dip [ call ] keep ] dip
843     compose 2reduce ; inline
844
845 <PRIVATE
846
847 : (map-find) ( seq quot find-quot -- result elt )
848     [ [ f ] 2dip [ [ nip ] dip call dup ] curry ] dip call
849     [ [ drop f ] unless ] dip ; inline
850
851 PRIVATE>
852
853 : map-find ( seq quot -- result elt )
854     [ find ] (map-find) ; inline
855
856 : map-find-last ( seq quot -- result elt )
857     [ find-last ] (map-find) ; inline
858
859 : unclip-last-slice ( seq -- butlast-slice last )
860     [ but-last-slice ] [ last ] bi ; inline
861
862 : <flat-slice> ( seq -- slice )
863     dup slice? [ { } like ] when
864     [ drop 0 ] [ length ] [ ] tri <slice> ;
865     inline
866
867 <PRIVATE
868     
869 : (trim-head) ( seq quot -- seq n )
870     over [ [ not ] compose find drop ] dip
871     [ length or ] keep swap ; inline
872
873 : (trim-tail) ( seq quot -- seq n )
874     over [ [ not ] compose find-last drop ?1+ ] dip
875     swap ; inline
876
877 PRIVATE>
878
879 : trim-head-slice ( seq quot -- slice )
880     (trim-head) tail-slice ; inline
881
882 : trim-head ( seq quot -- newseq )
883     (trim-head) tail ; inline
884
885 : trim-tail-slice ( seq quot -- slice )
886     (trim-tail) head-slice ; inline
887
888 : trim-tail ( seq quot -- newseq )
889     (trim-tail) head ; inline
890
891 : trim-slice ( seq quot -- slice )
892     [ trim-head-slice ] [ trim-tail-slice ] bi ; inline
893
894 : trim ( seq quot -- newseq )
895     [ trim-slice ] [ drop ] 2bi like ; inline
896
897 : sum ( seq -- n ) 0 [ + ] binary-reduce ;
898
899 : product ( seq -- n ) 1 [ * ] binary-reduce ;
900
901 : infimum ( seq -- n ) [ ] [ min ] map-reduce ;
902
903 : supremum ( seq -- n ) [ ] [ max ] map-reduce ;
904
905 : sigma ( seq quot -- n )
906     [ 0 ] 2dip [ dip + ] curry [ swap ] prepose each ; inline
907
908 : count ( seq quot -- n ) [ 1 0 ? ] compose sigma ; inline
909
910 ! We hand-optimize flip to such a degree because type hints
911 ! cannot express that an array is an array of arrays yet, and
912 ! this word happens to be performance-critical since the compiler
913 ! itself uses it. Optimizing it like this reduced compile time.
914 <PRIVATE
915
916 : generic-flip ( matrix -- newmatrix )
917     [ dup first length [ length min ] reduce ] keep
918     [ [ nth-unsafe ] with { } map-as ] curry { } map-as ; inline
919
920 USE: arrays
921
922 : array-length ( array -- len )
923     { array } declare length>> ; inline
924
925 : array-flip ( matrix -- newmatrix )
926     { array } declare
927     [ dup first array-length [ array-length min ] reduce ] keep
928     [ [ array-nth ] with { } map-as ] curry { } map-as ;
929
930 PRIVATE>
931
932 : flip ( matrix -- newmatrix )
933     dup empty? [
934         dup array? [
935             dup [ array? ] all?
936             [ array-flip ] [ generic-flip ] if
937         ] [ generic-flip ] if
938     ] unless ;