factor/core/sequences/sequences.factor

1085 lines
30 KiB
Factor
Raw Normal View History

! Copyright (C) 2005, 2011 Slava Pestov, Daniel Ehrenberg.
2007-09-20 18:09:08 -04:00
! See http://factorcode.org/license.txt for BSD license.
2011-11-30 19:02:37 -05:00
USING: accessors kernel kernel.private math math.order
math.private slots.private ;
2007-09-20 18:09:08 -04:00
IN: sequences
MIXIN: sequence
GENERIC: length ( seq -- n ) flushable
GENERIC: set-length ( n seq -- )
GENERIC: nth ( n seq -- elt ) flushable
GENERIC: set-nth ( elt n seq -- )
2008-04-13 13:54:58 -04:00
GENERIC: new-sequence ( len seq -- newseq ) flushable
2007-09-20 18:09:08 -04:00
GENERIC: new-resizable ( len seq -- newseq ) flushable
GENERIC: like ( seq exemplar -- newseq ) flushable
GENERIC: clone-like ( seq exemplar -- newseq ) flushable
: new-like ( len exemplar quot -- seq )
over [ [ new-sequence ] dip call ] dip like ; inline
2007-09-20 18:09:08 -04:00
M: sequence like drop ; inline
2007-09-20 18:09:08 -04:00
GENERIC: lengthen ( n seq -- )
2008-07-12 02:08:30 -04:00
GENERIC: shorten ( n seq -- )
2007-09-20 18:09:08 -04:00
M: sequence lengthen 2dup length > [ set-length ] [ 2drop ] if ; inline
M: sequence shorten 2dup length < [ set-length ] [ 2drop ] if ; inline
2008-07-12 02:08:30 -04:00
2008-10-02 07:47:20 -04:00
: empty? ( seq -- ? ) length 0 = ; inline
2008-09-05 20:29:14 -04:00
: if-empty ( ..a seq quot1: ( ..a -- ..b ) quot2: ( ..a seq -- ..b ) -- ..b )
[ dup empty? ] [ [ drop ] prepose ] [ ] tri* if ; inline
2008-09-05 20:29:14 -04:00
2008-09-07 03:10:13 -04:00
: when-empty ( seq quot -- ) [ ] if-empty ; inline
2008-09-05 20:29:14 -04:00
2008-09-07 03:10:13 -04:00
: unless-empty ( seq quot -- ) [ ] swap if-empty ; inline
2008-09-05 20:29:14 -04:00
2007-09-20 18:09:08 -04:00
: delete-all ( seq -- ) 0 swap set-length ;
: first ( seq -- first ) 0 swap nth ; inline
: second ( seq -- second ) 1 swap nth ; inline
: third ( seq -- third ) 2 swap nth ; inline
2008-08-18 21:13:24 -04:00
: fourth ( seq -- fourth ) 3 swap nth ; inline
2007-09-20 18:09:08 -04:00
: set-first ( first seq -- ) 0 swap set-nth ; inline
: set-second ( second seq -- ) 1 swap set-nth ; inline
: set-third ( third seq -- ) 2 swap set-nth ; inline
: set-fourth ( fourth seq -- ) 3 swap set-nth ; inline
: push ( elt seq -- ) [ length ] [ set-nth ] bi ;
2007-09-20 18:09:08 -04:00
2008-03-20 16:00:49 -04:00
ERROR: bounds-error index seq ;
2007-09-20 18:09:08 -04:00
GENERIC# bounds-check? 1 ( n seq -- ? )
M: integer bounds-check? ( n seq -- ? )
dupd length < [ 0 >= ] [ drop f ] if ; inline
2007-09-20 18:09:08 -04:00
: bounds-check ( n seq -- n seq )
2dup bounds-check? [ bounds-error ] unless ; inline
MIXIN: immutable-sequence
ERROR: immutable element index sequence ;
2007-09-20 18:09:08 -04:00
M: immutable-sequence set-nth immutable ;
INSTANCE: immutable-sequence sequence
<PRIVATE
: array-nth ( n array -- elt )
swap 2 fixnum+fast slot ; inline
: set-array-nth ( elt n array -- )
swap 2 fixnum+fast set-slot ; inline
2008-09-10 02:45:16 -04:00
: dispatch ( n array -- ) array-nth call ;
2008-02-11 02:19:53 -05:00
2007-09-20 18:09:08 -04:00
GENERIC: resize ( n seq -- newseq ) flushable
! Unsafe sequence protocol for inner loops
GENERIC: nth-unsafe ( n seq -- elt ) flushable
GENERIC: set-nth-unsafe ( elt n seq -- )
M: sequence nth bounds-check nth-unsafe ; inline
M: sequence set-nth bounds-check set-nth-unsafe ; inline
2007-09-20 18:09:08 -04:00
M: sequence nth-unsafe nth ; inline
M: sequence set-nth-unsafe set-nth ; inline
2007-09-20 18:09:08 -04:00
: change-nth-unsafe ( i seq quot -- )
[ [ nth-unsafe ] dip call ] 3keep drop set-nth-unsafe ; inline
2010-02-09 22:12:02 -05:00
PRIVATE>
2007-09-20 18:09:08 -04:00
! The f object supports the sequence protocol trivially
M: f length drop 0 ; inline
M: f nth-unsafe nip ; inline
M: f like drop [ f ] when-empty ; inline
2007-09-20 18:09:08 -04:00
INSTANCE: f immutable-sequence
2010-02-09 22:12:02 -05:00
! Integer sequences
TUPLE: iota-tuple { n integer read-only } ;
ERROR: non-negative-integer-expected n ;
: iota ( n -- iota )
dup 0 < [ non-negative-integer-expected ] when
\ iota-tuple boa ; inline
M: iota-tuple length n>> ; inline
M: iota-tuple nth-unsafe drop ; inline
INSTANCE: iota-tuple immutable-sequence
2010-02-09 22:12:02 -05:00
<PRIVATE
: first-unsafe ( seq -- first ) 0 swap nth-unsafe ; inline
: second-unsafe ( seq -- second ) 1 swap nth-unsafe ; inline
: third-unsafe ( seq -- third ) 2 swap nth-unsafe ; inline
: fourth-unsafe ( seq -- fourth ) 3 swap nth-unsafe ; inline
2008-11-29 11:38:43 -05:00
: first2-unsafe ( seq -- first second )
[ first-unsafe ] [ second-unsafe ] bi ; inline
2007-09-20 18:09:08 -04:00
: first3-unsafe ( seq -- first second third )
[ first2-unsafe ] [ third-unsafe ] bi ; inline
2007-09-20 18:09:08 -04:00
: first4-unsafe ( seq -- first second third fourth )
[ first3-unsafe ] [ fourth-unsafe ] bi ; inline
2007-09-20 18:09:08 -04:00
: exchange-unsafe ( m n seq -- )
[ [ nth-unsafe ] curry bi@ ]
[ [ set-nth-unsafe ] curry bi@ ] 3bi ; inline
2007-09-20 18:09:08 -04:00
: (head) ( seq n -- from to seq ) [ 0 ] 2dip swap ; inline
2007-09-20 18:09:08 -04:00
: (tail) ( seq n -- from to seq ) swap [ length ] keep ; inline
2007-09-20 18:09:08 -04:00
2008-12-15 22:34:25 -05:00
: from-end ( seq n -- seq n' ) [ dup length ] dip - ; inline
2007-09-20 18:09:08 -04:00
: (1sequence) ( obj seq -- seq )
[ 0 swap set-nth-unsafe ] keep ; inline
: (2sequence) ( obj1 obj2 seq -- seq )
[ 1 swap set-nth-unsafe ] keep (1sequence) ; inline
2007-09-20 18:09:08 -04:00
: (3sequence) ( obj1 obj2 obj3 seq -- seq )
[ 2 swap set-nth-unsafe ] keep (2sequence) ; inline
2007-09-20 18:09:08 -04:00
: (4sequence) ( obj1 obj2 obj3 obj4 seq -- seq )
[ 3 swap set-nth-unsafe ] keep (3sequence) ; inline
2007-09-20 18:09:08 -04:00
PRIVATE>
: 1sequence ( obj exemplar -- seq )
1 swap [ (1sequence) ] new-like ; inline
2007-09-20 18:09:08 -04:00
: 2sequence ( obj1 obj2 exemplar -- seq )
2 swap [ (2sequence) ] new-like ; inline
: 3sequence ( obj1 obj2 obj3 exemplar -- seq )
3 swap [ (3sequence) ] new-like ; inline
: 4sequence ( obj1 obj2 obj3 obj4 exemplar -- seq )
4 swap [ (4sequence) ] new-like ; inline
: first2 ( seq -- first second )
2009-10-26 18:30:37 -04:00
1 swap bounds-check nip first2-unsafe ; inline
2007-09-20 18:09:08 -04:00
: first3 ( seq -- first second third )
2009-10-26 18:30:37 -04:00
2 swap bounds-check nip first3-unsafe ; inline
2007-09-20 18:09:08 -04:00
: first4 ( seq -- first second third fourth )
2009-10-26 18:30:37 -04:00
3 swap bounds-check nip first4-unsafe ; inline
2007-09-20 18:09:08 -04:00
: ?nth ( n seq -- elt/f )
2dup bounds-check? [ nth-unsafe ] [ 2drop f ] if ; inline
2007-09-20 18:09:08 -04:00
: ?set-nth ( elt n seq -- )
2dup bounds-check? [ set-nth-unsafe ] [ 3drop ] if ; inline
2011-10-13 15:53:46 -04:00
: ?first ( seq -- elt/f ) 0 swap ?nth ; inline
: ?second ( seq -- elt/f ) 1 swap ?nth ; inline
2012-07-27 12:12:29 -04:00
: ?last ( seq -- elt/f )
[ length 1 - ] keep over 0 <
[ 2drop f ] [ nth-unsafe ] if ; inline
2011-10-13 15:53:46 -04:00
2007-09-20 18:09:08 -04:00
MIXIN: virtual-sequence
GENERIC: virtual-exemplar ( seq -- seq' )
2007-09-20 18:09:08 -04:00
GENERIC: virtual@ ( n seq -- n' seq' )
M: virtual-sequence nth virtual@ nth ; inline
M: virtual-sequence set-nth virtual@ set-nth ; inline
M: virtual-sequence nth-unsafe virtual@ nth-unsafe ; inline
M: virtual-sequence set-nth-unsafe virtual@ set-nth-unsafe ; inline
M: virtual-sequence like virtual-exemplar like ; inline
M: virtual-sequence new-sequence virtual-exemplar new-sequence ; inline
2007-09-20 18:09:08 -04:00
INSTANCE: virtual-sequence sequence
! A reversal of an underlying sequence.
2008-06-30 02:44:58 -04:00
TUPLE: reversed { seq read-only } ;
2007-09-20 18:09:08 -04:00
C: <reversed> reversed
M: reversed virtual-exemplar seq>> ; inline
M: reversed virtual@ seq>> [ length swap - 1 - ] keep ; inline
M: reversed length seq>> length ; inline
2007-09-20 18:09:08 -04:00
INSTANCE: reversed virtual-sequence
! A slice of another sequence.
2008-06-29 22:37:57 -04:00
TUPLE: slice
2008-06-30 02:44:58 -04:00
{ from read-only }
{ to read-only }
{ seq read-only } ;
2007-09-20 18:09:08 -04:00
: collapse-slice ( m n slice -- m' n' seq )
[ from>> ] [ seq>> ] bi [ [ + ] curry bi@ ] dip ; inline
2007-09-20 18:09:08 -04:00
ERROR: slice-error from to seq reason ;
2009-03-11 01:10:27 -04:00
: check-slice-error ( from to seq ? string -- from to seq )
[ slice-error ] curry when ; inline
2007-09-20 18:09:08 -04:00
2007-09-25 21:09:46 -04:00
: check-slice ( from to seq -- from to seq )
2009-03-11 01:10:27 -04:00
3dup
[ 2drop 0 < "start < 0" check-slice-error ]
[ [ drop ] 2dip length > "end > sequence" check-slice-error ]
[ drop > "start > end" check-slice-error ]
2009-03-12 21:37:26 -04:00
3tri ; inline
2007-09-20 18:09:08 -04:00
<PRIVATE
: <slice-unsafe> ( from to seq -- slice )
2012-09-04 22:56:10 -04:00
dup slice? [ collapse-slice ] when slice boa ; inline
PRIVATE>
2007-09-20 18:09:08 -04:00
: <slice> ( from to seq -- slice )
2012-09-04 22:56:10 -04:00
check-slice <slice-unsafe> ; inline
2007-09-20 18:09:08 -04:00
M: slice virtual-exemplar seq>> ; inline
M: slice virtual@ [ from>> + ] [ seq>> ] bi ; inline
M: slice length [ to>> ] [ from>> ] bi - ; inline
2007-09-20 18:09:08 -04:00
2008-06-18 06:58:05 -04:00
: short ( seq n -- seq n' ) over length min ; inline
2008-08-24 04:59:37 -04:00
: head-slice ( seq n -- slice ) (head) <slice> ; inline
2007-09-20 18:09:08 -04:00
2008-08-24 04:59:37 -04:00
: tail-slice ( seq n -- slice ) (tail) <slice> ; inline
2007-09-20 18:09:08 -04:00
2008-08-24 04:59:37 -04:00
: rest-slice ( seq -- slice ) 1 tail-slice ; inline
2008-08-24 04:59:37 -04:00
: head-slice* ( seq n -- slice ) from-end head-slice ; inline
2007-09-20 18:09:08 -04:00
2008-08-24 04:59:37 -04:00
: tail-slice* ( seq n -- slice ) from-end tail-slice ; inline
2007-09-20 18:09:08 -04:00
2008-08-24 04:59:37 -04:00
: but-last-slice ( seq -- slice ) 1 head-slice* ; inline
2007-09-20 18:09:08 -04:00
INSTANCE: slice virtual-sequence
! One element repeated many times
2008-06-30 02:44:58 -04:00
TUPLE: repetition { len read-only } { elt read-only } ;
2007-09-20 18:09:08 -04:00
C: <repetition> repetition
M: repetition length len>> ; inline
M: repetition nth-unsafe nip elt>> ; inline
2007-09-20 18:09:08 -04:00
INSTANCE: repetition immutable-sequence
<PRIVATE
ERROR: integer-length-expected obj ;
: check-length ( n -- n )
dup integer? [ integer-length-expected ] unless ; inline
2009-10-30 20:39:46 -04:00
TUPLE: copy-state
{ src-i read-only }
{ src read-only }
{ dst-i read-only }
{ dst read-only } ;
2009-10-30 20:39:46 -04:00
C: <copy> copy-state
2007-09-20 18:09:08 -04:00
2009-10-30 20:39:46 -04:00
: ((copy)) ( n copy -- )
[ [ src-i>> + ] [ src>> ] bi nth-unsafe ]
[ [ dst-i>> + ] [ dst>> ] bi set-nth-unsafe ] 2bi ; inline
: (copy) ( n copy -- dst )
over 0 <= [ nip dst>> ] [
[ 1 - ] dip [ ((copy)) ] [ (copy) ] 2bi
] if ; inline recursive
2007-09-20 18:09:08 -04:00
2009-10-30 20:39:46 -04:00
: subseq>copy ( from to seq -- n copy )
[ over - check-length swap ] dip
3dup nip new-sequence 0 swap <copy> ; inline
2007-09-20 18:09:08 -04:00
2011-10-14 13:23:52 -04:00
: bounds-check-head ( n seq -- n seq )
over 0 < [ bounds-error ] when ; inline
2009-10-30 20:39:46 -04:00
: check-copy ( src n dst -- src n dst )
2011-10-14 13:23:52 -04:00
3dup bounds-check-head
[ swap length + ] dip lengthen ; inline
2007-09-20 18:09:08 -04:00
: copy-unsafe ( src i dst -- )
#! The check-length call forces partial dispatch
[ [ length check-length 0 ] keep ] 2dip <copy> (copy) drop ; inline
2007-09-20 18:09:08 -04:00
PRIVATE>
: subseq ( from to seq -- subseq )
2009-10-30 20:39:46 -04:00
[ check-slice subseq>copy (copy) ] keep like ;
2007-09-20 18:09:08 -04:00
: head ( seq n -- headseq ) (head) subseq ;
: tail ( seq n -- tailseq ) (tail) subseq ;
: rest ( seq -- tailseq ) 1 tail ;
2007-09-20 18:09:08 -04:00
: head* ( seq n -- headseq ) from-end head ;
: tail* ( seq n -- tailseq ) from-end tail ;
2008-05-07 02:38:34 -04:00
: but-last ( seq -- headseq ) 1 head* ;
2007-09-20 18:09:08 -04:00
: copy ( src i dst -- )
check-copy copy-unsafe ; inline
2007-09-20 18:09:08 -04:00
M: sequence clone-like
[ dup length ] dip new-sequence [ 0 swap copy-unsafe ] keep ; inline
2007-09-20 18:09:08 -04:00
M: immutable-sequence clone-like like ; inline
2007-09-20 18:09:08 -04:00
: push-all ( src dst -- ) [ length ] [ copy ] bi ; inline
2007-09-20 18:09:08 -04:00
<PRIVATE
: (append) ( seq1 seq2 accum -- accum )
[ [ over length ] dip copy-unsafe ]
[ 0 swap copy-unsafe ]
[ ] tri ; inline
2007-09-20 18:09:08 -04:00
PRIVATE>
2007-09-20 18:09:08 -04:00
: append-as ( seq1 seq2 exemplar -- newseq )
[ 2dup [ length ] bi@ + ] dip
[ (append) ] new-like ; inline
2007-09-20 18:09:08 -04:00
: 3append-as ( seq1 seq2 seq3 exemplar -- newseq )
2009-03-11 01:10:27 -04:00
[ 3dup [ length ] tri@ + + ] dip [
[ [ 2over [ length ] bi@ + ] dip copy-unsafe ]
[ (append) ] bi
] new-like ; inline
: append ( seq1 seq2 -- newseq ) over append-as ;
2007-09-20 18:09:08 -04:00
: prepend-as ( seq1 seq2 exemplar -- newseq ) swapd append-as ; inline
: prepend ( seq1 seq2 -- newseq ) over prepend-as ;
2008-03-19 20:15:43 -04:00
: 3append ( seq1 seq2 seq3 -- newseq ) pick 3append-as ;
2007-09-20 18:09:08 -04:00
2008-12-03 09:32:54 -05:00
: surround ( seq1 seq2 seq3 -- newseq ) swapd 3append ; inline
: glue ( seq1 seq2 seq3 -- newseq ) swap 3append ; inline
: change-nth ( ..a i seq quot: ( ..a elt -- ..b newelt ) -- ..b )
[ [ nth ] dip call ] 3keep drop set-nth-unsafe ; inline
2007-09-20 18:09:08 -04:00
2008-03-29 21:36:58 -04:00
: min-length ( seq1 seq2 -- n ) [ length ] bi@ min ; inline
2007-09-20 18:09:08 -04:00
2008-03-29 21:36:58 -04:00
: max-length ( seq1 seq2 -- n ) [ length ] bi@ max ; inline
2007-09-20 18:09:08 -04:00
<PRIVATE
: ((each)) ( seq -- n quot )
[ length ] keep [ nth-unsafe ] curry ; inline
2007-09-20 18:09:08 -04:00
: (each) ( seq quot -- n quot' )
[ ((each)) ] dip compose ; inline
: (each-index) ( seq quot -- n quot' )
[ ((each)) [ keep ] curry ] dip compose ; inline
2007-09-20 18:09:08 -04:00
: (collect) ( quot into -- quot' )
[ [ keep ] dip set-nth-unsafe ] 2curry ; inline
2007-09-20 18:09:08 -04:00
: collect ( n quot into -- )
(collect) each-integer ; inline
: map-into ( seq quot into -- )
[ (each) ] dip collect ; inline
2007-09-20 18:09:08 -04:00
: 2nth-unsafe ( n seq1 seq2 -- elt1 elt2 )
[ nth-unsafe ] bi-curry@ bi ; inline
2007-09-20 18:09:08 -04:00
2012-08-08 23:08:08 -04:00
: ((2each)) ( seq1 seq2 -- n quot )
[ min-length ] 2keep [ 2nth-unsafe ] 2curry ; inline
2007-09-20 18:09:08 -04:00
: (2each) ( seq1 seq2 quot -- n quot' )
2012-08-08 23:08:08 -04:00
[ ((2each)) ] dip compose ; inline
: 3nth-unsafe ( n seq1 seq2 seq3 -- elt1 elt2 elt3 )
[ nth-unsafe ] tri-curry@ tri ; inline
2008-07-22 05:44:33 -04:00
: (3each) ( seq1 seq2 seq3 quot -- n quot' )
[
[ [ length ] tri@ min min ]
[ [ 3nth-unsafe ] 3curry ] 3bi
] dip compose ; inline
2007-09-20 18:09:08 -04:00
: finish-find ( i seq -- i elt )
over [ dupd nth-unsafe ] [ drop f ] if ; inline
: (find) ( seq quot quot' -- i elt )
pick [ [ (each) ] dip call ] dip finish-find ; inline
2007-09-20 18:09:08 -04:00
: (find-from) ( n seq quot quot' -- i elt )
2008-08-18 21:13:24 -04:00
[ 2dup bounds-check? ] 2dip
[ (find) ] 2curry
[ 2drop f f ]
if ; inline
2007-09-20 18:09:08 -04:00
2011-10-12 22:41:54 -04:00
: (find-index) ( seq quot quot' -- i elt )
pick [ [ (each-index) ] dip call ] dip finish-find ; inline
2012-04-27 16:41:47 -04:00
: (find-index-from) ( n seq quot quot' -- i elt )
[ 2dup bounds-check? ] 2dip
[ (find-index) ] 2curry
[ 2drop f f ]
if ; inline
: (accumulate) ( seq identity quot -- identity seq quot )
2011-10-14 13:23:52 -04:00
swapd [ curry keep ] curry ; inline
2010-01-22 16:04:13 -05:00
2007-09-20 18:09:08 -04:00
PRIVATE>
: each ( ... seq quot: ( ... x -- ... ) -- ... )
2007-09-20 18:09:08 -04:00
(each) each-integer ; inline
: reduce ( ... seq identity quot: ( ... prev elt -- ... next ) -- ... result )
2007-09-20 18:09:08 -04:00
swapd each ; inline
: map-integers ( ... len quot: ( ... i -- ... elt ) exemplar -- ... newseq )
[ over ] dip [ [ collect ] keep ] new-like ; inline
2010-03-09 23:29:44 -05:00
: map-as ( ... seq quot: ( ... elt -- ... newelt ) exemplar -- ... newseq )
[ (each) ] dip map-integers ; inline
2007-09-20 18:09:08 -04:00
2010-03-09 23:29:44 -05:00
: map ( ... seq quot: ( ... elt -- ... newelt ) -- ... newseq )
2007-09-20 18:09:08 -04:00
over map-as ; inline
2010-03-09 23:29:44 -05:00
: replicate-as ( ... len quot: ( ... -- ... newelt ) exemplar -- ... newseq )
2010-01-14 10:10:13 -05:00
[ [ drop ] prepose ] dip map-integers ; inline
2010-03-09 23:29:44 -05:00
: replicate ( ... len quot: ( ... -- ... newelt ) -- ... newseq )
2010-01-14 10:10:13 -05:00
{ } replicate-as ; inline
2010-03-09 23:29:44 -05:00
: map! ( ... seq quot: ( ... elt -- ... newelt ) -- ... seq )
over [ map-into ] keep ; inline
2007-09-20 18:09:08 -04:00
: accumulate-as ( ... seq identity quot: ( ... prev elt -- ... next ) exemplar -- ... final newseq )
2009-10-28 17:10:05 -04:00
[ (accumulate) ] dip map-as ; inline
: accumulate ( ... seq identity quot: ( ... prev elt -- ... next ) -- ... final newseq )
pick accumulate-as ; inline
2007-09-20 18:09:08 -04:00
: accumulate! ( ... seq identity quot: ( ... prev elt -- ... next ) -- ... final seq )
2009-10-28 17:10:05 -04:00
(accumulate) map! ; inline
2010-03-09 23:29:44 -05:00
: 2each ( ... seq1 seq2 quot: ( ... elt1 elt2 -- ... ) -- ... )
2007-09-20 18:09:08 -04:00
(2each) each-integer ; inline
: 2reduce ( ... seq1 seq2 identity quot: ( ... prev elt1 elt2 -- ... next ) -- ... result )
[ -rot ] dip 2each ; inline
2007-09-20 18:09:08 -04:00
2010-03-09 23:29:44 -05:00
: 2map-as ( ... seq1 seq2 quot: ( ... elt1 elt2 -- ... newelt ) exemplar -- ... newseq )
[ (2each) ] dip map-integers ; inline
2007-09-20 18:09:08 -04:00
2010-03-09 23:29:44 -05:00
: 2map ( ... seq1 seq2 quot: ( ... elt1 elt2 -- ... newelt ) -- ... newseq )
2008-07-13 20:55:54 -04:00
pick 2map-as ; inline
: 2all? ( ... seq1 seq2 quot: ( ... elt1 elt2 -- ... ? ) -- ... ? )
2007-09-20 18:09:08 -04:00
(2each) all-integers? ; inline
2010-03-09 23:29:44 -05:00
: 3each ( ... seq1 seq2 seq3 quot: ( ... elt1 elt2 elt3 -- ... ) -- ... )
2010-01-14 10:10:13 -05:00
(3each) each-integer ; inline
2010-03-09 23:29:44 -05:00
: 3map-as ( ... seq1 seq2 seq3 quot: ( ... elt1 elt2 elt3 -- ... newelt ) exemplar -- ... newseq )
[ (3each) ] dip map-integers ; inline
2010-03-09 23:29:44 -05:00
: 3map ( ... seq1 seq2 seq3 quot: ( ... elt1 elt2 elt3 -- ... newelt ) -- ... newseq )
[ pick ] dip swap 3map-as ; inline
: find-from ( ... n seq quot: ( ... elt -- ... ? ) -- ... i elt )
[ (find-integer) ] (find-from) ; inline
2007-09-20 18:09:08 -04:00
: find ( ... seq quot: ( ... elt -- ... ? ) -- ... i elt )
2007-09-20 18:09:08 -04:00
[ find-integer ] (find) ; inline
: find-last-from ( ... n seq quot: ( ... elt -- ... ? ) -- ... i elt )
[ nip find-last-integer ] (find-from) ; inline
2007-09-20 18:09:08 -04:00
: find-last ( ... seq quot: ( ... elt -- ... ? ) -- ... i elt )
[ [ 1 - ] dip find-last-integer ] (find) ; inline
2007-09-20 18:09:08 -04:00
2012-04-27 16:41:47 -04:00
: find-index-from ( ... n seq quot: ( ... elt i -- ... ? ) -- ... i elt )
[ (find-integer) ] (find-index-from) ; inline
2011-10-12 22:41:54 -04:00
: find-index ( ... seq quot: ( ... elt i -- ... ? ) -- ... i elt )
[ find-integer ] (find-index) ; inline
: all? ( ... seq quot: ( ... elt -- ... ? ) -- ... ? )
2007-09-20 18:09:08 -04:00
(each) all-integers? ; inline
: push-if ( ..a elt quot: ( ..a elt -- ..b ? ) accum -- ..b )
[ keep ] dip rot [ push ] [ 2drop ] if ; inline
2007-09-20 18:09:08 -04:00
: selector-for ( quot exemplar -- selector accum )
2009-10-22 18:28:01 -04:00
[ length ] keep new-resizable [ [ push-if ] 2curry ] keep ; inline
: selector ( quot -- selector accum )
V{ } selector-for ; inline
2009-10-22 18:28:01 -04:00
: filter-as ( ... seq quot: ( ... elt -- ... ? ) exemplar -- ... subseq )
dup [ selector-for [ each ] dip ] curry dip like ; inline
2007-09-20 18:09:08 -04:00
: filter ( ... seq quot: ( ... elt -- ... ? ) -- ... subseq )
2009-10-22 18:28:01 -04:00
over filter-as ; inline
2007-09-20 18:09:08 -04:00
: push-either ( ..a elt quot: ( ..a elt -- ..b ? ) accum1 accum2 -- ..b )
[ keep swap ] 2dip ? push ; inline
2008-09-05 20:32:19 -04:00
: 2selector ( quot -- selector accum1 accum2 )
2008-09-05 20:32:19 -04:00
V{ } clone V{ } clone [ [ push-either ] 3curry ] 2keep ; inline
: partition ( ... seq quot: ( ... elt -- ... ? ) -- ... trueseq falseseq )
over [ 2selector [ each ] 2dip ] dip [ like ] curry bi@ ; inline
2008-09-05 20:32:19 -04:00
: collector-for ( quot exemplar -- quot' vec )
[ length ] keep new-resizable [ [ push ] curry compose ] keep ; inline
: collector ( quot -- quot' vec )
V{ } collector-for ; inline
2008-06-15 01:32:48 -04:00
: produce-as ( ..a pred: ( ..a -- ..b ? ) quot: ( ..b -- ..a obj ) exemplar -- ..b seq )
dup [ collector-for [ while ] dip ] curry dip like ; inline
2008-09-27 18:54:44 -04:00
: produce ( ..a pred: ( ..a -- ..b ? ) quot: ( ..b -- ..a obj ) -- ..b seq )
2008-09-27 18:54:44 -04:00
{ } produce-as ; inline
2007-10-21 15:28:35 -04:00
: follow ( ... obj quot: ( ... prev -- ... result/f ) -- ... seq )
[ dup ] swap [ keep ] curry produce nip ; inline
2010-03-09 23:29:44 -05:00
: each-index ( ... seq quot: ( ... elt index -- ... ) -- ... )
(each-index) each-integer ; inline
: interleave ( ... seq between quot: ( ... elt -- ... ) -- ... )
pick empty? [ 3drop ] [
[ [ drop first-unsafe ] dip call ]
2009-06-16 15:05:39 -04:00
[ [ rest-slice ] 2dip [ bi* ] 2curry each ]
3bi
] if ; inline
2009-02-01 20:14:43 -05:00
2010-03-09 23:29:44 -05:00
: map-index ( ... seq quot: ( ... elt index -- ... newelt ) -- ... newseq )
[ dup length iota ] dip 2map ; inline
2010-03-09 23:29:44 -05:00
: reduce-index ( ... seq identity quot: ( ... prev elt index -- ... next ) -- ... result )
swapd each-index ; inline
2007-09-20 18:09:08 -04:00
: index ( obj seq -- n )
2008-01-09 17:36:30 -05:00
[ = ] with find drop ;
2007-09-20 18:09:08 -04:00
: index-from ( obj i seq -- n )
rot [ = ] curry find-from drop ;
2007-09-20 18:09:08 -04:00
: last-index ( obj seq -- n )
2008-01-09 17:36:30 -05:00
[ = ] with find-last drop ;
2007-09-20 18:09:08 -04:00
: last-index-from ( obj i seq -- n )
rot [ = ] curry find-last-from drop ;
2007-09-20 18:09:08 -04:00
2009-09-08 17:25:41 -04:00
<PRIVATE
: (indices) ( elt i obj accum -- )
[ swap [ = ] dip ] dip [ push ] 2curry when ; inline
2009-09-08 17:25:41 -04:00
PRIVATE>
2008-09-12 17:03:47 -04:00
: indices ( obj seq -- indices )
swap V{ } clone
[ [ (indices) ] 2curry each-index ] keep ;
2008-09-12 17:03:47 -04:00
2013-04-06 17:20:32 -04:00
<PRIVATE
: nths-unsafe ( indices seq -- seq' )
[ [ nth-unsafe ] curry ] keep map-as ;
PRIVATE>
: nths ( indices seq -- seq' )
[ [ nth ] curry ] keep map-as ;
2008-08-13 19:56:41 -04:00
: any? ( ... seq quot: ( ... elt -- ... ? ) -- ... ? )
2007-09-20 18:09:08 -04:00
find drop >boolean ; inline
2008-10-19 04:34:58 -04:00
: member? ( elt seq -- ? )
[ = ] with any? ;
2007-09-20 18:09:08 -04:00
: member-eq? ( elt seq -- ? )
[ eq? ] with any? ;
2007-09-20 18:09:08 -04:00
2008-10-19 04:34:58 -04:00
: remove ( elt seq -- newseq )
[ = not ] with filter ;
2007-09-20 18:09:08 -04:00
2009-10-28 01:23:08 -04:00
: remove-eq ( elt seq -- newseq )
2008-10-19 04:34:58 -04:00
[ eq? not ] with filter ;
2008-05-14 00:36:55 -04:00
: sift ( seq -- newseq )
[ ] filter ;
: harvest ( seq -- newseq )
[ empty? not ] filter ;
2012-07-28 00:30:03 -04:00
<PRIVATE
: mismatch-unsafe ( n seq1 seq2 -- i )
[ 2nth-unsafe = not ] 2curry find-integer ; inline
2007-09-20 18:09:08 -04:00
2012-07-28 00:30:03 -04:00
PRIVATE>
: mismatch ( seq1 seq2 -- i )
[ min-length ] 2keep mismatch-unsafe ; inline
2007-09-20 18:09:08 -04:00
M: sequence <=>
2010-02-09 21:44:15 -05:00
[ mismatch ] 2keep pick
[ 2nth-unsafe <=> ] [ [ length ] compare nip ] if ;
2007-09-20 18:09:08 -04:00
: sequence= ( seq1 seq2 -- ? )
2012-07-28 00:30:03 -04:00
2dup [ length ] bi@ dupd =
[ -rot mismatch-unsafe not ] [ 3drop f ] if ; inline
2007-09-20 18:09:08 -04:00
2009-04-13 15:40:55 -04:00
ERROR: assert-sequence got expected ;
: assert-sequence= ( a b -- )
2009-04-13 15:40:55 -04:00
2dup sequence= [ 2drop ] [ assert-sequence ] if ;
<PRIVATE
: sequence-hashcode-step ( oldhash newpart -- newhash )
integer>fixnum swap [
[ -2 fixnum-shift-fast ] [ 5 fixnum-shift-fast ] bi
fixnum+fast fixnum+fast
] keep fixnum-bitxor ; inline
PRIVATE>
: sequence-hashcode ( n seq -- x )
[ 0 ] 2dip [ hashcode* sequence-hashcode-step ] with each ; inline
M: reversed equal? over reversed? [ sequence= ] [ 2drop f ] if ;
M: slice equal? over slice? [ sequence= ] [ 2drop f ] if ;
2007-09-20 18:09:08 -04:00
: move ( to from seq -- )
2008-10-02 07:47:20 -04:00
2over =
[ 3drop ] [ [ nth swap ] [ set-nth ] bi ] if ; inline
2007-09-20 18:09:08 -04:00
<PRIVATE
: move-unsafe ( to from seq -- )
2over =
[ 3drop ] [ [ nth-unsafe swap ] [ set-nth-unsafe ] bi ] if ; inline
: (filter!) ( ... quot: ( ... elt -- ... ? ) store scan seq -- ... )
2007-09-20 18:09:08 -04:00
2dup length < [
[ move-unsafe ] 3keep
[ nth-unsafe pick call [ 1 + ] when ] 2keep
[ 1 + ] dip
2009-10-28 01:44:05 -04:00
(filter!)
2008-10-19 04:34:58 -04:00
] [ nip set-length drop ] if ; inline recursive
2007-09-20 18:09:08 -04:00
PRIVATE>
: filter! ( ... seq quot: ( ... elt -- ... ? ) -- ... seq )
2009-10-28 01:44:05 -04:00
swap [ [ 0 0 ] dip (filter!) ] keep ; inline
2008-10-19 04:34:58 -04:00
2009-10-28 00:25:35 -04:00
: remove! ( elt seq -- seq )
2009-10-28 01:44:05 -04:00
[ = not ] with filter! ;
2008-10-19 04:34:58 -04:00
2009-10-28 01:23:08 -04:00
: remove-eq! ( elt seq -- seq )
2009-10-28 01:44:05 -04:00
[ eq? not ] with filter! ;
2007-09-20 18:09:08 -04:00
: prefix ( seq elt -- newseq )
over [ over length 1 + ] dip [
(1sequence) [ 1 swap copy-unsafe ] keep
] new-like ;
: suffix ( seq elt -- newseq )
over [ over length 1 + ] dip [
[ [ over length ] dip set-nth-unsafe ] keep
[ 0 swap copy-unsafe ] keep
2007-09-20 18:09:08 -04:00
] new-like ;
2009-11-13 04:01:28 -05:00
: suffix! ( seq elt -- seq ) over push ; inline
2009-10-28 14:38:27 -04:00
2009-11-13 04:01:28 -05:00
: append! ( seq1 seq2 -- seq1 ) over push-all ; inline
2009-10-28 14:38:27 -04:00
2012-07-18 15:31:53 -04:00
: last ( seq -- elt )
[ length 1 - ] keep
over 0 < [ bounds-error ] [ nth-unsafe ] if ; inline
2007-09-20 18:09:08 -04:00
<PRIVATE
2013-03-14 21:41:44 -04:00
: last-unsafe ( seq -- elt )
[ length 1 - ] [ nth-unsafe ] bi ; inline
PRIVATE>
2012-07-18 15:31:53 -04:00
: set-last ( elt seq -- )
[ length 1 - ] keep
over 0 < [ bounds-error ] [ set-nth-unsafe ] if ; inline
: pop* ( seq -- ) [ length 1 - ] [ shorten ] bi ;
2007-09-20 18:09:08 -04:00
<PRIVATE
2007-09-20 18:09:08 -04:00
: move-backward ( shift from to seq -- )
2008-10-02 07:47:20 -04:00
2over = [
2012-09-28 12:16:08 -04:00
4drop
2007-09-20 18:09:08 -04:00
] [
[ [ 2over + pick ] dip move-unsafe [ 1 + ] dip ] keep
2007-09-20 18:09:08 -04:00
move-backward
] if ;
: move-forward ( shift from to seq -- )
2008-10-02 07:47:20 -04:00
2over = [
2012-09-28 12:16:08 -04:00
4drop
2007-09-20 18:09:08 -04:00
] [
[ [ pick [ dup dup ] dip + swap ] dip move-unsafe 1 - ] keep
2007-09-20 18:09:08 -04:00
move-forward
] if ;
: (open-slice) ( shift from to seq ? -- )
[
[ [ 1 - ] bi@ ] dip move-forward
2007-09-20 18:09:08 -04:00
] [
[ over - ] 2dip move-backward
2007-09-20 18:09:08 -04:00
] if ;
: open-slice ( shift from seq -- )
2008-10-02 07:47:20 -04:00
pick 0 = [
2007-09-20 18:09:08 -04:00
3drop
] [
pick over length + over
[ pick 0 > [ [ length ] keep ] dip (open-slice) ] 2dip
set-length
2007-09-20 18:09:08 -04:00
] if ;
PRIVATE>
2007-09-20 18:09:08 -04:00
: delete-slice ( from to seq -- )
check-slice [ over [ - ] dip ] dip open-slice ;
2007-09-20 18:09:08 -04:00
2009-10-28 00:41:57 -04:00
: remove-nth! ( n seq -- seq )
[ [ dup 1 + ] dip delete-slice ] keep ;
2007-09-20 18:09:08 -04:00
: snip ( from to seq -- head tail )
[ swap head ] [ swap tail ] bi-curry bi* ; inline
: snip-slice ( from to seq -- head tail )
[ swap head-slice ] [ swap tail-slice ] bi-curry bi* ; inline
: replace-slice ( new from to seq -- seq' )
snip-slice surround ;
2007-09-20 18:09:08 -04:00
2008-09-05 20:29:14 -04:00
: remove-nth ( n seq -- seq' )
[ [ { } ] dip dup 1 + ] dip replace-slice ;
2008-09-05 20:29:14 -04:00
2007-09-20 18:09:08 -04:00
: pop ( seq -- elt )
[ length 1 - ] keep over 0 >=
[ [ nth-unsafe ] [ shorten ] 2bi ]
[ bounds-error ] if ;
2007-09-20 18:09:08 -04:00
: exchange ( m n seq -- )
[ nip bounds-check 2drop ]
[ bounds-check 3drop ]
[ exchange-unsafe ]
3tri ;
2007-09-20 18:09:08 -04:00
2011-10-16 16:01:58 -04:00
: midpoint@ ( seq -- n ) length 2/ ; inline
2009-10-28 15:40:15 -04:00
: reverse! ( seq -- seq )
[
[ midpoint@ ] [ length ] [ ] tri
[ [ over - 1 - ] dip exchange-unsafe ] 2curry
each-integer
2009-10-28 15:40:15 -04:00
] keep ;
2007-09-20 18:09:08 -04:00
2008-08-18 21:13:24 -04:00
: reverse ( seq -- newseq )
[
2008-08-18 21:13:24 -04:00
dup [ length ] keep new-sequence
[ 0 swap copy-unsafe ] keep reverse!
2008-08-18 21:13:24 -04:00
] keep like ;
2007-09-20 18:09:08 -04:00
: sum-lengths ( seq -- n )
0 [ length + ] reduce ;
: concat-as ( seq exemplar -- newseq )
swap [ { } ] [
[ sum-lengths over new-resizable ] keep
[ append! ] each
] if-empty swap like ;
2007-09-20 18:09:08 -04:00
: concat ( seq -- newseq )
[ { } ] [ dup first concat-as ] if-empty ;
2007-09-20 18:09:08 -04:00
<PRIVATE
2007-09-20 18:09:08 -04:00
: joined-length ( seq glue -- n )
[ [ sum-lengths ] [ length 1 [-] ] bi ] dip length * + ;
2007-09-20 18:09:08 -04:00
PRIVATE>
: join-as ( seq glue exemplar -- newseq )
over empty? [ nip concat-as ] [
[
2dup joined-length over new-resizable [
[ [ push-all ] 2curry ]
[ nip [ push-all ] curry ] 2bi
interleave
] keep
] dip like
] if ;
2007-09-20 18:09:08 -04:00
: join ( seq glue -- newseq )
dup join-as ; inline
: padding ( ... seq n elt quot: ( ... seq1 seq2 -- ... newseq ) -- ... newseq )
2008-08-18 21:13:24 -04:00
[
2008-10-02 07:47:20 -04:00
[ over length [-] dup 0 = [ drop ] ] dip
2008-08-18 21:13:24 -04:00
[ <repetition> ] curry
] dip compose if ; inline
2007-09-20 18:09:08 -04:00
: pad-head ( seq n elt -- padded )
[ swap dup append-as ] padding ;
2007-09-20 18:09:08 -04:00
: pad-tail ( seq n elt -- padded )
2007-09-20 18:09:08 -04:00
[ append ] padding ;
: shorter? ( seq1 seq2 -- ? ) [ length ] bi@ < ;
: longer? ( seq1 seq2 -- ? ) [ length ] bi@ > ;
: shorter ( seq1 seq2 -- seq ) [ [ length ] bi@ <= ] 2keep ? ; inline
: longer ( seq1 seq2 -- seq ) [ [ length ] bi@ >= ] 2keep ? ; inline
2007-09-20 18:09:08 -04:00
: head? ( seq begin -- ? )
2dup shorter? [
2drop f
] [
2009-01-23 19:20:47 -05:00
[ nip ] [ length head-slice ] 2bi sequence=
2007-09-20 18:09:08 -04:00
] if ;
: tail? ( seq end -- ? )
2dup shorter? [
2drop f
] [
2009-01-23 19:20:47 -05:00
[ nip ] [ length tail-slice* ] 2bi sequence=
2007-09-20 18:09:08 -04:00
] if ;
: cut-slice ( seq n -- before-slice after-slice )
2012-09-04 22:56:10 -04:00
[ head-slice ] [ tail-slice ] 2bi ; inline
2008-02-11 01:16:30 -05:00
2008-09-05 20:29:14 -04:00
: insert-nth ( elt n seq -- seq' )
swap cut-slice [ swap suffix ] dip append ;
: halves ( seq -- first-slice second-slice )
2012-09-05 12:32:54 -04:00
dup midpoint@ cut-slice ; inline
2008-02-11 01:16:30 -05:00
2012-09-05 12:32:54 -04:00
<PRIVATE
: nth2-unsafe ( n seq -- a b )
[ nth-unsafe ] [ [ 1 + ] dip nth-unsafe ] 2bi ; inline
: nth3-unsafe ( n seq -- a b c )
2012-09-05 13:52:44 -04:00
[ nth2-unsafe ] [ [ 2 + ] dip nth-unsafe ] 2bi ; inline
2012-09-05 12:32:54 -04:00
2012-09-10 19:48:40 -04:00
: (binary-reduce) ( ... seq start quot: ( ... elt1 elt2 -- ... newelt ) from length -- ... value )
2008-02-11 02:19:53 -05:00
#! We can't use case here since combinators depends on
#! sequences
2012-09-10 19:48:40 -04:00
dup 4 < [
integer>fixnum {
2012-09-05 12:32:54 -04:00
[ 2drop nip ]
[ 2nip swap nth-unsafe ]
[ -rot [ drop swap nth2-unsafe ] dip call ]
[ -rot [ drop swap nth3-unsafe ] dip bi@ ]
2008-02-11 02:19:53 -05:00
} dispatch
] [
2012-09-10 19:48:40 -04:00
[ 2/ ] [ over - ] bi [ 2dup + ] dip
2012-09-05 12:32:54 -04:00
[ (binary-reduce) ] [ 2curry ] curry 2bi@
pick [ 3bi ] dip call
2008-07-18 20:22:59 -04:00
] if ; inline recursive
2007-09-20 18:09:08 -04:00
2012-09-05 12:32:54 -04:00
PRIVATE>
: binary-reduce ( ... seq start quot: ( ... elt1 elt2 -- ... newelt ) -- ... value )
pick length 0 max 0 swap (binary-reduce) ; inline
2007-10-12 16:30:36 -04:00
: cut ( seq n -- before after )
[ head ] [ tail ] 2bi ;
2007-09-20 18:09:08 -04:00
2007-10-12 16:30:36 -04:00
: cut* ( seq n -- before after )
[ head* ] [ tail* ] 2bi ;
2007-09-20 18:09:08 -04:00
<PRIVATE
: (start) ( subseq seq n length -- subseq seq ? )
[
[ 3dup ] dip [ + swap nth-unsafe ] keep rot nth-unsafe =
] all-integers? nip ; inline
2007-09-20 18:09:08 -04:00
PRIVATE>
: start* ( subseq seq n -- i )
pick length [ pick length swap - 1 + ] keep
[ (start) ] curry (find-integer) 2nip ;
2007-09-20 18:09:08 -04:00
: start ( subseq seq -- i ) 0 start* ; inline
: subseq? ( subseq seq -- ? ) start >boolean ;
: drop-prefix ( seq1 seq2 -- slice1 slice2 )
2dup mismatch [ 2dup min-length ] unless*
[ tail-slice ] curry bi@ ;
2007-09-20 18:09:08 -04:00
: unclip ( seq -- rest first )
2008-11-29 11:38:43 -05:00
[ rest ] [ first-unsafe ] bi ;
2007-09-20 18:09:08 -04:00
2008-05-24 22:49:48 -04:00
: unclip-last ( seq -- butlast last )
[ but-last ] [ last-unsafe ] bi ;
2008-05-05 01:13:17 -04:00
: unclip-slice ( seq -- rest-slice first )
2008-11-29 11:38:43 -05:00
[ rest-slice ] [ first-unsafe ] bi ; inline
2007-09-20 18:09:08 -04:00
: map-reduce ( ..a seq map-quot: ( ..a elt -- ..b intermediate ) reduce-quot: ( ..b prev intermediate -- ..a next ) -- ..a result )
[ [ dup first ] dip [ call ] keep ] dip compose
swapd [ 1 ] 2dip (each) (each-integer) ; inline
2010-03-09 23:29:44 -05:00
: 2map-reduce ( ..a seq1 seq2 map-quot: ( ..a elt1 elt2 -- ..b intermediate ) reduce-quot: ( ..b prev intermediate -- ..a next ) -- ..a result )
[ [ 2dup [ first ] bi@ ] dip [ call ] keep ] dip compose
[ -rot ] dip [ 1 ] 3dip (2each) (each-integer) ; inline
<PRIVATE
: (map-find) ( seq quot find-quot -- result elt )
[ [ f ] 2dip [ [ nip ] dip call dup ] curry ] dip call
2009-03-03 13:22:47 -05:00
[ [ drop f ] unless ] dip ; inline
PRIVATE>
2010-03-09 23:29:44 -05:00
: map-find ( ... seq quot: ( ... elt -- ... result/f ) -- ... result elt )
[ find ] (map-find) ; inline
2010-03-09 23:29:44 -05:00
: map-find-last ( ... seq quot: ( ... elt -- ... result/f ) -- ... result elt )
[ find-last ] (map-find) ; inline
: unclip-last-slice ( seq -- butlast-slice last )
[ but-last-slice ] [ last-unsafe ] bi ; inline
2008-05-05 01:14:43 -04:00
<PRIVATE
: (trim-head) ( seq quot -- seq n )
over [ [ not ] compose find drop ] dip
[ length or ] keep swap ; inline
: (trim-tail) ( seq quot -- seq n )
over [ [ not ] compose find-last drop ?1+ ] dip
swap ; inline
PRIVATE>
: trim-head-slice ( ... seq quot: ( ... elt -- ... ? ) -- ... slice )
(trim-head) tail-slice ; inline
: trim-head ( ... seq quot: ( ... elt -- ... ? ) -- ... newseq )
(trim-head) tail ; inline
2007-09-20 18:09:08 -04:00
: trim-tail-slice ( ... seq quot: ( ... elt -- ... ? ) -- ... slice )
(trim-tail) head-slice ; inline
: trim-tail ( ... seq quot: ( ... elt -- ... ? ) -- ... newseq )
(trim-tail) head ; inline
: trim-slice ( ... seq quot: ( ... elt -- ... ? ) -- ... slice )
[ trim-head-slice ] [ trim-tail-slice ] bi ; inline
2007-09-20 18:09:08 -04:00
: trim ( ... seq quot: ( ... elt -- ... ? ) -- ... newseq )
[ trim-slice ] [ drop ] 2bi like ; inline
2009-11-02 15:21:19 -05:00
GENERIC: sum ( seq -- n )
M: object sum 0 [ + ] binary-reduce ; inline
2008-08-18 21:13:24 -04:00
2008-02-11 01:16:30 -05:00
: product ( seq -- n ) 1 [ * ] binary-reduce ;
2009-02-02 04:33:40 -05:00
: infimum ( seq -- n ) [ ] [ min ] map-reduce ;
2008-08-18 21:13:24 -04:00
2009-02-02 04:33:40 -05:00
: supremum ( seq -- n ) [ ] [ max ] map-reduce ;
2007-11-21 03:13:23 -05:00
: map-sum ( ... seq quot: ( ... elt -- ... n ) -- ... n )
[ 0 ] 2dip [ dip + ] curry [ swap ] prepose each ; inline
2008-07-03 13:24:16 -04:00
: count ( ... seq quot: ( ... elt -- ... ? ) -- ... n )
[ 1 0 ? ] compose map-sum ; inline
: cartesian-each ( ... seq1 seq2 quot: ( ... elt1 elt2 -- ... ) -- ... )
[ with each ] 2curry each ; inline
: cartesian-map ( ... seq1 seq2 quot: ( ... elt1 elt2 -- ... newelt ) -- ... newseq )
[ with map ] 2curry map ; inline
: cartesian-product ( seq1 seq2 -- newseq )
[ { } 2sequence ] cartesian-map ;
: each-from ( ... seq quot: ( ... x -- ... ) i -- ... )
-rot (each) (each-integer) ; inline
: supremum-by ( ... seq quot: ( ... elt -- ... x ) -- ... elt )
[ keep swap ] curry [ [ first ] dip call ] 2keep [
curry 2dip pick over after?
[ 2drop ] [ [ 2drop ] 2dip ] if
] curry 1 each-from drop ; inline
: infimum-by ( ... seq quot: ( ... elt -- ... x ) -- ... elt )
[ keep swap ] curry [ [ first ] dip call ] 2keep [
curry 2dip pick over before?
[ 2drop ] [ [ 2drop ] 2dip ] if
] curry 1 each-from drop ; inline
: shortest ( seqs -- elt ) [ length ] infimum-by ;
: longest ( seqs -- elt ) [ length ] supremum-by ;
! We hand-optimize flip to such a degree because type hints
! cannot express that an array is an array of arrays yet, and
! this word happens to be performance-critical since the compiler
! itself uses it. Optimizing it like this reduced compile time.
<PRIVATE
: generic-flip ( matrix -- newmatrix )
[
[ first-unsafe length 1 ] keep
[ length min ] (each) (each-integer) iota
] keep
[ [ nth-unsafe ] with { } map-as ] curry { } map-as ; inline
USE: arrays
: array-length ( array -- len )
2008-12-09 22:50:31 -05:00
{ array } declare length>> ; inline
: array-flip ( matrix -- newmatrix )
2008-12-09 22:50:31 -05:00
{ array } declare
[
[ first-unsafe array-length 1 ] keep
[ array-length min ] (each) (each-integer) iota
] keep
[ [ { array } declare array-nth ] with { } map-as ] curry { } map-as ;
PRIVATE>
: flip ( matrix -- newmatrix )
dup empty? [
dup array? [
dup [ array? ] all?
[ array-flip ] [ generic-flip ] if
] [ generic-flip ] if
] unless ;