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