1 USING: arrays grouping kernel locals math math.order math.ranges
6 : reduce1 ( seq quot -- result ) [ unclip ] dip reduce ; inline
9 ( list identity quot: ( obj1 obj2 -- obj ) -- result )
12 [ list rest identity quot reduce-r list first quot call ] if ;
15 ! Quot must have static stack effect, unlike "reduce"
16 :: reduce* ( seq id quot -- result ) seq
18 [ unclip id swap quot call( prev elt -- next ) quot reduce* ] if-empty ; inline recursive
20 :: combos ( list1 list2 -- result ) list2 [ [ 2array ] curry list1 swap map ] map concat ;
21 : find-all ( seq quot -- elts ) [ [ length iota ] keep ] dip
22 [ dupd call( a -- ? ) [ 2array ] [ 2drop f ] if ] curry 2map [ ] filter ; inline
24 : insert-sorted ( elt seq -- seq ) 2dup [ < ] with find drop over length or swap insert-nth ;
26 : max-by ( obj1 obj2 quot: ( obj -- n ) -- obj1/obj2 )
27 [ bi@ [ max ] keep eq? not ] curry most ; inline
29 : min-by ( obj1 obj2 quot: ( obj -- n ) -- obj1/obj2 )
30 [ bi@ [ min ] keep eq? not ] curry most ; inline
32 : maximum ( seq quot: ( ... elt -- ... x ) -- elt )
34 [ [ first ] max-by ] map-reduce second ; inline
36 : minimum ( seq quot: ( ... elt -- ... x ) -- elt )
38 [ [ first ] min-by ] map-reduce second ; inline
40 : all-subseqs ( seq -- seqs )
41 dup length [1,b] [ <clumps> ] with map concat ;
43 :: each-subseq ( ... seq quot: ( ... x -- ... ) -- ... )
46 from seq length (a,b] [
48 from to seq subseq quot call( x -- )
52 :: longest-subseq ( seq1 seq2 -- subseq )
57 len1 1 + [ len2 1 + 0 <array> ] replicate :> table
62 y 1 - x 1 - table nth nth 1 + :> len
63 len y x table nth set-nth
64 len n > [ len n! x end! ] when
65 ] [ 0 y x table nth set-nth ] if
67 ] each end n - end seq1 subseq ;
69 : pad-longest ( seq1 seq2 elt -- seq1 seq2 )
70 [ 2dup max-length ] dip [ pad-tail ] 2curry bi@ ;