1 ! Copyright (C) 2005, 2008 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
105 [ 0 swap nth-unsafe 1 ] [ nth-unsafe ] bi ; inline
108 [ first2-unsafe 2 ] [ nth-unsafe ] bi ; inline
111 [ first3-unsafe 3 ] [ nth-unsafe ] bi ; inline
113 : exchange-unsafe ( m n seq -- )
114 [ tuck [ nth-unsafe ] 2bi@ ]
115 [ tuck [ set-nth-unsafe ] 2bi@ ] 3bi ; inline
117 : (head) ( seq n -- from to seq ) 0 spin ; inline
119 : (tail) ( seq n -- from to seq ) over length rot ; inline
121 : from-end [ dup length ] dip - ; inline
124 tuck 1 swap set-nth-unsafe
125 tuck 0 swap set-nth-unsafe ; inline
128 tuck 2 swap set-nth-unsafe
132 tuck 3 swap set-nth-unsafe
137 : 2sequence ( obj1 obj2 exemplar -- seq )
138 2 swap [ (2sequence) ] new-like ; inline
140 : 3sequence ( obj1 obj2 obj3 exemplar -- seq )
141 3 swap [ (3sequence) ] new-like ; inline
143 : 4sequence ( obj1 obj2 obj3 obj4 exemplar -- seq )
144 4 swap [ (4sequence) ] new-like ; inline
146 : first2 ( seq -- first second )
147 1 swap bounds-check nip first2-unsafe ; flushable
149 : first3 ( seq -- first second third )
150 2 swap bounds-check nip first3-unsafe ; flushable
152 : first4 ( seq -- first second third fourth )
153 3 swap bounds-check nip first4-unsafe ; flushable
155 : ?nth ( n seq -- elt/f )
156 2dup bounds-check? [ nth-unsafe ] [ 2drop f ] if ; flushable
158 MIXIN: virtual-sequence
159 GENERIC: virtual-seq ( seq -- seq' )
160 GENERIC: virtual@ ( n seq -- n' seq' )
162 M: virtual-sequence nth virtual@ nth ;
163 M: virtual-sequence set-nth virtual@ set-nth ;
164 M: virtual-sequence nth-unsafe virtual@ nth-unsafe ;
165 M: virtual-sequence set-nth-unsafe virtual@ set-nth-unsafe ;
166 M: virtual-sequence like virtual-seq like ;
167 M: virtual-sequence new-sequence virtual-seq new-sequence ;
169 INSTANCE: virtual-sequence sequence
171 ! A reversal of an underlying sequence.
172 TUPLE: reversed { seq read-only } ;
174 C: <reversed> reversed
176 M: reversed virtual-seq seq>> ;
178 M: reversed virtual@ seq>> [ length swap - 1- ] keep ;
180 M: reversed length seq>> length ;
182 INSTANCE: reversed virtual-sequence
184 ! A slice of another sequence.
190 : collapse-slice ( m n slice -- m' n' seq )
191 [ from>> ] [ seq>> ] bi [ tuck [ + ] 2bi@ ] dip ; inline
193 ERROR: slice-error from to seq reason ;
195 : check-slice ( from to seq -- from to seq )
196 pick 0 < [ "start < 0" slice-error ] when
197 dup length pick < [ "end > sequence" slice-error ] when
198 2over > [ "start > end" slice-error ] when ; inline
200 : <slice> ( from to seq -- slice )
201 dup slice? [ collapse-slice ] when
205 M: slice virtual-seq seq>> ;
207 M: slice virtual@ [ from>> + ] [ seq>> ] bi ;
209 M: slice length [ to>> ] [ from>> ] bi - ;
211 : short ( seq n -- seq n' ) over length min ; inline
213 : head-slice ( seq n -- slice ) (head) <slice> ; inline
215 : tail-slice ( seq n -- slice ) (tail) <slice> ; inline
217 : rest-slice ( seq -- slice ) 1 tail-slice ; inline
219 : head-slice* ( seq n -- slice ) from-end head-slice ; inline
221 : tail-slice* ( seq n -- slice ) from-end tail-slice ; inline
223 : but-last-slice ( seq -- slice ) 1 head-slice* ; inline
225 INSTANCE: slice virtual-sequence
227 ! One element repeated many times
228 TUPLE: repetition { len read-only } { elt read-only } ;
230 C: <repetition> repetition
232 M: repetition length len>> ;
233 M: repetition nth-unsafe nip elt>> ;
235 INSTANCE: repetition immutable-sequence
239 : check-length ( n -- n )
241 dup integer? [ "length not an integer" throw ] unless ; inline
243 : ((copy)) ( dst i src j n -- dst i src j n )
245 + swap nth-unsafe -roll [
246 + swap set-nth-unsafe
250 : (copy) ( dst i src j n -- dst )
251 dup 0 <= [ 2drop 2drop ] [ 1- ((copy)) (copy) ] if ;
254 : prepare-subseq ( from to seq -- dst i src j n )
255 #! The check-length call forces partial dispatch
256 [ [ swap - ] dip new-sequence dup 0 ] 3keep
257 -rot drop roll length check-length ; inline
259 : check-copy ( src n dst -- )
260 over 0 < [ bounds-error ] when
261 [ swap length + ] dip lengthen ; inline
265 : subseq ( from to seq -- subseq )
266 [ check-slice prepare-subseq (copy) ] [ like ] bi ;
268 : head ( seq n -- headseq ) (head) subseq ;
270 : tail ( seq n -- tailseq ) (tail) subseq ;
272 : rest ( seq -- tailseq ) 1 tail ;
274 : head* ( seq n -- headseq ) from-end head ;
276 : tail* ( seq n -- tailseq ) from-end tail ;
278 : but-last ( seq -- headseq ) 1 head* ;
280 : copy ( src i dst -- )
281 #! The check-length call forces partial dispatch
282 pick length check-length [ 3dup check-copy spin 0 ] dip
285 M: sequence clone-like
286 [ dup length ] dip new-sequence [ 0 swap copy ] keep ;
288 M: immutable-sequence clone-like like ;
290 : push-all ( src dest -- ) [ length ] [ copy ] bi ;
294 : (append) ( seq1 seq2 accum -- accum )
295 [ [ over length ] dip copy ]
301 : append-as ( seq1 seq2 exemplar -- newseq )
302 [ over length over length + ] dip
303 [ (append) ] new-like ; inline
305 : 3append-as ( seq1 seq2 seq3 exemplar -- newseq )
306 [ pick length pick length pick length + + ] dip [
307 [ [ pick length pick length + ] dip copy ]
311 : append ( seq1 seq2 -- newseq ) over append-as ;
313 : prepend ( seq1 seq2 -- newseq ) swap append ; inline
315 : 3append ( seq1 seq2 seq3 -- newseq ) pick 3append-as ;
317 : change-nth ( i seq quot -- )
318 [ [ nth ] dip call ] 3keep drop set-nth ; inline
320 : min-length ( seq1 seq2 -- n ) [ length ] bi@ min ; inline
322 : max-length ( seq1 seq2 -- n ) [ length ] bi@ max ; inline
326 : (each) ( seq quot -- n quot' )
327 [ dup length swap [ nth-unsafe ] curry ] dip compose ; inline
329 : (collect) ( quot into -- quot' )
330 [ [ keep ] dip set-nth-unsafe ] 2curry ; inline
332 : collect ( n quot into -- )
333 (collect) each-integer ; inline
335 : map-into ( seq quot into -- )
336 [ (each) ] dip collect ; inline
338 : 2nth-unsafe ( n seq1 seq2 -- elt1 elt2 )
339 [ over ] dip nth-unsafe [ nth-unsafe ] dip ; inline
341 : (2each) ( seq1 seq2 quot -- n quot' )
342 [ [ min-length ] 2keep ] dip
343 [ [ 2nth-unsafe ] dip call ] 3curry ; inline
345 : 2map-into ( seq1 seq2 quot into -- newseq )
346 [ (2each) ] dip collect ; inline
348 : finish-find ( i seq -- i elt )
349 over [ dupd nth-unsafe ] [ drop f ] if ; inline
351 : (find) ( seq quot quot' -- i elt )
352 pick [ [ (each) ] dip call ] dip finish-find ; inline
354 : (find-from) ( n seq quot quot' -- i elt )
355 [ 2dup bounds-check? ] 2dip
360 : (monotonic) ( seq quot -- ? )
361 [ 2dup nth-unsafe rot 1+ rot nth-unsafe ]
362 prepose curry ; inline
364 : (interleave) ( n elt between quot -- )
365 roll 0 = [ nip ] [ swapd 2slip ] if call ; inline
369 : each ( seq quot -- )
370 (each) each-integer ; inline
372 : reduce ( seq identity quot -- result )
375 : map-as ( seq quot exemplar -- newseq )
376 [ over length ] dip [ [ map-into ] keep ] new-like ; inline
378 : map ( seq quot -- newseq )
381 : replicate ( seq quot -- newseq )
382 [ drop ] prepose map ; inline
384 : replicate-as ( seq quot exemplar -- newseq )
385 [ [ drop ] prepose ] dip map-as ; inline
387 : change-each ( seq quot -- )
388 over map-into ; inline
390 : accumulate ( seq identity quot -- final newseq )
391 swapd [ pick slip ] curry map ; inline
393 : 2each ( seq1 seq2 quot -- )
394 (2each) each-integer ; inline
396 : 2reverse-each ( seq1 seq2 quot -- )
397 [ [ <reversed> ] bi@ ] dip 2each ; inline
399 : 2reduce ( seq1 seq2 identity quot -- result )
400 [ -rot ] dip 2each ; inline
402 : 2map-as ( seq1 seq2 quot exemplar -- newseq )
403 [ 2over min-length ] dip
404 [ [ 2map-into ] keep ] new-like ; inline
406 : 2map ( seq1 seq2 quot -- newseq )
407 pick 2map-as ; inline
409 : 2change-each ( seq1 seq2 quot -- )
410 pick 2map-into ; inline
412 : 2all? ( seq1 seq2 quot -- ? )
413 (2each) all-integers? ; inline
415 : find-from ( n seq quot -- i elt )
416 [ (find-integer) ] (find-from) ; inline
418 : find ( seq quot -- i elt )
419 [ find-integer ] (find) ; inline
421 : find-last-from ( n seq quot -- i elt )
422 [ nip find-last-integer ] (find-from) ; inline
424 : find-last ( seq quot -- i elt )
425 [ [ 1- ] dip find-last-integer ] (find) ; inline
427 : all? ( seq quot -- ? )
428 (each) all-integers? ; inline
430 : push-if ( elt quot accum -- )
431 [ keep ] dip rot [ push ] [ 2drop ] if ; inline
433 : pusher ( quot -- quot accum )
434 V{ } clone [ [ push-if ] 2curry ] keep ; inline
436 : filter ( seq quot -- subseq )
437 over [ pusher [ each ] dip ] dip like ; inline
439 : push-either ( elt quot accum1 accum2 -- )
440 [ keep swap ] 2dip ? push ; inline
442 : 2pusher ( quot -- quot accum1 accum2 )
443 V{ } clone V{ } clone [ [ push-either ] 3curry ] 2keep ; inline
445 : partition ( seq quot -- trueseq falseseq )
446 over [ 2pusher [ each ] 2dip ] dip tuck [ like ] 2bi@ ; inline
448 : monotonic? ( seq quot -- ? )
449 [ dup length 1- swap ] dip (monotonic) all? ; inline
451 : interleave ( seq between quot -- )
452 [ (interleave) ] 2curry [ dup length swap ] dip 2each ; inline
454 : accumulator ( quot -- quot' vec )
455 V{ } clone [ [ push ] curry compose ] keep ; inline
457 : produce-as ( pred quot tail exemplar -- seq )
458 [ swap accumulator [ swap while ] dip ] dip like ; inline
460 : produce ( pred quot tail -- seq )
461 { } produce-as ; inline
463 : follow ( obj quot -- seq )
464 [ dup ] swap [ keep ] curry [ ] produce nip ; inline
466 : prepare-index ( seq quot -- seq n quot )
467 [ dup length ] dip ; inline
469 : each-index ( seq quot -- )
470 prepare-index 2each ; inline
472 : map-index ( seq quot -- )
473 prepare-index 2map ; inline
475 : reduce-index ( seq identity quot -- )
476 swapd each-index ; inline
478 : index ( obj seq -- n )
479 [ = ] with find drop ;
481 : index-from ( obj i seq -- n )
482 rot [ = ] curry find-from drop ;
484 : last-index ( obj seq -- n )
485 [ = ] with find-last drop ;
487 : last-index-from ( obj i seq -- n )
488 rot [ = ] curry find-last-from drop ;
490 : indices ( obj seq -- indices )
492 [ rot = [ over push ] [ drop ] if ]
495 : nths ( indices seq -- seq' )
498 : contains? ( seq quot -- ? )
499 find drop >boolean ; inline
501 : member? ( elt seq -- ? )
502 [ = ] with contains? ;
504 : memq? ( elt seq -- ? )
505 [ eq? ] with contains? ;
507 : remove ( elt seq -- newseq )
508 [ = not ] with filter ;
510 : remq ( elt seq -- newseq )
511 [ eq? not ] with filter ;
513 : sift ( seq -- newseq )
516 : harvest ( seq -- newseq )
517 [ empty? not ] filter ;
519 : cache-nth ( i seq quot -- elt )
523 drop swap [ over [ call dup ] dip ] dip set-nth
526 : mismatch ( seq1 seq2 -- i )
528 [ 2nth-unsafe = not ] 2curry
533 [ -rot 2nth-unsafe <=> ] [ [ length ] compare ] if* ;
535 : sequence= ( seq1 seq2 -- ? )
536 2dup [ length ] bi@ =
537 [ mismatch not ] [ 2drop f ] if ; inline
539 : sequence-hashcode-step ( oldhash newpart -- newhash )
541 dup -2 fixnum-shift-fast swap 5 fixnum-shift-fast
542 fixnum+fast fixnum+fast
543 ] keep fixnum-bitxor ; inline
545 : sequence-hashcode ( n seq -- x )
546 0 -rot [ hashcode* sequence-hashcode-step ] with each ; inline
548 M: reversed equal? over reversed? [ sequence= ] [ 2drop f ] if ;
550 M: slice equal? over slice? [ sequence= ] [ 2drop f ] if ;
552 : move ( to from seq -- )
554 [ 3drop ] [ [ nth swap ] [ set-nth ] bi ] if ; inline
558 : (filter-here) ( quot: ( elt -- ? ) store scan seq -- )
561 [ nth-unsafe pick call [ 1+ ] when ] 2keep
564 ] [ nip set-length drop ] if ; inline recursive
568 : filter-here ( seq quot -- )
569 0 0 roll (filter-here) ; inline
571 : delete ( elt seq -- )
572 [ = not ] with filter-here ;
574 : delq ( elt seq -- )
575 [ eq? not ] with filter-here ;
577 : prefix ( seq elt -- newseq )
578 over [ over length 1+ ] dip [
579 [ 0 swap set-nth-unsafe ] keep
583 : suffix ( seq elt -- newseq )
584 over [ over length 1+ ] dip [
585 [ [ over length ] dip set-nth-unsafe ] keep
589 : peek ( seq -- elt ) [ length 1- ] [ nth ] bi ;
591 : pop* ( seq -- ) [ length 1- ] [ shorten ] bi ;
595 : move-backward ( shift from to seq -- )
599 [ [ 2over + pick ] dip move [ 1+ ] dip ] keep
603 : move-forward ( shift from to seq -- )
607 [ [ pick [ dup dup ] dip + swap ] dip move 1- ] keep
611 : (open-slice) ( shift from to seq ? -- )
613 [ [ 1- ] bi@ ] dip move-forward
615 [ over - ] 2dip move-backward
620 : open-slice ( shift from seq -- )
624 pick over length + over
625 [ pick 0 > [ [ length ] keep ] dip (open-slice) ] 2dip
629 : delete-slice ( from to seq -- )
630 check-slice [ over [ - ] dip ] dip open-slice ;
632 : delete-nth ( n seq -- )
633 [ dup 1+ ] dip delete-slice ;
635 : replace-slice ( new from to seq -- )
636 [ [ [ dup pick length + ] dip - over ] dip open-slice ] keep
639 : remove-nth ( n seq -- seq' )
640 [ swap head-slice ] [ swap 1+ tail-slice ] 2bi append ;
643 [ length 1- ] [ [ nth ] [ shorten ] 2bi ] bi ;
645 : all-equal? ( seq -- ? ) [ = ] monotonic? ;
647 : all-eq? ( seq -- ? ) [ eq? ] monotonic? ;
649 : exchange ( m n seq -- )
650 pick over bounds-check 2drop 2dup bounds-check 2drop
653 : reverse-here ( seq -- )
656 tuck - 1- rot exchange-unsafe
659 : reverse ( seq -- newseq )
661 dup [ length ] keep new-sequence
663 [ reverse-here ] keep
666 : sum-lengths ( seq -- n )
667 0 [ length + ] reduce ;
669 : concat ( seq -- newseq )
674 [ first new-resizable ] keep
675 [ [ over push-all ] each ] keep
681 : joined-length ( seq glue -- n )
682 [ dup sum-lengths swap length 1 [-] ] dip length * + ;
686 : join ( seq glue -- newseq )
688 2dup joined-length over new-resizable spin
689 [ dup pick push-all ] [ pick push-all ] interleave drop
692 : padding ( seq n elt quot -- newseq )
694 [ over length [-] dup 0 = [ drop ] ] dip
695 [ <repetition> ] curry
696 ] dip compose if ; inline
698 : pad-left ( seq n elt -- padded )
699 [ swap dup append-as ] padding ;
701 : pad-right ( seq n elt -- padded )
704 : shorter? ( seq1 seq2 -- ? ) [ length ] bi@ < ;
706 : head? ( seq begin -- ? )
710 tuck length head-slice sequence=
713 : tail? ( seq end -- ? )
717 tuck length tail-slice* sequence=
720 : cut-slice ( seq n -- before-slice after-slice )
721 [ head-slice ] [ tail-slice ] 2bi ;
723 : insert-nth ( elt n seq -- seq' )
724 swap cut-slice [ swap suffix ] dip append ;
726 : midpoint@ ( seq -- n ) length 2/ ; inline
728 : halves ( seq -- first-slice second-slice )
729 dup midpoint@ cut-slice ;
731 : binary-reduce ( seq start quot: ( elt1 elt2 -- newelt ) -- value )
732 #! We can't use case here since combinators depends on
734 pick length dup 0 3 between? [
738 [ [ drop first2 ] dip call ]
739 [ [ drop first3 ] dip bi@ ]
744 [ [ binary-reduce ] 2curry bi@ ] keep
746 ] if ; inline recursive
748 : cut ( seq n -- before after )
749 [ head ] [ tail ] 2bi ;
751 : cut* ( seq n -- before after )
752 [ head* ] [ tail* ] 2bi ;
756 : (start) ( subseq seq n -- subseq seq ? )
758 [ 3dup ] dip [ + swap nth-unsafe ] keep rot nth-unsafe =
763 : start* ( subseq seq n -- i )
764 pick length pick length swap - 1+
765 [ (start) ] find-from
768 : start ( subseq seq -- i ) 0 start* ; inline
770 : subseq? ( subseq seq -- ? ) start >boolean ;
772 : drop-prefix ( seq1 seq2 -- slice1 slice2 )
773 2dup mismatch [ 2dup min-length ] unless*
774 tuck [ tail-slice ] 2bi@ ;
776 : unclip ( seq -- rest first )
777 [ rest ] [ first ] bi ;
779 : unclip-last ( seq -- butlast last )
780 [ but-last ] [ peek ] bi ;
782 : unclip-slice ( seq -- rest-slice first )
783 [ rest-slice ] [ first ] bi ; inline
785 : 2unclip-slice ( seq1 seq2 -- rest-slice1 rest-slice2 first1 first2 )
786 [ unclip-slice ] bi@ swapd ; inline
788 : map-reduce ( seq map-quot reduce-quot -- result )
789 [ [ unclip-slice ] dip [ call ] keep ] dip
790 compose reduce ; inline
792 : 2map-reduce ( seq1 seq2 map-quot reduce-quot -- result )
793 [ [ 2unclip-slice ] dip [ call ] keep ] dip
794 compose 2reduce ; inline
796 : unclip-last-slice ( seq -- butlast-slice last )
797 [ but-last-slice ] [ peek ] bi ; inline
799 : <flat-slice> ( seq -- slice )
800 dup slice? [ { } like ] when 0 over length rot <slice> ;
803 : trim-left-slice ( seq quot -- slice )
804 over [ [ not ] compose find drop ] dip swap
805 [ tail-slice ] [ dup length tail-slice ] if* ; inline
807 : trim-left ( seq quot -- newseq )
808 over [ trim-left-slice ] dip like ; inline
810 : trim-right-slice ( seq quot -- slice )
811 over [ [ not ] compose find-last drop ] dip swap
812 [ 1+ head-slice ] [ 0 head-slice ] if* ; inline
814 : trim-right ( seq quot -- newseq )
815 over [ trim-right-slice ] dip like ; inline
817 : trim-slice ( seq quot -- slice )
818 [ trim-left-slice ] [ trim-right-slice ] bi ; inline
820 : trim ( seq quot -- newseq )
821 over [ trim-slice ] dip like ; inline
823 : sum ( seq -- n ) 0 [ + ] binary-reduce ;
825 : product ( seq -- n ) 1 [ * ] binary-reduce ;
827 : infimum ( seq -- n ) dup first [ min ] reduce ;
829 : supremum ( seq -- n ) dup first [ max ] reduce ;
831 : flip ( matrix -- newmatrix )
833 dup [ length ] map infimum
834 swap [ [ nth-unsafe ] with { } map-as ] curry { } map-as
837 : sigma ( seq quot -- n ) [ + ] compose 0 swap reduce ; inline
839 : count ( seq quot -- n ) [ 1 0 ? ] compose sigma ; inline