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 ;
23 GENERIC: lengthen ( n seq -- )
24 GENERIC: shorten ( n seq -- )
26 M: sequence lengthen 2dup length > [ set-length ] [ 2drop ] if ;
28 M: sequence shorten 2dup length < [ set-length ] [ 2drop ] if ;
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 ;
86 M: sequence set-nth bounds-check set-nth-unsafe ;
88 M: sequence nth-unsafe nth ;
89 M: sequence set-nth-unsafe set-nth ;
91 ! The f object supports the sequence protocol trivially
94 M: f like drop [ f ] when-empty ;
96 INSTANCE: f immutable-sequence
98 ! Integers support the sequence protocol
100 M: integer nth-unsafe drop ;
102 INSTANCE: integer immutable-sequence
106 ! In the future, this will replace integer sequences
107 TUPLE: iota { n integer read-only } ;
109 : iota ( n -- iota ) \ iota boa ; inline
114 M: iota nth-unsafe drop ;
116 INSTANCE: iota immutable-sequence
118 : first-unsafe ( seq -- first )
119 0 swap nth-unsafe ; inline
121 : first2-unsafe ( seq -- first second )
122 [ first-unsafe ] [ 1 swap nth-unsafe ] bi ; inline
124 : first3-unsafe ( seq -- first second third )
125 [ first2-unsafe ] [ 2 swap nth-unsafe ] bi ; inline
127 : first4-unsafe ( seq -- first second third fourth )
128 [ first3-unsafe ] [ 3 swap nth-unsafe ] bi ; inline
130 : exchange-unsafe ( m n seq -- )
131 [ tuck [ nth-unsafe ] 2bi@ ]
132 [ tuck [ set-nth-unsafe ] 2bi@ ] 3bi ; inline
134 : (head) ( seq n -- from to seq ) [ 0 ] 2dip swap ; inline
136 : (tail) ( seq n -- from to seq ) swap [ length ] keep ; inline
138 : from-end ( seq n -- seq n' ) [ dup length ] dip - ; inline
140 : (2sequence) ( obj1 obj2 seq -- seq )
141 [ 1 swap set-nth-unsafe ] keep
142 [ 0 swap set-nth-unsafe ] keep ; inline
144 : (3sequence) ( obj1 obj2 obj3 seq -- seq )
145 [ 2 swap set-nth-unsafe ] keep
148 : (4sequence) ( obj1 obj2 obj3 obj4 seq -- seq )
149 [ 3 swap set-nth-unsafe ] keep
154 : 2sequence ( obj1 obj2 exemplar -- seq )
155 2 swap [ (2sequence) ] new-like ; inline
157 : 3sequence ( obj1 obj2 obj3 exemplar -- seq )
158 3 swap [ (3sequence) ] new-like ; inline
160 : 4sequence ( obj1 obj2 obj3 obj4 exemplar -- seq )
161 4 swap [ (4sequence) ] new-like ; inline
163 : first2 ( seq -- first second )
164 1 swap bounds-check nip first2-unsafe ; flushable
166 : first3 ( seq -- first second third )
167 2 swap bounds-check nip first3-unsafe ; flushable
169 : first4 ( seq -- first second third fourth )
170 3 swap bounds-check nip first4-unsafe ; flushable
172 : ?nth ( n seq -- elt/f )
173 2dup bounds-check? [ nth-unsafe ] [ 2drop f ] if ; flushable
175 MIXIN: virtual-sequence
176 GENERIC: virtual-seq ( seq -- seq' )
177 GENERIC: virtual@ ( n seq -- n' seq' )
179 M: virtual-sequence nth virtual@ nth ;
180 M: virtual-sequence set-nth virtual@ set-nth ;
181 M: virtual-sequence nth-unsafe virtual@ nth-unsafe ;
182 M: virtual-sequence set-nth-unsafe virtual@ set-nth-unsafe ;
183 M: virtual-sequence like virtual-seq like ;
184 M: virtual-sequence new-sequence virtual-seq new-sequence ;
186 INSTANCE: virtual-sequence sequence
188 ! A reversal of an underlying sequence.
189 TUPLE: reversed { seq read-only } ;
191 C: <reversed> reversed
193 M: reversed virtual-seq seq>> ;
195 M: reversed virtual@ seq>> [ length swap - 1- ] keep ;
197 M: reversed length seq>> length ;
199 INSTANCE: reversed virtual-sequence
201 ! A slice of another sequence.
207 : collapse-slice ( m n slice -- m' n' seq )
208 [ from>> ] [ seq>> ] bi [ tuck [ + ] 2bi@ ] dip ; inline
210 ERROR: slice-error from to seq reason ;
212 : check-slice ( from to seq -- from to seq )
213 pick 0 < [ "start < 0" slice-error ] when
214 dup length pick < [ "end > sequence" slice-error ] when
215 2over > [ "start > end" slice-error ] when ; inline
217 : <slice> ( from to seq -- slice )
218 dup slice? [ collapse-slice ] when
222 M: slice virtual-seq seq>> ;
224 M: slice virtual@ [ from>> + ] [ seq>> ] bi ;
226 M: slice length [ to>> ] [ from>> ] bi - ;
228 : short ( seq n -- seq n' ) over length min ; inline
230 : head-slice ( seq n -- slice ) (head) <slice> ; inline
232 : tail-slice ( seq n -- slice ) (tail) <slice> ; inline
234 : rest-slice ( seq -- slice ) 1 tail-slice ; inline
236 : head-slice* ( seq n -- slice ) from-end head-slice ; inline
238 : tail-slice* ( seq n -- slice ) from-end tail-slice ; inline
240 : but-last-slice ( seq -- slice ) 1 head-slice* ; inline
242 INSTANCE: slice virtual-sequence
244 ! One element repeated many times
245 TUPLE: repetition { len read-only } { elt read-only } ;
247 C: <repetition> repetition
249 M: repetition length len>> ;
250 M: repetition nth-unsafe nip elt>> ;
252 INSTANCE: repetition immutable-sequence
256 : check-length ( n -- n )
258 dup integer? [ "length not an integer" throw ] unless ; inline
260 : ((copy)) ( dst i src j n -- dst i src j n )
262 + swap nth-unsafe -roll [
263 + swap set-nth-unsafe
267 : (copy) ( dst i src j n -- dst )
268 dup 0 <= [ 2drop 2drop ] [ 1- ((copy)) (copy) ] if ;
271 : prepare-subseq ( from to seq -- dst i src j n )
272 #! The check-length call forces partial dispatch
273 [ [ swap - ] dip new-sequence dup 0 ] 3keep
274 -rot drop roll length check-length ; inline
276 : check-copy ( src n dst -- )
277 over 0 < [ bounds-error ] when
278 [ swap length + ] dip lengthen ; inline
282 : subseq ( from to seq -- subseq )
283 [ check-slice prepare-subseq (copy) ] [ like ] bi ;
285 : head ( seq n -- headseq ) (head) subseq ;
287 : tail ( seq n -- tailseq ) (tail) subseq ;
289 : rest ( seq -- tailseq ) 1 tail ;
291 : head* ( seq n -- headseq ) from-end head ;
293 : tail* ( seq n -- tailseq ) from-end tail ;
295 : but-last ( seq -- headseq ) 1 head* ;
297 : copy ( src i dst -- )
298 #! The check-length call forces partial dispatch
299 pick length check-length [ 3dup check-copy spin 0 ] dip
302 M: sequence clone-like
303 [ dup length ] dip new-sequence [ 0 swap copy ] keep ;
305 M: immutable-sequence clone-like like ;
307 : push-all ( src dest -- ) [ length ] [ copy ] bi ;
311 : (append) ( seq1 seq2 accum -- accum )
312 [ [ over length ] dip copy ]
318 : append-as ( seq1 seq2 exemplar -- newseq )
319 [ over length over length + ] dip
320 [ (append) ] new-like ; inline
322 : 3append-as ( seq1 seq2 seq3 exemplar -- newseq )
323 [ pick length pick length pick length + + ] dip [
324 [ [ pick length pick length + ] dip copy ]
328 : append ( seq1 seq2 -- newseq ) over append-as ;
330 : prepend ( seq1 seq2 -- newseq ) swap append ; inline
332 : 3append ( seq1 seq2 seq3 -- newseq ) pick 3append-as ;
334 : surround ( seq1 seq2 seq3 -- newseq ) swapd 3append ; inline
336 : glue ( seq1 seq2 seq3 -- newseq ) swap 3append ; inline
338 : change-nth ( i seq quot -- )
339 [ [ nth ] dip call ] 3keep drop set-nth ; inline
341 : min-length ( seq1 seq2 -- n ) [ length ] bi@ min ; inline
343 : max-length ( seq1 seq2 -- n ) [ length ] bi@ max ; inline
347 : (each) ( seq quot -- n quot' )
348 [ [ length ] keep [ nth-unsafe ] curry ] dip compose ; inline
350 : (collect) ( quot into -- quot' )
351 [ [ keep ] dip set-nth-unsafe ] 2curry ; inline
353 : collect ( n quot into -- )
354 (collect) each-integer ; inline
356 : map-into ( seq quot into -- )
357 [ (each) ] dip collect ; inline
359 : 2nth-unsafe ( n seq1 seq2 -- elt1 elt2 )
360 [ over ] dip [ nth-unsafe ] 2bi@ ; inline
362 : (2each) ( seq1 seq2 quot -- n quot' )
365 [ 2nth-unsafe ] 2curry
366 ] dip compose ; inline
368 : 3nth-unsafe ( n seq1 seq2 seq3 -- elt1 elt2 elt3 )
369 [ over ] 2dip [ over ] dip [ nth-unsafe ] 2tri@ ; inline
371 : (3each) ( seq1 seq2 seq3 quot -- n quot' )
373 [ [ length ] tri@ min min ] 3keep
374 [ 3nth-unsafe ] 3curry
375 ] dip compose ; inline
377 : finish-find ( i seq -- i elt )
378 over [ dupd nth-unsafe ] [ drop f ] if ; inline
380 : (find) ( seq quot quot' -- i elt )
381 pick [ [ (each) ] dip call ] dip finish-find ; inline
383 : (find-from) ( n seq quot quot' -- i elt )
384 [ 2dup bounds-check? ] 2dip
389 : (interleave) ( n elt between quot -- )
390 roll 0 = [ nip ] [ swapd 2slip ] if call ; inline
394 : each ( seq quot -- )
395 (each) each-integer ; inline
397 : reduce ( seq identity quot -- result )
400 : map-as ( seq quot exemplar -- newseq )
401 [ over length ] dip [ [ map-into ] keep ] new-like ; inline
403 : map ( seq quot -- newseq )
406 : replicate ( seq quot -- newseq )
407 [ drop ] prepose map ; inline
409 : replicate-as ( seq quot exemplar -- newseq )
410 [ [ drop ] prepose ] dip map-as ; inline
412 : change-each ( seq quot -- )
413 over map-into ; inline
415 : accumulate ( seq identity quot -- final newseq )
416 swapd [ pick slip ] curry map ; inline
418 : 2each ( seq1 seq2 quot -- )
419 (2each) each-integer ; inline
421 : 2reverse-each ( seq1 seq2 quot -- )
422 [ [ <reversed> ] bi@ ] dip 2each ; inline
424 : 2reduce ( seq1 seq2 identity quot -- result )
425 [ -rot ] dip 2each ; inline
427 : 2map-as ( seq1 seq2 quot exemplar -- newseq )
428 [ (2each) ] dip map-as ; inline
430 : 2map ( seq1 seq2 quot -- newseq )
431 pick 2map-as ; inline
433 : 2all? ( seq1 seq2 quot -- ? )
434 (2each) all-integers? ; inline
436 : 3each ( seq1 seq2 seq3 quot -- )
437 (3each) each ; inline
439 : 3map-as ( seq1 seq2 seq3 quot exemplar -- newseq )
440 [ (3each) ] dip map-as ; inline
442 : 3map ( seq1 seq2 seq3 quot -- newseq )
443 [ pick ] dip swap 3map-as ; inline
445 : find-from ( n seq quot -- i elt )
446 [ (find-integer) ] (find-from) ; inline
448 : find ( seq quot -- i elt )
449 [ find-integer ] (find) ; inline
451 : find-last-from ( n seq quot -- i elt )
452 [ nip find-last-integer ] (find-from) ; inline
454 : find-last ( seq quot -- i elt )
455 [ [ 1- ] dip find-last-integer ] (find) ; inline
457 : all? ( seq quot -- ? )
458 (each) all-integers? ; inline
460 : push-if ( elt quot accum -- )
461 [ keep ] dip rot [ push ] [ 2drop ] if ; inline
463 : pusher ( quot -- quot accum )
464 V{ } clone [ [ push-if ] 2curry ] keep ; inline
466 : filter ( seq quot -- subseq )
467 over [ pusher [ each ] dip ] dip like ; inline
469 : push-either ( elt quot accum1 accum2 -- )
470 [ keep swap ] 2dip ? push ; inline
472 : 2pusher ( quot -- quot accum1 accum2 )
473 V{ } clone V{ } clone [ [ push-either ] 3curry ] 2keep ; inline
475 : partition ( seq quot -- trueseq falseseq )
476 over [ 2pusher [ each ] 2dip ] dip tuck [ like ] 2bi@ ; inline
478 : interleave ( seq between quot -- )
479 [ (interleave) ] 2curry [ [ length ] keep ] dip 2each ; inline
481 : accumulator ( quot -- quot' vec )
482 V{ } clone [ [ push ] curry compose ] keep ; inline
484 : produce-as ( pred quot tail exemplar -- seq )
485 [ swap accumulator [ swap while ] dip ] dip like ; inline
487 : produce ( pred quot tail -- seq )
488 { } produce-as ; inline
490 : follow ( obj quot -- seq )
491 [ dup ] swap [ keep ] curry [ ] produce nip ; inline
493 : prepare-index ( seq quot -- seq n quot )
494 [ dup length ] dip ; inline
496 : each-index ( seq quot -- )
497 prepare-index 2each ; inline
499 : map-index ( seq quot -- )
500 prepare-index 2map ; inline
502 : reduce-index ( seq identity quot -- )
503 swapd each-index ; inline
505 : index ( obj seq -- n )
506 [ = ] with find drop ;
508 : index-from ( obj i seq -- n )
509 rot [ = ] curry find-from drop ;
511 : last-index ( obj seq -- n )
512 [ = ] with find-last drop ;
514 : last-index-from ( obj i seq -- n )
515 rot [ = ] curry find-last-from drop ;
517 : (indices) ( elt i obj accum -- )
518 [ swap [ = ] dip ] dip [ push ] 2curry when ; inline
520 : indices ( obj seq -- indices )
522 [ [ (indices) ] 2curry each-index ] keep ;
524 : nths ( indices seq -- seq' )
527 : contains? ( seq quot -- ? )
528 find drop >boolean ; inline
530 : member? ( elt seq -- ? )
531 [ = ] with contains? ;
533 : memq? ( elt seq -- ? )
534 [ eq? ] with contains? ;
536 : remove ( elt seq -- newseq )
537 [ = not ] with filter ;
539 : remq ( elt seq -- newseq )
540 [ eq? not ] with filter ;
542 : sift ( seq -- newseq )
545 : harvest ( seq -- newseq )
546 [ empty? not ] filter ;
548 : mismatch ( seq1 seq2 -- i )
550 [ 2nth-unsafe = not ] 2curry
555 [ -rot 2nth-unsafe <=> ] [ [ length ] compare ] if* ;
557 : sequence= ( seq1 seq2 -- ? )
558 2dup [ length ] bi@ =
559 [ mismatch not ] [ 2drop f ] if ; inline
561 : sequence-hashcode-step ( oldhash newpart -- newhash )
563 [ -2 fixnum-shift-fast ] [ 5 fixnum-shift-fast ] bi
564 fixnum+fast fixnum+fast
565 ] keep fixnum-bitxor ; inline
567 : sequence-hashcode ( n seq -- x )
568 [ 0 ] 2dip [ hashcode* sequence-hashcode-step ] with each ; inline
570 M: reversed equal? over reversed? [ sequence= ] [ 2drop f ] if ;
572 M: slice equal? over slice? [ sequence= ] [ 2drop f ] if ;
574 : move ( to from seq -- )
576 [ 3drop ] [ [ nth swap ] [ set-nth ] bi ] if ; inline
580 : (filter-here) ( quot: ( elt -- ? ) store scan seq -- )
583 [ nth-unsafe pick call [ 1+ ] when ] 2keep
586 ] [ nip set-length drop ] if ; inline recursive
590 : filter-here ( seq quot -- )
591 swap [ 0 0 ] dip (filter-here) ; inline
593 : delete ( elt seq -- )
594 [ = not ] with filter-here ;
596 : delq ( elt seq -- )
597 [ eq? not ] with filter-here ;
599 : prefix ( seq elt -- newseq )
600 over [ over length 1+ ] dip [
601 [ 0 swap set-nth-unsafe ] keep
605 : suffix ( seq elt -- newseq )
606 over [ over length 1+ ] dip [
607 [ [ over length ] dip set-nth-unsafe ] keep
611 : peek ( seq -- elt ) [ length 1- ] [ nth ] bi ;
613 : pop* ( seq -- ) [ length 1- ] [ shorten ] bi ;
617 : move-backward ( shift from to seq -- )
621 [ [ 2over + pick ] dip move [ 1+ ] dip ] keep
625 : move-forward ( shift from to seq -- )
629 [ [ pick [ dup dup ] dip + swap ] dip move 1- ] keep
633 : (open-slice) ( shift from to seq ? -- )
635 [ [ 1- ] bi@ ] dip move-forward
637 [ over - ] 2dip move-backward
642 : open-slice ( shift from seq -- )
646 pick over length + over
647 [ pick 0 > [ [ length ] keep ] dip (open-slice) ] 2dip
651 : delete-slice ( from to seq -- )
652 check-slice [ over [ - ] dip ] dip open-slice ;
654 : delete-nth ( n seq -- )
655 [ dup 1+ ] dip delete-slice ;
657 : replace-slice ( new from to seq -- )
658 [ [ [ dup pick length + ] dip - over ] dip open-slice ] keep
661 : remove-nth ( n seq -- seq' )
662 [ swap head-slice ] [ swap 1+ tail-slice ] 2bi append ;
665 [ length 1- ] [ [ nth ] [ shorten ] 2bi ] bi ;
667 : exchange ( m n seq -- )
668 pick over bounds-check 2drop 2dup bounds-check 2drop
671 : reverse-here ( seq -- )
674 tuck - 1- rot exchange-unsafe
677 : reverse ( seq -- newseq )
679 dup [ length ] keep new-sequence
681 [ reverse-here ] keep
684 : sum-lengths ( seq -- n )
685 0 [ length + ] reduce ;
687 : concat ( seq -- newseq )
690 [ first new-resizable ] keep
691 [ [ over push-all ] each ] keep
697 : joined-length ( seq glue -- n )
698 [ [ sum-lengths ] [ length 1 [-] ] bi ] dip length * + ;
702 : join ( seq glue -- newseq )
704 2dup joined-length over new-resizable spin
705 [ dup pick push-all ] [ pick push-all ] interleave drop
708 : padding ( seq n elt quot -- newseq )
710 [ over length [-] dup 0 = [ drop ] ] dip
711 [ <repetition> ] curry
712 ] dip compose if ; inline
714 : pad-left ( seq n elt -- padded )
715 [ swap dup append-as ] padding ;
717 : pad-right ( seq n elt -- padded )
720 : shorter? ( seq1 seq2 -- ? ) [ length ] bi@ < ;
722 : head? ( seq begin -- ? )
726 [ nip ] [ length head-slice ] 2bi sequence=
729 : tail? ( seq end -- ? )
733 [ nip ] [ length tail-slice* ] 2bi sequence=
736 : cut-slice ( seq n -- before-slice after-slice )
737 [ head-slice ] [ tail-slice ] 2bi ;
739 : insert-nth ( elt n seq -- seq' )
740 swap cut-slice [ swap suffix ] dip append ;
742 : midpoint@ ( seq -- n ) length 2/ ; inline
744 : halves ( seq -- first-slice second-slice )
745 dup midpoint@ cut-slice ;
747 : binary-reduce ( seq start quot: ( elt1 elt2 -- newelt ) -- value )
748 #! We can't use case here since combinators depends on
750 pick length dup 0 3 between? [
754 [ [ drop first2 ] dip call ]
755 [ [ drop first3 ] dip bi@ ]
760 [ [ binary-reduce ] 2curry bi@ ] keep
762 ] if ; inline recursive
764 : cut ( seq n -- before after )
765 [ head ] [ tail ] 2bi ;
767 : cut* ( seq n -- before after )
768 [ head* ] [ tail* ] 2bi ;
772 : (start) ( subseq seq n -- subseq seq ? )
774 [ 3dup ] dip [ + swap nth-unsafe ] keep rot nth-unsafe =
779 : start* ( subseq seq n -- i )
780 pick length pick length swap - 1+
781 [ (start) ] find-from
784 : start ( subseq seq -- i ) 0 start* ; inline
786 : subseq? ( subseq seq -- ? ) start >boolean ;
788 : drop-prefix ( seq1 seq2 -- slice1 slice2 )
789 2dup mismatch [ 2dup min-length ] unless*
790 tuck [ tail-slice ] 2bi@ ;
792 : unclip ( seq -- rest first )
793 [ rest ] [ first-unsafe ] bi ;
795 : unclip-last ( seq -- butlast last )
796 [ but-last ] [ peek ] bi ;
798 : unclip-slice ( seq -- rest-slice first )
799 [ rest-slice ] [ first-unsafe ] bi ; inline
801 : 2unclip-slice ( seq1 seq2 -- rest-slice1 rest-slice2 first1 first2 )
802 [ unclip-slice ] bi@ swapd ; inline
804 : map-reduce ( seq map-quot reduce-quot -- result )
805 [ [ unclip-slice ] dip [ call ] keep ] dip
806 compose reduce ; inline
808 : 2map-reduce ( seq1 seq2 map-quot reduce-quot -- result )
809 [ [ 2unclip-slice ] dip [ call ] keep ] dip
810 compose 2reduce ; inline
812 : unclip-last-slice ( seq -- butlast-slice last )
813 [ but-last-slice ] [ peek ] bi ; inline
815 : <flat-slice> ( seq -- slice )
816 dup slice? [ { } like ] when 0 over length rot <slice> ;
819 : trim-left-slice ( seq quot -- slice )
820 over [ [ not ] compose find drop ] dip swap
821 [ tail-slice ] [ dup length tail-slice ] if* ; inline
823 : trim-left ( seq quot -- newseq )
824 over [ trim-left-slice ] dip like ; inline
826 : trim-right-slice ( seq quot -- slice )
827 over [ [ not ] compose find-last drop ] dip swap
828 [ 1+ head-slice ] [ 0 head-slice ] if* ; inline
830 : trim-right ( seq quot -- newseq )
831 over [ trim-right-slice ] dip like ; inline
833 : trim-slice ( seq quot -- slice )
834 [ trim-left-slice ] [ trim-right-slice ] bi ; inline
836 : trim ( seq quot -- newseq )
837 over [ trim-slice ] dip like ; inline
839 : sum ( seq -- n ) 0 [ + ] binary-reduce ;
841 : product ( seq -- n ) 1 [ * ] binary-reduce ;
843 : infimum ( seq -- n ) dup first [ min ] reduce ;
845 : supremum ( seq -- n ) dup first [ max ] reduce ;
847 : sigma ( seq quot -- n ) [ 0 ] 2dip [ rot slip + ] curry each ; inline
849 : count ( seq quot -- n ) [ 1 0 ? ] compose sigma ; inline
851 ! We hand-optimize flip to such a degree because type hints
852 ! cannot express that an array is an array of arrays yet, and
853 ! this word happens to be performance-critical since the compiler
854 ! itself uses it. Optimizing it like this reduced compile time.
857 : generic-flip ( matrix -- newmatrix )
858 [ dup first length [ length min ] reduce ] keep
859 [ [ nth-unsafe ] with { } map-as ] curry { } map-as ; inline
863 : array-length ( array -- len )
864 { array } declare length>> ; inline
866 : array-flip ( matrix -- newmatrix )
868 [ dup first array-length [ array-length min ] reduce ] keep
869 [ [ array-nth ] with { } map-as ] curry { } map-as ;
873 : flip ( matrix -- newmatrix )
877 [ array-flip ] [ generic-flip ] if
878 ] [ generic-flip ] if