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 ;
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
18 : new-like ( len exemplar quot -- seq )
19 over [ [ new-sequence ] dip call ] dip like ; inline
21 M: sequence like drop ; inline
23 GENERIC: lengthen ( n seq -- )
24 GENERIC: shorten ( n seq -- )
26 M: sequence lengthen 2dup length > [ set-length ] [ 2drop ] if ; inline
28 M: sequence shorten 2dup length < [ set-length ] [ 2drop ] if ; inline
30 : empty? ( seq -- ? ) length 0 = ; inline
32 : if-empty ( seq quot1 quot2 -- )
33 [ dup empty? ] [ [ drop ] prepose ] [ ] tri* if ; inline
35 : when-empty ( seq quot -- ) [ ] if-empty ; inline
37 : unless-empty ( seq quot -- ) [ ] swap if-empty ; inline
39 : delete-all ( seq -- ) 0 swap set-length ;
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
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
51 : push ( elt seq -- ) [ length ] [ set-nth ] bi ;
53 : bounds-check? ( n seq -- ? )
54 dupd length < [ 0 >= ] [ drop f ] if ; inline
56 ERROR: bounds-error index seq ;
58 : bounds-check ( n seq -- n seq )
59 2dup bounds-check? [ bounds-error ] unless ; inline
61 MIXIN: immutable-sequence
63 ERROR: immutable seq ;
65 M: immutable-sequence set-nth immutable ;
67 INSTANCE: immutable-sequence sequence
71 : array-nth ( n array -- elt )
72 swap 2 fixnum+fast slot ; inline
74 : set-array-nth ( elt n array -- )
75 swap 2 fixnum+fast set-slot ; inline
77 : dispatch ( n array -- ) array-nth call ;
79 GENERIC: resize ( n seq -- newseq ) flushable
81 ! Unsafe sequence protocol for inner loops
82 GENERIC: nth-unsafe ( n seq -- elt ) flushable
83 GENERIC: set-nth-unsafe ( elt n seq -- )
85 M: sequence nth bounds-check nth-unsafe ; inline
86 M: sequence set-nth bounds-check set-nth-unsafe ; inline
88 M: sequence nth-unsafe nth ; inline
89 M: sequence set-nth-unsafe set-nth ; inline
91 : change-nth-unsafe ( i seq quot -- )
92 [ [ nth-unsafe ] dip call ] 3keep drop set-nth-unsafe ; inline
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
99 INSTANCE: f immutable-sequence
101 ! Integers used to support the sequence protocol
102 M: integer length ; inline
103 M: integer nth-unsafe drop ; inline
105 INSTANCE: integer immutable-sequence
109 ! In the future, this will replace integer sequences
110 TUPLE: iota { n integer read-only } ;
112 : iota ( n -- iota ) \ iota boa ; inline
116 M: iota length n>> ; inline
117 M: iota nth-unsafe drop ; inline
119 INSTANCE: iota immutable-sequence
121 : first-unsafe ( seq -- first )
122 0 swap nth-unsafe ; inline
124 : first2-unsafe ( seq -- first second )
125 [ first-unsafe ] [ 1 swap nth-unsafe ] bi ; inline
127 : first3-unsafe ( seq -- first second third )
128 [ first2-unsafe ] [ 2 swap nth-unsafe ] bi ; inline
130 : first4-unsafe ( seq -- first second third fourth )
131 [ first3-unsafe ] [ 3 swap nth-unsafe ] bi ; inline
133 : exchange-unsafe ( m n seq -- )
134 [ [ nth-unsafe ] curry bi@ ]
135 [ [ set-nth-unsafe ] curry bi@ ] 3bi ; inline
137 : (head) ( seq n -- from to seq ) [ 0 ] 2dip swap ; inline
139 : (tail) ( seq n -- from to seq ) swap [ length ] keep ; inline
141 : from-end ( seq n -- seq n' ) [ dup length ] dip - ; inline
143 : (1sequence) ( obj seq -- seq )
144 [ 0 swap set-nth-unsafe ] keep ; inline
146 : (2sequence) ( obj1 obj2 seq -- seq )
147 [ 1 swap set-nth-unsafe ] keep
150 : (3sequence) ( obj1 obj2 obj3 seq -- seq )
151 [ 2 swap set-nth-unsafe ] keep
154 : (4sequence) ( obj1 obj2 obj3 obj4 seq -- seq )
155 [ 3 swap set-nth-unsafe ] keep
160 : 1sequence ( obj exemplar -- seq )
161 1 swap [ (1sequence) ] new-like ; inline
163 : 2sequence ( obj1 obj2 exemplar -- seq )
164 2 swap [ (2sequence) ] new-like ; inline
166 : 3sequence ( obj1 obj2 obj3 exemplar -- seq )
167 3 swap [ (3sequence) ] new-like ; inline
169 : 4sequence ( obj1 obj2 obj3 obj4 exemplar -- seq )
170 4 swap [ (4sequence) ] new-like ; inline
172 : first2 ( seq -- first second )
173 1 swap bounds-check nip first2-unsafe ; inline
175 : first3 ( seq -- first second third )
176 2 swap bounds-check nip first3-unsafe ; inline
178 : first4 ( seq -- first second third fourth )
179 3 swap bounds-check nip first4-unsafe ; inline
181 : ?nth ( n seq -- elt/f )
182 2dup bounds-check? [ nth-unsafe ] [ 2drop f ] if ; inline
184 MIXIN: virtual-sequence
185 GENERIC: virtual-exemplar ( seq -- seq' )
186 GENERIC: virtual@ ( n seq -- n' seq' )
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
195 INSTANCE: virtual-sequence sequence
197 ! A reversal of an underlying sequence.
198 TUPLE: reversed { seq read-only } ;
200 C: <reversed> reversed
202 M: reversed virtual-exemplar seq>> ; inline
203 M: reversed virtual@ seq>> [ length swap - 1 - ] keep ; inline
204 M: reversed length seq>> length ; inline
206 INSTANCE: reversed virtual-sequence
208 ! A slice of another sequence.
214 : collapse-slice ( m n slice -- m' n' seq )
215 [ from>> ] [ seq>> ] bi [ [ + ] curry bi@ ] dip ; inline
217 TUPLE: slice-error from to seq reason ;
219 : slice-error ( from to seq ? string -- from to seq )
220 [ \ slice-error boa throw ] curry when ; inline
222 : check-slice ( from to seq -- from to seq )
224 [ 2drop 0 < "start < 0" slice-error ]
225 [ [ drop ] 2dip length > "end > sequence" slice-error ]
226 [ drop > "start > end" slice-error ]
229 : <slice> ( from to seq -- slice )
230 dup slice? [ collapse-slice ] when
234 M: slice virtual-exemplar seq>> ; inline
236 M: slice virtual@ [ from>> + ] [ seq>> ] bi ; inline
238 M: slice length [ to>> ] [ from>> ] bi - ; inline
240 : short ( seq n -- seq n' ) over length min ; inline
242 : head-slice ( seq n -- slice ) (head) <slice> ; inline
244 : tail-slice ( seq n -- slice ) (tail) <slice> ; inline
246 : rest-slice ( seq -- slice ) 1 tail-slice ; inline
248 : head-slice* ( seq n -- slice ) from-end head-slice ; inline
250 : tail-slice* ( seq n -- slice ) from-end tail-slice ; inline
252 : but-last-slice ( seq -- slice ) 1 head-slice* ; inline
254 INSTANCE: slice virtual-sequence
256 ! One element repeated many times
257 TUPLE: repetition { len read-only } { elt read-only } ;
259 C: <repetition> repetition
261 M: repetition length len>> ; inline
262 M: repetition nth-unsafe nip elt>> ; inline
264 INSTANCE: repetition immutable-sequence
268 ERROR: integer-length-expected obj ;
270 : check-length ( n -- n )
271 dup integer? [ integer-length-expected ] unless ; inline
281 : ((copy)) ( n copy -- )
282 [ [ src-i>> + ] [ src>> ] bi nth-unsafe ]
283 [ [ dst-i>> + ] [ dst>> ] bi set-nth-unsafe ] 2bi ; inline
285 : (copy) ( n copy -- dst )
286 over 0 <= [ nip dst>> ] [ [ 1 - ] dip [ ((copy)) ] [ (copy) ] 2bi ] if ;
289 : subseq>copy ( from to seq -- n copy )
290 [ over - check-length swap ] dip
291 3dup nip new-sequence 0 swap <copy> ; inline
293 : check-copy ( src n dst -- src n dst )
294 3dup over 0 < [ bounds-error ] when
295 [ swap length + ] dip lengthen ; inline
299 : subseq ( from to seq -- subseq )
300 [ check-slice subseq>copy (copy) ] keep like ;
302 : head ( seq n -- headseq ) (head) subseq ;
304 : tail ( seq n -- tailseq ) (tail) subseq ;
306 : rest ( seq -- tailseq ) 1 tail ;
308 : head* ( seq n -- headseq ) from-end head ;
310 : tail* ( seq n -- tailseq ) from-end tail ;
312 : but-last ( seq -- headseq ) 1 head* ;
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
319 M: sequence clone-like
320 [ dup length ] dip new-sequence [ 0 swap copy ] keep ; inline
322 M: immutable-sequence clone-like like ; inline
324 : push-all ( src dest -- ) [ length ] [ copy ] bi ;
328 : (append) ( seq1 seq2 accum -- accum )
329 [ [ over length ] dip copy ]
335 : append-as ( seq1 seq2 exemplar -- newseq )
336 [ over length over length + ] dip
337 [ (append) ] new-like ; inline
339 : 3append-as ( seq1 seq2 seq3 exemplar -- newseq )
340 [ 3dup [ length ] tri@ + + ] dip [
341 [ [ 2over [ length ] bi@ + ] dip copy ]
345 : append ( seq1 seq2 -- newseq ) over append-as ;
347 : prepend ( seq1 seq2 -- newseq ) swap append ; inline
349 : 3append ( seq1 seq2 seq3 -- newseq ) pick 3append-as ;
351 : surround ( seq1 seq2 seq3 -- newseq ) swapd 3append ; inline
353 : glue ( seq1 seq2 seq3 -- newseq ) swap 3append ; inline
355 : change-nth ( i seq quot -- )
356 [ [ nth ] dip call ] 3keep drop set-nth ; inline
358 : min-length ( seq1 seq2 -- n ) [ length ] bi@ min ; inline
360 : max-length ( seq1 seq2 -- n ) [ length ] bi@ max ; inline
364 : ((each)) ( seq -- n quot )
365 [ length ] keep [ nth-unsafe ] curry ; inline
367 : (each) ( seq quot -- n quot' )
368 [ ((each)) ] dip compose ; inline
370 : (each-index) ( seq quot -- n quot' )
371 [ ((each)) [ keep ] curry ] dip compose ; inline
373 : (collect) ( quot into -- quot' )
374 [ [ keep ] dip set-nth-unsafe ] 2curry ; inline
376 : collect ( n quot into -- )
377 (collect) each-integer ; inline
379 : map-into ( seq quot into -- )
380 [ (each) ] dip collect ; inline
382 : 2nth-unsafe ( n seq1 seq2 -- elt1 elt2 )
383 [ nth-unsafe ] bi-curry@ bi ; inline
385 : (2each) ( seq1 seq2 quot -- n quot' )
388 [ 2nth-unsafe ] 2curry
389 ] dip compose ; inline
391 : 3nth-unsafe ( n seq1 seq2 seq3 -- elt1 elt2 elt3 )
392 [ nth-unsafe ] tri-curry@ tri ; inline
394 : (3each) ( seq1 seq2 seq3 quot -- n quot' )
396 [ [ length ] tri@ min min ]
397 [ [ 3nth-unsafe ] 3curry ] 3bi
398 ] dip compose ; inline
400 : finish-find ( i seq -- i elt )
401 over [ dupd nth-unsafe ] [ drop f ] if ; inline
403 : (find) ( seq quot quot' -- i elt )
404 pick [ [ (each) ] dip call ] dip finish-find ; inline
406 : (find-from) ( n seq quot quot' -- i elt )
407 [ 2dup bounds-check? ] 2dip
414 : each ( seq quot -- )
415 (each) each-integer ; inline
417 : reduce ( seq identity quot -- result )
420 : map-integers ( len quot exemplar -- newseq )
421 [ over ] dip [ [ collect ] keep ] new-like ; inline
423 : map-as ( seq quot exemplar -- newseq )
424 [ (each) ] dip map-integers ; inline
426 : map ( seq quot -- newseq )
429 : replicate ( seq quot -- newseq )
430 [ drop ] prepose map ; inline
432 : replicate-as ( seq quot exemplar -- newseq )
433 [ [ drop ] prepose ] dip map-as ; inline
435 : map! ( seq quot -- seq )
436 over [ map-into ] keep ; inline
438 : (accumulate) ( seq identity quot -- seq identity quot )
439 [ swap ] dip [ curry keep ] curry ; inline
441 : accumulate-as ( seq identity quot exemplar -- final newseq )
442 [ (accumulate) ] dip map-as ; inline
444 : accumulate ( seq identity quot -- final newseq )
445 { } accumulate-as ; inline
447 : accumulate! ( seq identity quot -- final seq )
448 (accumulate) map! ; inline
450 : 2each ( seq1 seq2 quot -- )
451 (2each) each-integer ; inline
453 : 2reverse-each ( seq1 seq2 quot -- )
454 [ [ <reversed> ] bi@ ] dip 2each ; inline
456 : 2reduce ( seq1 seq2 identity quot -- result )
457 [ -rot ] dip 2each ; inline
459 : 2map-as ( seq1 seq2 quot exemplar -- newseq )
460 [ (2each) ] dip map-integers ; inline
462 : 2map ( seq1 seq2 quot -- newseq )
463 pick 2map-as ; inline
465 : 2all? ( seq1 seq2 quot -- ? )
466 (2each) all-integers? ; inline
468 : 3each ( seq1 seq2 seq3 quot -- )
469 (3each) each ; inline
471 : 3map-as ( seq1 seq2 seq3 quot exemplar -- newseq )
472 [ (3each) ] dip map-integers ; inline
474 : 3map ( seq1 seq2 seq3 quot -- newseq )
475 [ pick ] dip swap 3map-as ; inline
477 : find-from ( n seq quot -- i elt )
478 [ (find-integer) ] (find-from) ; inline
480 : find ( seq quot -- i elt )
481 [ find-integer ] (find) ; inline
483 : find-last-from ( n seq quot -- i elt )
484 [ nip find-last-integer ] (find-from) ; inline
486 : find-last ( seq quot -- i elt )
487 [ [ 1 - ] dip find-last-integer ] (find) ; inline
489 : all? ( seq quot -- ? )
490 (each) all-integers? ; inline
492 : push-if ( elt quot accum -- )
493 [ keep ] dip rot [ push ] [ 2drop ] if ; inline
495 : pusher-for ( quot exemplar -- quot accum )
496 [ length ] keep new-resizable [ [ push-if ] 2curry ] keep ; inline
498 : pusher ( quot -- quot accum )
499 V{ } pusher-for ; inline
501 : filter-as ( seq quot exemplar -- subseq )
502 dup [ pusher-for [ each ] dip ] curry dip like ; inline
504 : filter ( seq quot -- subseq )
505 over filter-as ; inline
507 : push-either ( elt quot accum1 accum2 -- )
508 [ keep swap ] 2dip ? push ; inline
510 : 2pusher ( quot -- quot accum1 accum2 )
511 V{ } clone V{ } clone [ [ push-either ] 3curry ] 2keep ; inline
513 : partition ( seq quot -- trueseq falseseq )
514 over [ 2pusher [ each ] 2dip ] dip [ like ] curry bi@ ; inline
516 : accumulator-for ( quot exemplar -- quot' vec )
517 [ length ] keep new-resizable [ [ push ] curry compose ] keep ; inline
519 : accumulator ( quot -- quot' vec )
520 V{ } accumulator-for ; inline
522 : produce-as ( pred quot exemplar -- seq )
523 dup [ accumulator-for [ while ] dip ] curry dip like ; inline
525 : produce ( pred quot -- seq )
526 { } produce-as ; inline
528 : follow ( obj quot -- seq )
529 [ dup ] swap [ keep ] curry produce nip ; inline
531 : each-index ( seq quot -- )
532 (each-index) each-integer ; inline
534 : interleave ( seq between quot -- )
535 pick empty? [ 3drop ] [
536 [ [ drop first-unsafe ] dip call ]
537 [ [ rest-slice ] 2dip [ bi* ] 2curry each ]
541 : map-index ( seq quot -- newseq )
542 [ dup length iota ] dip 2map ; inline
544 : reduce-index ( seq identity quot -- )
545 swapd each-index ; inline
547 : index ( obj seq -- n )
548 [ = ] with find drop ;
550 : index-from ( obj i seq -- n )
551 rot [ = ] curry find-from drop ;
553 : last-index ( obj seq -- n )
554 [ = ] with find-last drop ;
556 : last-index-from ( obj i seq -- n )
557 rot [ = ] curry find-last-from drop ;
561 : (indices) ( elt i obj accum -- )
562 [ swap [ = ] dip ] dip [ push ] 2curry when ; inline
566 : indices ( obj seq -- indices )
568 [ [ (indices) ] 2curry each-index ] keep ;
570 : nths ( indices seq -- seq' )
573 : any? ( seq quot -- ? )
574 find drop >boolean ; inline
576 : member? ( elt seq -- ? )
579 : member-eq? ( elt seq -- ? )
582 : remove ( elt seq -- newseq )
583 [ = not ] with filter ;
585 : remove-eq ( elt seq -- newseq )
586 [ eq? not ] with filter ;
588 : sift ( seq -- newseq )
591 : harvest ( seq -- newseq )
592 [ empty? not ] filter ;
594 : mismatch ( seq1 seq2 -- i )
595 [ min-length iota ] 2keep
596 [ 2nth-unsafe = not ] 2curry
601 [ -rot 2nth-unsafe <=> ] [ [ length ] compare ] if* ;
603 : sequence= ( seq1 seq2 -- ? )
604 2dup [ length ] bi@ =
605 [ mismatch not ] [ 2drop f ] if ; inline
607 ERROR: assert-sequence got expected ;
609 : assert-sequence= ( a b -- )
610 2dup sequence= [ 2drop ] [ assert-sequence ] if ;
612 : sequence-hashcode-step ( oldhash newpart -- newhash )
614 [ -2 fixnum-shift-fast ] [ 5 fixnum-shift-fast ] bi
615 fixnum+fast fixnum+fast
616 ] keep fixnum-bitxor ; inline
618 : sequence-hashcode ( n seq -- x )
619 [ 0 ] 2dip [ hashcode* sequence-hashcode-step ] with each ; inline
621 M: reversed equal? over reversed? [ sequence= ] [ 2drop f ] if ;
623 M: slice equal? over slice? [ sequence= ] [ 2drop f ] if ;
625 : move ( to from seq -- )
627 [ 3drop ] [ [ nth swap ] [ set-nth ] bi ] if ; inline
631 : (filter!) ( quot: ( elt -- ? ) store scan seq -- )
634 [ nth-unsafe pick call [ 1 + ] when ] 2keep
637 ] [ nip set-length drop ] if ; inline recursive
641 : filter! ( seq quot -- seq )
642 swap [ [ 0 0 ] dip (filter!) ] keep ; inline
644 : remove! ( elt seq -- seq )
645 [ = not ] with filter! ;
647 : remove-eq! ( elt seq -- seq )
648 [ eq? not ] with filter! ;
650 : prefix ( seq elt -- newseq )
651 over [ over length 1 + ] dip [
652 [ 0 swap set-nth-unsafe ] keep
656 : suffix ( seq elt -- newseq )
657 over [ over length 1 + ] dip [
658 [ [ over length ] dip set-nth-unsafe ] keep
662 : suffix! ( seq elt -- seq ) over push ; inline
664 : append! ( seq1 seq2 -- seq1 ) over push-all ; inline
666 : last ( seq -- elt ) [ length 1 - ] [ nth ] bi ;
668 : set-last ( elt seq -- ) [ length 1 - ] keep set-nth ;
670 : pop* ( seq -- ) [ length 1 - ] [ shorten ] bi ;
674 : move-backward ( shift from to seq -- )
678 [ [ 2over + pick ] dip move [ 1 + ] dip ] keep
682 : move-forward ( shift from to seq -- )
686 [ [ pick [ dup dup ] dip + swap ] dip move 1 - ] keep
690 : (open-slice) ( shift from to seq ? -- )
692 [ [ 1 - ] bi@ ] dip move-forward
694 [ over - ] 2dip move-backward
697 : open-slice ( shift from seq -- )
701 pick over length + over
702 [ pick 0 > [ [ length ] keep ] dip (open-slice) ] 2dip
708 : delete-slice ( from to seq -- )
709 check-slice [ over [ - ] dip ] dip open-slice ;
711 : remove-nth! ( n seq -- seq )
712 [ [ dup 1 + ] dip delete-slice ] keep ;
714 : snip ( from to seq -- head tail )
715 [ swap head ] [ swap tail ] bi-curry bi* ; inline
717 : snip-slice ( from to seq -- head tail )
718 [ swap head-slice ] [ swap tail-slice ] bi-curry bi* ; inline
720 : replace-slice ( new from to seq -- seq' )
721 snip-slice surround ;
723 : remove-nth ( n seq -- seq' )
724 [ [ { } ] dip dup 1 + ] dip replace-slice ;
727 [ length 1 - ] [ [ nth ] [ shorten ] 2bi ] bi ;
729 : exchange ( m n seq -- )
730 [ nip bounds-check 2drop ]
731 [ bounds-check 3drop ]
735 : reverse! ( seq -- seq )
737 [ length 2/ iota ] [ length ] [ ] tri
738 [ [ over - 1 - ] dip exchange-unsafe ] 2curry each
741 : reverse ( seq -- newseq )
743 dup [ length ] keep new-sequence
744 [ 0 swap copy ] keep reverse!
747 : sum-lengths ( seq -- n )
748 0 [ length + ] reduce ;
750 : concat-as ( seq exemplar -- newseq )
752 [ sum-lengths over new-resizable ] keep
754 ] if-empty swap like ;
756 : concat ( seq -- newseq )
757 [ { } ] [ dup first concat-as ] if-empty ;
761 : joined-length ( seq glue -- n )
762 [ [ sum-lengths ] [ length 1 [-] ] bi ] dip length * + ;
766 : join ( seq glue -- newseq )
767 dup empty? [ concat-as ] [
769 2dup joined-length over new-resizable [
770 [ [ push-all ] 2curry ] [ [ nip push-all ] 2curry ] 2bi
776 : padding ( seq n elt quot -- newseq )
778 [ over length [-] dup 0 = [ drop ] ] dip
779 [ <repetition> ] curry
780 ] dip compose if ; inline
782 : pad-head ( seq n elt -- padded )
783 [ swap dup append-as ] padding ;
785 : pad-tail ( seq n elt -- padded )
788 : shorter? ( seq1 seq2 -- ? ) [ length ] bi@ < ;
790 : head? ( seq begin -- ? )
794 [ nip ] [ length head-slice ] 2bi sequence=
797 : tail? ( seq end -- ? )
801 [ nip ] [ length tail-slice* ] 2bi sequence=
804 : cut-slice ( seq n -- before-slice after-slice )
805 [ head-slice ] [ tail-slice ] 2bi ;
807 : insert-nth ( elt n seq -- seq' )
808 swap cut-slice [ swap suffix ] dip append ;
810 : midpoint@ ( seq -- n ) length 2/ ; inline
812 : halves ( seq -- first-slice second-slice )
813 dup midpoint@ cut-slice ;
815 : binary-reduce ( seq start quot: ( elt1 elt2 -- newelt ) -- value )
816 #! We can't use case here since combinators depends on
818 pick length dup 0 3 between? [
822 [ [ drop first2 ] dip call ]
823 [ [ drop first3 ] dip bi@ ]
828 [ [ binary-reduce ] 2curry bi@ ] keep
830 ] if ; inline recursive
832 : cut ( seq n -- before after )
833 [ head ] [ tail ] 2bi ;
835 : cut* ( seq n -- before after )
836 [ head* ] [ tail* ] 2bi ;
840 : (start) ( subseq seq n -- subseq seq ? )
842 [ 3dup ] dip [ + swap nth-unsafe ] keep rot nth-unsafe =
847 : start* ( subseq seq n -- i )
848 pick length pick length swap - 1 + iota
849 [ (start) ] find-from
852 : start ( subseq seq -- i ) 0 start* ; inline
854 : subseq? ( subseq seq -- ? ) start >boolean ;
856 : drop-prefix ( seq1 seq2 -- slice1 slice2 )
857 2dup mismatch [ 2dup min-length ] unless*
858 [ tail-slice ] curry bi@ ;
860 : unclip ( seq -- rest first )
861 [ rest ] [ first-unsafe ] bi ;
863 : unclip-last ( seq -- butlast last )
864 [ but-last ] [ last ] bi ;
866 : unclip-slice ( seq -- rest-slice first )
867 [ rest-slice ] [ first-unsafe ] bi ; inline
869 : 2unclip-slice ( seq1 seq2 -- rest-slice1 rest-slice2 first1 first2 )
870 [ unclip-slice ] bi@ swapd ; inline
872 : map-reduce ( seq map-quot reduce-quot -- result )
873 [ [ unclip-slice ] dip [ call ] keep ] dip
874 compose reduce ; inline
876 : 2map-reduce ( seq1 seq2 map-quot reduce-quot -- result )
877 [ [ 2unclip-slice ] dip [ call ] keep ] dip
878 compose 2reduce ; inline
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
888 : map-find ( seq quot -- result elt )
889 [ find ] (map-find) ; inline
891 : map-find-last ( seq quot -- result elt )
892 [ find-last ] (map-find) ; inline
894 : unclip-last-slice ( seq -- butlast-slice last )
895 [ but-last-slice ] [ last ] bi ; inline
897 : <flat-slice> ( seq -- slice )
898 dup slice? [ { } like ] when
899 [ drop 0 ] [ length ] [ ] tri <slice> ;
904 : (trim-head) ( seq quot -- seq n )
905 over [ [ not ] compose find drop ] dip
906 [ length or ] keep swap ; inline
908 : (trim-tail) ( seq quot -- seq n )
909 over [ [ not ] compose find-last drop ?1+ ] dip
914 : trim-head-slice ( seq quot -- slice )
915 (trim-head) tail-slice ; inline
917 : trim-head ( seq quot -- newseq )
918 (trim-head) tail ; inline
920 : trim-tail-slice ( seq quot -- slice )
921 (trim-tail) head-slice ; inline
923 : trim-tail ( seq quot -- newseq )
924 (trim-tail) head ; inline
926 : trim-slice ( seq quot -- slice )
927 [ trim-head-slice ] [ trim-tail-slice ] bi ; inline
929 : trim ( seq quot -- newseq )
930 [ trim-slice ] [ drop ] 2bi like ; inline
932 GENERIC: sum ( seq -- n )
933 M: object sum 0 [ + ] binary-reduce ; inline
935 : product ( seq -- n ) 1 [ * ] binary-reduce ;
937 : infimum ( seq -- n ) [ ] [ min ] map-reduce ;
939 : supremum ( seq -- n ) [ ] [ max ] map-reduce ;
941 : map-sum ( seq quot -- n )
942 [ 0 ] 2dip [ dip + ] curry [ swap ] prepose each ; inline
944 : count ( seq quot -- n ) [ 1 0 ? ] compose map-sum ; inline
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.
952 : generic-flip ( matrix -- newmatrix )
953 [ dup first length [ length min ] reduce iota ] keep
954 [ [ nth-unsafe ] with { } map-as ] curry { } map-as ; inline
958 : array-length ( array -- len )
959 { array } declare length>> ; inline
961 : array-flip ( matrix -- newmatrix )
963 [ dup first array-length [ array-length min ] reduce iota ] keep
964 [ [ { array } declare array-nth ] with { } map-as ] curry { } map-as ;
968 : flip ( matrix -- newmatrix )
972 [ array-flip ] [ generic-flip ] if
973 ] [ generic-flip ] if