factor/core/sequences/sequences.factor

975 lines
25 KiB
Factor
Raw Normal View History

! Copyright (C) 2005, 2009 Slava Pestov, Daniel Ehrenberg.
2007-09-20 18:09:08 -04:00
! See http://factorcode.org/license.txt for BSD license.
2008-06-29 22:37:57 -04:00
USING: accessors kernel kernel.private slots.private math
math.private math.order ;
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
2007-09-20 18:09:08 -04:00
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 ( seq quot1 quot2 -- )
[ 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
: bounds-check? ( n seq -- ? )
dupd length < [ 0 >= ] [ drop f ] if ; inline
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
: bounds-check ( n seq -- n seq )
2dup bounds-check? [ bounds-error ] unless ; inline
MIXIN: immutable-sequence
2008-03-20 16:00:49 -04:00
ERROR: immutable seq ;
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
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
! Integers used to support the sequence protocol
M: integer length ; inline
M: integer nth-unsafe drop ; inline
2007-09-20 18:09:08 -04:00
INSTANCE: integer immutable-sequence
2007-09-20 18:09:08 -04:00
PRIVATE>
! In the future, this will replace integer sequences
TUPLE: iota { n integer read-only } ;
: iota ( n -- iota ) \ iota boa ; inline
<PRIVATE
M: iota length n>> ; inline
M: iota nth-unsafe drop ; inline
INSTANCE: iota immutable-sequence
: first-unsafe ( seq -- first )
2008-11-29 11:38:43 -05:00
0 swap nth-unsafe ; inline
: first2-unsafe ( seq -- first second )
2008-11-29 11:38:43 -05:00
[ first-unsafe ] [ 1 swap nth-unsafe ] bi ; inline
2007-09-20 18:09:08 -04:00
: first3-unsafe ( seq -- first second third )
2008-11-29 11:38:43 -05:00
[ first2-unsafe ] [ 2 swap nth-unsafe ] bi ; inline
2007-09-20 18:09:08 -04:00
: first4-unsafe ( seq -- first second third fourth )
2008-11-29 11:38:43 -05:00
[ first3-unsafe ] [ 3 swap nth-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 )
2009-01-23 19:20:47 -05:00
[ 1 swap set-nth-unsafe ] keep
(1sequence) ; inline
2007-09-20 18:09:08 -04:00
: (3sequence) ( obj1 obj2 obj3 seq -- seq )
2009-01-23 19:20:47 -05:00
[ 2 swap set-nth-unsafe ] keep
2007-09-20 18:09:08 -04:00
(2sequence) ; inline
: (4sequence) ( obj1 obj2 obj3 obj4 seq -- seq )
2009-01-23 19:20:47 -05:00
[ 3 swap set-nth-unsafe ] keep
2007-09-20 18:09:08 -04:00
(3sequence) ; inline
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
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
2009-03-11 01:10:27 -04:00
TUPLE: slice-error from to seq reason ;
: slice-error ( from to seq ? string -- from to seq )
[ \ slice-error boa throw ] 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" slice-error ]
2009-03-12 21:37:26 -04:00
[ [ drop ] 2dip length > "end > sequence" slice-error ]
[ drop > "start > end" slice-error ]
3tri ; inline
2007-09-20 18:09:08 -04:00
: <slice> ( from to seq -- slice )
dup slice? [ collapse-slice ] when
2007-09-25 21:09:46 -04:00
check-slice
slice boa ; 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 ;
2008-07-18 20:22:59 -04:00
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
2009-10-30 20:39:46 -04:00
: check-copy ( src n dst -- src n dst )
3dup over 0 < [ bounds-error ] when
[ swap length + ] dip lengthen ; 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 -- )
#! The check-length call forces partial dispatch
2009-10-30 20:39:46 -04:00
[ [ length check-length 0 ] keep ] 2dip
check-copy <copy> (copy) drop ; inline
2007-09-20 18:09:08 -04:00
M: sequence clone-like
[ dup length ] dip new-sequence [ 0 swap copy ] 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 dest -- ) [ length ] [ copy ] bi ;
2007-09-20 18:09:08 -04:00
<PRIVATE
: (append) ( seq1 seq2 accum -- accum )
[ [ over length ] dip copy ]
[ 0 swap copy ]
[ ] 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 )
[ over length over length + ] 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 ]
[ (append) ] bi
] new-like ; inline
: append ( seq1 seq2 -- newseq ) over append-as ;
2007-09-20 18:09:08 -04:00
2008-03-19 20:15:43 -04:00
: prepend ( seq1 seq2 -- newseq ) swap append ; inline
: 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
2007-09-20 18:09:08 -04:00
: change-nth ( i seq quot -- )
[ [ nth ] dip call ] 3keep drop set-nth ; 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
: (2each) ( seq1 seq2 quot -- n quot' )
[
[ min-length ] 2keep
[ 2nth-unsafe ] 2curry
] dip compose ; inline
2007-09-20 18:09:08 -04:00
: 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
PRIVATE>
: each ( seq quot -- )
(each) each-integer ; inline
: reduce ( seq identity quot -- result )
swapd each ; inline
: map-integers ( len quot exemplar -- newseq )
[ over ] dip [ [ collect ] keep ] new-like ; inline
2007-09-20 18:09:08 -04:00
: map-as ( seq quot exemplar -- newseq )
[ (each) ] dip map-integers ; inline
2007-09-20 18:09:08 -04:00
: map ( seq quot -- newseq )
over map-as ; inline
: replicate ( seq quot -- newseq )
[ drop ] prepose map ; inline
: replicate-as ( seq quot exemplar -- newseq )
[ [ drop ] prepose ] dip map-as ; inline
: map! ( seq quot -- seq )
over [ map-into ] keep ; inline
2007-09-20 18:09:08 -04:00
2009-10-28 17:10:05 -04:00
: (accumulate) ( seq identity quot -- seq identity quot )
[ swap ] dip [ curry keep ] curry ; inline
: accumulate-as ( seq identity quot exemplar -- final newseq )
2009-10-28 17:10:05 -04:00
[ (accumulate) ] dip map-as ; inline
2007-09-20 18:09:08 -04:00
: accumulate ( seq identity quot -- final newseq )
{ } accumulate-as ; inline
2007-09-20 18:09:08 -04:00
2009-10-28 17:10:05 -04:00
: accumulate! ( seq identity quot -- final seq )
(accumulate) map! ; inline
2007-09-20 18:09:08 -04:00
: 2each ( seq1 seq2 quot -- )
(2each) each-integer ; inline
: 2reverse-each ( seq1 seq2 quot -- )
[ [ <reversed> ] bi@ ] dip 2each ; inline
2007-09-20 18:09:08 -04:00
: 2reduce ( seq1 seq2 identity quot -- result )
[ -rot ] dip 2each ; inline
2007-09-20 18:09:08 -04:00
2008-07-13 20:55:54 -04:00
: 2map-as ( seq1 seq2 quot exemplar -- newseq )
[ (2each) ] dip map-integers ; inline
2007-09-20 18:09:08 -04:00
2008-07-13 20:55:54 -04:00
: 2map ( seq1 seq2 quot -- newseq )
pick 2map-as ; inline
2007-09-20 18:09:08 -04:00
: 2all? ( seq1 seq2 quot -- ? )
(2each) all-integers? ; inline
: 3each ( seq1 seq2 seq3 quot -- )
(3each) each ; inline
: 3map-as ( seq1 seq2 seq3 quot exemplar -- newseq )
[ (3each) ] dip map-integers ; inline
: 3map ( seq1 seq2 seq3 quot -- newseq )
[ pick ] dip swap 3map-as ; inline
: find-from ( n seq quot -- i elt )
[ (find-integer) ] (find-from) ; inline
2007-09-20 18:09:08 -04:00
: find ( seq quot -- i elt )
[ find-integer ] (find) ; inline
: find-last-from ( n seq quot -- i elt )
[ nip find-last-integer ] (find-from) ; inline
2007-09-20 18:09:08 -04:00
: find-last ( seq quot -- i elt )
[ [ 1 - ] dip find-last-integer ] (find) ; inline
2007-09-20 18:09:08 -04:00
: all? ( seq quot -- ? )
(each) all-integers? ; inline
: push-if ( elt quot accum -- )
[ keep ] dip rot [ push ] [ 2drop ] if ; inline
2007-09-20 18:09:08 -04:00
2009-10-22 18:28:01 -04:00
: pusher-for ( quot exemplar -- quot accum )
[ length ] keep new-resizable [ [ push-if ] 2curry ] keep ; inline
2007-09-20 18:09:08 -04:00
: pusher ( quot -- quot accum )
2009-10-22 18:28:01 -04:00
V{ } pusher-for ; inline
: filter-as ( seq quot exemplar -- subseq )
dup [ pusher-for [ each ] dip ] curry dip like ; inline
2007-09-20 18:09:08 -04:00
: filter ( seq quot -- subseq )
2009-10-22 18:28:01 -04:00
over filter-as ; inline
2007-09-20 18:09:08 -04:00
2008-09-05 20:32:19 -04:00
: push-either ( elt quot accum1 accum2 -- )
[ keep swap ] 2dip ? push ; inline
2008-09-05 20:32:19 -04:00
: 2pusher ( quot -- quot accum1 accum2 )
V{ } clone V{ } clone [ [ push-either ] 3curry ] 2keep ; inline
: partition ( seq quot -- trueseq falseseq )
over [ 2pusher [ each ] 2dip ] dip [ like ] curry bi@ ; inline
2008-09-05 20:32:19 -04:00
: accumulator-for ( quot exemplar -- quot' vec )
[ length ] keep new-resizable [ [ push ] curry compose ] keep ; inline
2008-06-15 01:32:48 -04:00
: accumulator ( quot -- quot' vec )
V{ } accumulator-for ; inline
2008-06-15 01:32:48 -04:00
: produce-as ( pred quot exemplar -- seq )
2009-10-22 15:53:16 -04:00
dup [ accumulator-for [ while ] dip ] curry dip like ; inline
2008-09-27 18:54:44 -04:00
: produce ( pred quot -- seq )
2008-09-27 18:54:44 -04:00
{ } produce-as ; inline
2007-10-21 15:28:35 -04:00
: follow ( obj quot -- seq )
[ dup ] swap [ keep ] curry produce nip ; inline
: each-index ( seq quot -- )
(each-index) each-integer ; inline
2009-02-01 20:14:43 -05:00
: interleave ( seq between quot -- )
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
2009-04-04 21:22:49 -04:00
: map-index ( seq quot -- newseq )
[ dup length iota ] dip 2map ; inline
: reduce-index ( seq identity quot -- )
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
: nths ( indices seq -- seq' )
[ nth ] curry map ;
2008-08-13 19:56:41 -04:00
: any? ( seq quot -- ? )
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 ;
2007-09-20 18:09:08 -04:00
: mismatch ( seq1 seq2 -- i )
[ min-length iota ] 2keep
2007-09-20 18:09:08 -04:00
[ 2nth-unsafe = not ] 2curry
find drop ; inline
M: sequence <=>
2dup mismatch
[ -rot 2nth-unsafe <=> ] [ [ length ] compare ] if* ;
: sequence= ( seq1 seq2 -- ? )
2008-10-02 07:47:20 -04:00
2dup [ length ] bi@ =
2007-09-20 18:09:08 -04:00
[ mismatch not ] [ 2drop f ] if ; inline
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 ;
: sequence-hashcode-step ( oldhash newpart -- newhash )
2008-09-02 03:02:05 -04:00
>fixnum swap [
2008-12-10 18:26:54 -05:00
[ -2 fixnum-shift-fast ] [ 5 fixnum-shift-fast ] bi
fixnum+fast fixnum+fast
] keep fixnum-bitxor ; inline
: sequence-hashcode ( n seq -- x )
2008-12-10 18:26:54 -05:00
[ 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
2009-10-28 01:44:05 -04:00
: (filter!) ( quot: ( elt -- ? ) store scan seq -- )
2007-09-20 18:09:08 -04:00
2dup length < [
2008-10-19 04:34:58 -04:00
[ move ] 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>
2009-10-28 01:44:05 -04:00
: filter! ( seq quot -- seq )
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 [
[ 0 swap set-nth-unsafe ] keep
[ 1 swap copy ] keep
] new-like ;
: suffix ( seq elt -- newseq )
over [ over length 1 + ] dip [
[ [ over length ] dip set-nth-unsafe ] keep
[ 0 swap copy ] 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
: last ( seq -- elt ) [ length 1 - ] [ nth ] bi ;
2007-09-20 18:09:08 -04:00
: set-last ( elt seq -- ) [ length 1 - ] keep set-nth ;
: 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 = [
2007-09-20 18:09:08 -04:00
2drop 2drop
] [
[ [ 2over + pick ] dip move [ 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 = [
2007-09-20 18:09:08 -04:00
2drop 2drop
] [
[ [ pick [ dup dup ] dip + swap ] dip move 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 - ] [ [ nth ] [ shorten ] 2bi ] bi ;
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
2009-10-28 15:40:15 -04:00
: reverse! ( seq -- seq )
[
[ length 2/ iota ] [ length ] [ ] tri
[ [ over - 1 - ] dip exchange-unsafe ] 2curry each
] keep ;
2007-09-20 18:09:08 -04:00
2008-08-18 21:13:24 -04:00
: reverse ( seq -- newseq )
[
dup [ length ] keep new-sequence
2009-10-28 15:40:15 -04:00
[ 0 swap copy ] 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>
2007-09-20 18:09:08 -04:00
: join ( seq glue -- newseq )
dup empty? [ concat-as ] [
[
2dup joined-length over new-resizable [
[ [ push-all ] 2curry ] [ [ nip push-all ] 2curry ] 2bi
interleave
] keep
] keep like
] if ;
2007-09-20 18:09:08 -04:00
: padding ( seq n elt quot -- 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@ < ;
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 )
[ head-slice ] [ tail-slice ] 2bi ;
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 ;
2008-02-11 01:16:30 -05:00
: midpoint@ ( seq -- n ) length 2/ ; inline
: halves ( seq -- first-slice second-slice )
2008-02-11 01:16:30 -05:00
dup midpoint@ cut-slice ;
2008-07-18 20:22:59 -04:00
: binary-reduce ( seq start quot: ( elt1 elt2 -- newelt ) -- value )
2008-02-11 02:19:53 -05:00
#! We can't use case here since combinators depends on
#! sequences
pick length dup 0 3 between? [
>fixnum {
[ drop nip ]
[ 2drop first ]
[ [ drop first2 ] dip call ]
[ [ drop first3 ] dip bi@ ]
2008-02-11 02:19:53 -05:00
} dispatch
] [
drop
[ halves ] 2dip
2008-03-29 21:36:58 -04:00
[ [ binary-reduce ] 2curry bi@ ] keep
2008-02-11 02:19:53 -05:00
call
2008-07-18 20:22:59 -04:00
] if ; inline recursive
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
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 -- subseq seq ? )
2009-08-02 19:18:31 -04:00
pick length iota [
[ 3dup ] dip [ + swap nth-unsafe ] keep rot nth-unsafe =
2007-09-20 18:09:08 -04:00
] all? nip ; inline
PRIVATE>
: start* ( subseq seq n -- i )
2009-08-02 19:18:31 -04:00
pick length pick length swap - 1 + iota
[ (start) ] find-from
swap [ 3drop ] dip ;
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 ] 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
: 2unclip-slice ( seq1 seq2 -- rest-slice1 rest-slice2 first1 first2 )
2008-09-12 12:32:40 -04:00
[ unclip-slice ] bi@ swapd ; inline
: map-reduce ( seq map-quot reduce-quot -- result )
[ [ unclip-slice ] dip [ call ] keep ] dip
compose reduce ; inline
: 2map-reduce ( seq1 seq2 map-quot reduce-quot -- result )
[ [ 2unclip-slice ] dip [ call ] keep ] dip
compose 2reduce ; 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>
: map-find ( seq quot -- result elt )
[ find ] (map-find) ; inline
: map-find-last ( seq quot -- result elt )
[ find-last ] (map-find) ; inline
: unclip-last-slice ( seq -- butlast-slice last )
[ but-last-slice ] [ last ] bi ; inline
2008-05-05 01:14:43 -04:00
2007-09-20 18:09:08 -04:00
: <flat-slice> ( seq -- slice )
dup slice? [ { } like ] when
[ drop 0 ] [ length ] [ ] tri <slice> ;
2007-09-20 18:09:08 -04:00
inline
<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 -- slice )
(trim-head) tail-slice ; inline
: trim-head ( seq quot -- newseq )
(trim-head) tail ; inline
2007-09-20 18:09:08 -04:00
: trim-tail-slice ( seq quot -- slice )
(trim-tail) head-slice ; inline
: trim-tail ( seq quot -- newseq )
(trim-tail) head ; inline
: trim-slice ( seq quot -- slice )
[ trim-head-slice ] [ trim-tail-slice ] bi ; inline
2007-09-20 18:09:08 -04:00
: trim ( seq quot -- 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
2009-10-29 15:34:04 -04:00
: map-sum ( seq quot -- n )
[ 0 ] 2dip [ dip + ] curry [ swap ] prepose each ; inline
2008-07-03 13:24:16 -04:00
2009-10-29 15:34:04 -04:00
: count ( seq quot -- n ) [ 1 0 ? ] compose map-sum ; inline
! 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 )
2009-08-22 20:56:16 -04:00
[ dup first length [ length min ] reduce 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
2009-08-22 20:56:16 -04:00
[ dup first array-length [ array-length min ] reduce 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 ;