2009-01-05 18:32:08 -05:00
|
|
|
! 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 )
|
2008-11-23 03:44:56 -05:00
|
|
|
over [ [ new-sequence ] dip call ] dip like ; inline
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2009-08-17 23:32:21 -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
|
|
|
|
2009-08-17 23:32:21 -04:00
|
|
|
M: sequence lengthen 2dup length > [ set-length ] [ 2drop ] if ; inline
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2009-08-17 23:32:21 -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 -- )
|
2009-08-14 15:27:23 -04:00
|
|
|
[ 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
|
|
|
|
|
2008-04-26 00:12:44 -04:00
|
|
|
: push ( elt seq -- ) [ length ] [ set-nth ] bi ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
|
|
|
: bounds-check? ( n seq -- ? )
|
2008-09-12 19:08:19 -04:00
|
|
|
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 -- )
|
|
|
|
|
2009-08-17 23:32:21 -04:00
|
|
|
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
|
|
|
|
2009-08-17 23:32:21 -04:00
|
|
|
M: sequence nth-unsafe nth ; inline
|
|
|
|
M: sequence set-nth-unsafe set-nth ; inline
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2009-05-18 01:24:24 -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
|
2009-08-17 23:32:21 -04:00
|
|
|
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
|
|
|
|
|
2009-08-29 11:29:41 -04:00
|
|
|
! Integers used to support the sequence protocol
|
2009-09-08 15:18:26 -04:00
|
|
|
M: integer length ; inline
|
|
|
|
M: integer nth-unsafe drop ; inline
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2009-08-22 20:59:56 -04:00
|
|
|
INSTANCE: integer immutable-sequence
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2009-01-10 14:05:25 -05:00
|
|
|
PRIVATE>
|
|
|
|
|
2009-01-09 22:35:49 -05:00
|
|
|
! In the future, this will replace integer sequences
|
2009-01-10 14:05:25 -05:00
|
|
|
TUPLE: iota { n integer read-only } ;
|
2009-01-09 22:35:49 -05:00
|
|
|
|
|
|
|
: iota ( n -- iota ) \ iota boa ; inline
|
|
|
|
|
2009-01-10 14:05:25 -05:00
|
|
|
<PRIVATE
|
|
|
|
|
2009-08-17 23:32:21 -04:00
|
|
|
M: iota length n>> ; inline
|
|
|
|
M: iota nth-unsafe drop ; inline
|
2009-01-09 22:35:49 -05:00
|
|
|
|
|
|
|
INSTANCE: iota immutable-sequence
|
|
|
|
|
2008-12-16 02:12:36 -05:00
|
|
|
: first-unsafe ( seq -- first )
|
2008-11-29 11:38:43 -05:00
|
|
|
0 swap nth-unsafe ; inline
|
|
|
|
|
2008-12-16 02:12:36 -05:00
|
|
|
: 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
|
|
|
|
2008-12-16 02:12:36 -05: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
|
|
|
|
2008-12-16 02:12:36 -05: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 -- )
|
2009-02-02 14:43:54 -05:00
|
|
|
[ [ nth-unsafe ] curry bi@ ]
|
|
|
|
[ [ set-nth-unsafe ] curry bi@ ] 3bi ; inline
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2009-01-05 18:32:08 -05:00
|
|
|
: (head) ( seq n -- from to seq ) [ 0 ] 2dip swap ; inline
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2009-01-05 18:32:08 -05: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
|
|
|
|
2009-02-04 06:13:12 -05:00
|
|
|
: (1sequence) ( obj seq -- seq )
|
|
|
|
[ 0 swap set-nth-unsafe ] keep ; inline
|
|
|
|
|
2008-12-15 20:44:56 -05:00
|
|
|
: (2sequence) ( obj1 obj2 seq -- seq )
|
2009-01-23 19:20:47 -05:00
|
|
|
[ 1 swap set-nth-unsafe ] keep
|
2009-02-04 06:13:12 -05:00
|
|
|
(1sequence) ; inline
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2008-12-15 20:44:56 -05: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
|
|
|
|
|
2008-12-15 20:44:56 -05:00
|
|
|
: (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>
|
|
|
|
|
2009-02-04 06:13:12 -05:00
|
|
|
: 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 )
|
2009-03-21 04:17:35 -04:00
|
|
|
2dup bounds-check? [ nth-unsafe ] [ 2drop f ] if ; inline
|
2007-09-20 18:09:08 -04:00
|
|
|
|
|
|
|
MIXIN: virtual-sequence
|
|
|
|
GENERIC: virtual-seq ( seq -- seq' )
|
|
|
|
GENERIC: virtual@ ( n seq -- n' seq' )
|
|
|
|
|
2009-08-17 23:32:21 -04:00
|
|
|
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-seq like ; inline
|
|
|
|
M: virtual-sequence new-sequence virtual-seq 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
|
|
|
|
|
2009-08-17 23:32:21 -04:00
|
|
|
M: reversed virtual-seq 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 )
|
2009-02-02 14:43:54 -05:00
|
|
|
[ 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
|
2008-04-13 16:06:09 -04:00
|
|
|
slice boa ; inline
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2009-08-17 23:32:21 -04:00
|
|
|
M: slice virtual-seq seq>> ; inline
|
2008-04-07 21:07:03 -04:00
|
|
|
|
2009-08-17 23:32:21 -04:00
|
|
|
M: slice virtual@ [ from>> + ] [ seq>> ] bi ; inline
|
2008-04-07 21:07:03 -04:00
|
|
|
|
2009-08-17 23:32:21 -04:00
|
|
|
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-04-26 00:12:44 -04:00
|
|
|
|
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
|
2008-05-06 13:36:32 -04:00
|
|
|
|
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
|
|
|
|
|
2009-08-17 23:32:21 -04:00
|
|
|
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
|
|
|
|
|
2009-08-12 00:09:02 -04:00
|
|
|
ERROR: integer-length-expected obj ;
|
|
|
|
|
2008-09-12 09:18:57 -04:00
|
|
|
: check-length ( n -- n )
|
2009-08-12 00:09:02 -04:00
|
|
|
dup integer? [ integer-length-expected ] unless ; inline
|
2008-09-12 09:18:57 -04:00
|
|
|
|
2009-10-30 20:39:46 -04:00
|
|
|
TUPLE: copy-state
|
2009-10-30 23:01:51 -04:00
|
|
|
{ src-i read-only }
|
|
|
|
{ src read-only }
|
|
|
|
{ dst-i read-only }
|
|
|
|
{ dst read-only } ;
|
2009-10-30 01:42:21 -04:00
|
|
|
|
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
|
2008-11-23 03:44:56 -05:00
|
|
|
[ 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 ;
|
|
|
|
|
2008-04-26 03:01:06 -04:00
|
|
|
: rest ( seq -- tailseq ) 1 tail ;
|
2008-04-26 00:12:44 -04:00
|
|
|
|
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* ;
|
2008-05-06 13:36:32 -04:00
|
|
|
|
2007-09-20 18:09:08 -04:00
|
|
|
: copy ( src i dst -- )
|
2008-09-12 09:18:57 -04:00
|
|
|
#! 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
|
2009-08-17 23:32:21 -04:00
|
|
|
[ dup length ] dip new-sequence [ 0 swap copy ] keep ; inline
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2009-08-17 23:32:21 -04:00
|
|
|
M: immutable-sequence clone-like like ; inline
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2008-04-26 00:12:44 -04:00
|
|
|
: push-all ( src dest -- ) [ length ] [ copy ] bi ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2008-09-17 19:37:57 -04:00
|
|
|
<PRIVATE
|
|
|
|
|
2008-11-23 02:01:04 -05:00
|
|
|
: (append) ( seq1 seq2 accum -- accum )
|
|
|
|
[ [ over length ] dip copy ]
|
|
|
|
[ 0 swap copy ]
|
2008-04-26 00:12:44 -04:00
|
|
|
[ ] tri ; inline
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2008-11-23 02:01:04 -05:00
|
|
|
PRIVATE>
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2008-11-23 02:01:04 -05:00
|
|
|
: append-as ( seq1 seq2 exemplar -- newseq )
|
|
|
|
[ over length over length + ] dip
|
|
|
|
[ (append) ] new-like ; inline
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2008-11-23 02:01:04 -05: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 ]
|
2008-11-23 02:01:04 -05:00
|
|
|
[ (append) ] bi
|
|
|
|
] new-like ; inline
|
2008-09-17 19:37:57 -04:00
|
|
|
|
2008-11-23 02:01:04 -05:00
|
|
|
: 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
|
|
|
|
|
2008-11-23 02:01:04 -05: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
|
|
|
|
|
2007-09-20 18:09:08 -04:00
|
|
|
: change-nth ( i seq quot -- )
|
2008-11-23 03:44:56 -05:00
|
|
|
[ [ 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
|
|
|
|
|
2009-06-14 19:10:24 -04:00
|
|
|
: ((each)) ( seq -- n quot )
|
|
|
|
[ length ] keep [ nth-unsafe ] curry ; inline
|
|
|
|
|
2007-09-20 18:09:08 -04:00
|
|
|
: (each) ( seq quot -- n quot' )
|
2009-06-14 19:10:24 -04:00
|
|
|
[ ((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' )
|
2008-11-23 03:44:56 -05:00
|
|
|
[ [ 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 -- )
|
2008-11-23 03:44:56 -05:00
|
|
|
[ (each) ] dip collect ; inline
|
2007-09-20 18:09:08 -04:00
|
|
|
|
|
|
|
: 2nth-unsafe ( n seq1 seq2 -- elt1 elt2 )
|
2009-02-02 14:43:54 -05:00
|
|
|
[ nth-unsafe ] bi-curry@ bi ; inline
|
2007-09-20 18:09:08 -04:00
|
|
|
|
|
|
|
: (2each) ( seq1 seq2 quot -- n quot' )
|
2009-01-05 19:12:34 -05:00
|
|
|
[
|
|
|
|
[ min-length ] 2keep
|
|
|
|
[ 2nth-unsafe ] 2curry
|
|
|
|
] dip compose ; inline
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2009-01-05 19:12:34 -05:00
|
|
|
: 3nth-unsafe ( n seq1 seq2 seq3 -- elt1 elt2 elt3 )
|
2009-02-02 14:43:54 -05:00
|
|
|
[ nth-unsafe ] tri-curry@ tri ; inline
|
2008-07-22 05:44:33 -04:00
|
|
|
|
2009-01-05 18:32:08 -05:00
|
|
|
: (3each) ( seq1 seq2 seq3 quot -- n quot' )
|
2009-01-05 19:12:34 -05:00
|
|
|
[
|
2009-02-02 14:43:54 -05:00
|
|
|
[ [ length ] tri@ min min ]
|
|
|
|
[ [ 3nth-unsafe ] 3curry ] 3bi
|
2009-01-05 19:12:34 -05:00
|
|
|
] dip compose ; inline
|
2009-01-05 18:32:08 -05:00
|
|
|
|
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 )
|
2008-11-23 03:44:56 -05:00
|
|
|
pick [ [ (each) ] dip call ] dip finish-find ; inline
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2008-04-26 00:12:44 -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
|
|
|
|
|
2009-08-03 14:30:55 -04:00
|
|
|
: 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 )
|
2009-08-03 14:30:55 -04:00
|
|
|
[ (each) ] dip map-integers ; inline
|
2007-09-20 18:09:08 -04:00
|
|
|
|
|
|
|
: map ( seq quot -- newseq )
|
|
|
|
over map-as ; inline
|
|
|
|
|
2008-06-13 02:51:46 -04:00
|
|
|
: replicate ( seq quot -- newseq )
|
2009-08-22 20:59:56 -04:00
|
|
|
[ drop ] prepose map ; inline
|
2008-06-13 02:51:46 -04:00
|
|
|
|
|
|
|
: replicate-as ( seq quot exemplar -- newseq )
|
2008-11-23 03:44:56 -05:00
|
|
|
[ [ drop ] prepose ] dip map-as ; inline
|
2008-06-13 02:51:46 -04:00
|
|
|
|
2009-10-27 23:32:56 -04:00
|
|
|
: 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
|
|
|
|
|
2009-10-02 00:13:33 -04:00
|
|
|
: accumulate-as ( seq identity quot exemplar -- final newseq )
|
2009-10-28 17:10:05 -04:00
|
|
|
[ (accumulate) ] dip map-as ; inline
|
2009-10-02 00:13:33 -04:00
|
|
|
|
2007-09-20 18:09:08 -04:00
|
|
|
: accumulate ( seq identity quot -- final newseq )
|
2009-10-02 00:13:33 -04:00
|
|
|
{ } 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 -- )
|
2008-11-23 03:44:56 -05:00
|
|
|
[ [ <reversed> ] bi@ ] dip 2each ; inline
|
2007-09-20 18:09:08 -04:00
|
|
|
|
|
|
|
: 2reduce ( seq1 seq2 identity quot -- result )
|
2008-11-23 03:44:56 -05:00
|
|
|
[ -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 )
|
2009-08-03 14:30:55 -04:00
|
|
|
[ (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
|
|
|
|
|
2009-01-05 18:32:08 -05:00
|
|
|
: 3each ( seq1 seq2 seq3 quot -- )
|
|
|
|
(3each) each ; inline
|
|
|
|
|
2009-01-05 19:12:34 -05:00
|
|
|
: 3map-as ( seq1 seq2 seq3 quot exemplar -- newseq )
|
2009-08-03 14:30:55 -04:00
|
|
|
[ (3each) ] dip map-integers ; inline
|
2009-01-05 19:12:34 -05:00
|
|
|
|
2009-01-05 18:32:08 -05:00
|
|
|
: 3map ( seq1 seq2 seq3 quot -- newseq )
|
2009-01-05 19:12:34 -05:00
|
|
|
[ pick ] dip swap 3map-as ; inline
|
2009-01-05 18:32:08 -05:00
|
|
|
|
2008-04-26 00:12:44 -04:00
|
|
|
: 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
|
|
|
|
|
2008-04-26 00:12:44 -04:00
|
|
|
: 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 )
|
2009-05-01 20:58:24 -04:00
|
|
|
[ [ 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 -- )
|
2008-11-23 03:44:56 -05:00
|
|
|
[ 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
|
|
|
|
2008-04-26 00:12:44 -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 -- )
|
2008-11-23 03:44:56 -05:00
|
|
|
[ 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 )
|
2009-02-02 14:43:54 -05:00
|
|
|
over [ 2pusher [ each ] 2dip ] dip [ like ] curry bi@ ; inline
|
2008-09-05 20:32:19 -04:00
|
|
|
|
2009-10-22 15:35:27 -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 )
|
2009-10-22 15:35:27 -04:00
|
|
|
V{ } accumulator-for ; inline
|
2008-06-15 01:32:48 -04:00
|
|
|
|
2009-02-28 16:31:34 -05: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
|
|
|
|
2009-02-28 16:31:34 -05:00
|
|
|
: produce ( pred quot -- seq )
|
2008-09-27 18:54:44 -04:00
|
|
|
{ } produce-as ; inline
|
2007-10-21 15:28:35 -04:00
|
|
|
|
2008-04-02 22:27:49 -04:00
|
|
|
: follow ( obj quot -- seq )
|
2009-02-28 16:31:34 -05:00
|
|
|
[ dup ] swap [ keep ] curry produce nip ; inline
|
2008-04-02 22:27:49 -04:00
|
|
|
|
2008-07-07 20:36:33 -04:00
|
|
|
: each-index ( seq quot -- )
|
2009-06-14 19:10:24 -04:00
|
|
|
(each-index) each-integer ; inline
|
2008-07-07 20:36:33 -04:00
|
|
|
|
2009-02-01 20:14:43 -05:00
|
|
|
: interleave ( seq between quot -- )
|
2009-06-14 19:22:31 -04:00
|
|
|
pick empty? [ 3drop ] [
|
|
|
|
[ [ drop first-unsafe ] dip call ]
|
2009-06-16 15:05:39 -04:00
|
|
|
[ [ rest-slice ] 2dip [ bi* ] 2curry each ]
|
2009-06-14 19:22:31 -04:00
|
|
|
3bi
|
|
|
|
] if ; inline
|
2009-02-01 20:14:43 -05:00
|
|
|
|
2009-04-04 21:22:49 -04:00
|
|
|
: map-index ( seq quot -- newseq )
|
2009-06-14 19:10:24 -04:00
|
|
|
[ dup length iota ] dip 2map ; inline
|
2008-07-07 20:36:33 -04:00
|
|
|
|
|
|
|
: 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
|
|
|
|
2008-04-26 00:12:44 -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
|
|
|
|
2008-04-26 00:12:44 -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
|
|
|
|
|
2009-01-05 18:32:08 -05:00
|
|
|
: (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 )
|
2009-01-05 18:32:08 -05:00
|
|
|
swap V{ } clone
|
|
|
|
[ [ (indices) ] 2curry each-index ] keep ;
|
2008-09-12 17:03:47 -04:00
|
|
|
|
2008-09-12 19:08:19 -04:00
|
|
|
: nths ( indices seq -- seq' )
|
|
|
|
[ nth ] curry map ;
|
2008-08-13 19:56:41 -04:00
|
|
|
|
2009-01-29 23:19:07 -05: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 -- ? )
|
2009-01-29 23:19:07 -05:00
|
|
|
[ = ] with any? ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2009-10-28 16:02:00 -04:00
|
|
|
: member-eq? ( elt seq -- ? )
|
2009-01-29 23:19:07 -05:00
|
|
|
[ eq? ] with any? ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2008-10-19 04:34:58 -04:00
|
|
|
: remove ( elt seq -- newseq )
|
2008-04-26 00:12:44 -04:00
|
|
|
[ = 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 )
|
2009-05-01 20:58:24 -04:00
|
|
|
[ 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 ;
|
|
|
|
|
2009-04-09 05:49:54 -04:00
|
|
|
: assert-sequence= ( a b -- )
|
2009-04-13 15:40:55 -04:00
|
|
|
2dup sequence= [ 2drop ] [ assert-sequence ] if ;
|
2009-04-09 05:49:54 -04:00
|
|
|
|
2008-04-07 21:07:03 -04:00
|
|
|
: 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
|
2008-04-07 21:07:03 -04:00
|
|
|
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
|
2008-04-07 21:07:03 -04:00
|
|
|
|
|
|
|
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 =
|
2008-04-26 00:12:44 -04:00
|
|
|
[ 3drop ] [ [ nth swap ] [ set-nth ] bi ] if ; inline
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2008-09-17 19:37:57 -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
|
2009-05-01 20:58:24 -04:00
|
|
|
[ 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
|
|
|
|
2008-09-17 19:37:57 -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
|
|
|
|
2008-03-31 17:28:21 -04:00
|
|
|
: prefix ( seq elt -- newseq )
|
2009-05-01 20:58:24 -04:00
|
|
|
over [ over length 1 + ] dip [
|
2008-03-31 17:28:21 -04:00
|
|
|
[ 0 swap set-nth-unsafe ] keep
|
|
|
|
[ 1 swap copy ] keep
|
|
|
|
] new-like ;
|
|
|
|
|
|
|
|
: suffix ( seq elt -- newseq )
|
2009-05-01 20:58:24 -04:00
|
|
|
over [ over length 1 + ] dip [
|
2008-11-23 03:44:56 -05:00
|
|
|
[ [ over length ] dip set-nth-unsafe ] keep
|
2008-03-31 17:28:21 -04:00
|
|
|
[ 0 swap copy ] keep
|
2007-09-20 18:09:08 -04:00
|
|
|
] new-like ;
|
|
|
|
|
2009-10-28 14:38:27 -04:00
|
|
|
: suffix! ( seq elt -- seq ) over push ;
|
|
|
|
|
|
|
|
: append! ( seq1 seq2 -- seq1 ) over push-all ;
|
|
|
|
|
2009-05-25 17:38:33 -04:00
|
|
|
: last ( seq -- elt ) [ length 1 - ] [ nth ] bi ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2009-07-28 12:51:47 -04:00
|
|
|
: set-last ( elt seq -- ) [ length 1 - ] keep set-nth ;
|
|
|
|
|
2009-05-01 20:58:24 -04:00
|
|
|
: pop* ( seq -- ) [ length 1 - ] [ shorten ] bi ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2008-09-17 19:37:57 -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
|
|
|
|
] [
|
2009-05-01 20:58:24 -04:00
|
|
|
[ [ 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
|
|
|
|
] [
|
2009-05-01 20:58:24 -04:00
|
|
|
[ [ 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 ? -- )
|
|
|
|
[
|
2009-05-01 20:58:24 -04:00
|
|
|
[ [ 1 - ] bi@ ] dip move-forward
|
2007-09-20 18:09:08 -04:00
|
|
|
] [
|
2008-11-23 03:44:56 -05: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
|
|
|
|
] [
|
2008-11-23 03:44:56 -05:00
|
|
|
pick over length + over
|
|
|
|
[ pick 0 > [ [ length ] keep ] dip (open-slice) ] 2dip
|
|
|
|
set-length
|
2007-09-20 18:09:08 -04:00
|
|
|
] if ;
|
|
|
|
|
2009-01-29 04:04:23 -05:00
|
|
|
PRIVATE>
|
|
|
|
|
2007-09-20 18:09:08 -04:00
|
|
|
: delete-slice ( from to seq -- )
|
2008-11-23 03:44:56 -05:00
|
|
|
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
|
|
|
|
2009-02-02 14:43:54 -05: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
|
|
|
|
|
2009-01-29 04:04:23 -05:00
|
|
|
: replace-slice ( new from to seq -- seq' )
|
2009-02-02 14:43:54 -05:00
|
|
|
snip-slice surround ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2008-09-05 20:29:14 -04:00
|
|
|
: remove-nth ( n seq -- seq' )
|
2009-05-01 20:58:24 -04:00
|
|
|
[ [ { } ] 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 )
|
2009-05-01 20:58:24 -04:00
|
|
|
[ length 1 - ] [ [ nth ] [ shorten ] 2bi ] bi ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
|
|
|
: exchange ( m n seq -- )
|
2009-02-02 14:43:54 -05:00
|
|
|
[ 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 ;
|
|
|
|
|
2009-05-10 17:39:51 -04:00
|
|
|
: concat-as ( seq exemplar -- newseq )
|
|
|
|
swap [ { } ] [
|
|
|
|
[ sum-lengths over new-resizable ] keep
|
2009-10-28 16:29:01 -04:00
|
|
|
[ append! ] each
|
2009-05-10 17:39:51 -04:00
|
|
|
] if-empty swap like ;
|
|
|
|
|
2007-09-20 18:09:08 -04:00
|
|
|
: concat ( seq -- newseq )
|
2009-05-10 17:39:51 -04:00
|
|
|
[ { } ] [ dup first concat-as ] if-empty ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2008-09-17 19:37:57 -04:00
|
|
|
<PRIVATE
|
|
|
|
|
2007-09-20 18:09:08 -04:00
|
|
|
: joined-length ( seq glue -- n )
|
2008-12-15 22:20:32 -05:00
|
|
|
[ [ sum-lengths ] [ length 1 [-] ] bi ] dip length * + ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2008-09-17 19:37:57 -04:00
|
|
|
PRIVATE>
|
|
|
|
|
2007-09-20 18:09:08 -04:00
|
|
|
: join ( seq glue -- newseq )
|
2009-05-10 17:39:51 -04:00
|
|
|
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
|
|
|
|
2009-01-29 23:19:07 -05:00
|
|
|
: pad-head ( seq n elt -- padded )
|
2008-11-23 02:01:04 -05:00
|
|
|
[ swap dup append-as ] padding ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2009-01-29 23:19:07 -05:00
|
|
|
: pad-tail ( seq n elt -- padded )
|
2007-09-20 18:09:08 -04:00
|
|
|
[ append ] padding ;
|
|
|
|
|
2008-09-17 19:37:57 -04:00
|
|
|
: 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 ;
|
|
|
|
|
2008-09-17 19:37:57 -04:00
|
|
|
: cut-slice ( seq n -- before-slice after-slice )
|
2008-04-26 00:12:44 -04:00
|
|
|
[ 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
|
|
|
|
|
2008-09-17 19:37:57 -04:00
|
|
|
: 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 ]
|
2008-11-23 03:44:56 -05:00
|
|
|
[ [ drop first2 ] dip call ]
|
|
|
|
[ [ drop first3 ] dip bi@ ]
|
2008-02-11 02:19:53 -05:00
|
|
|
} dispatch
|
|
|
|
] [
|
|
|
|
drop
|
2008-11-23 03:44:56 -05:00
|
|
|
[ 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 )
|
2008-04-26 00:12:44 -04:00
|
|
|
[ head ] [ tail ] 2bi ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2007-10-12 16:30:36 -04:00
|
|
|
: cut* ( seq n -- before after )
|
2008-04-26 00:12:44 -04:00
|
|
|
[ 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 [
|
2008-11-23 03:44:56 -05:00
|
|
|
[ 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
|
2008-04-26 00:12:44 -04:00
|
|
|
[ (start) ] find-from
|
2008-11-23 03:44:56 -05:00
|
|
|
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*
|
2009-02-02 14:43:54 -05:00
|
|
|
[ 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 )
|
2009-05-25 17:38:33 -04:00
|
|
|
[ but-last ] [ last ] bi ;
|
2008-05-05 01:13:17 -04:00
|
|
|
|
2008-09-17 19:37:57 -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
|
|
|
|
2008-09-17 19:37:57 -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
|
|
|
|
|
2008-09-12 12:29:12 -04:00
|
|
|
: 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
|
|
|
|
|
2009-05-14 23:31:29 -04:00
|
|
|
<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
|
2008-09-12 12:29:12 -04:00
|
|
|
|
2009-05-14 23:31:29 -04:00
|
|
|
PRIVATE>
|
|
|
|
|
|
|
|
: map-find ( seq quot -- result elt )
|
|
|
|
[ find ] (map-find) ; inline
|
|
|
|
|
|
|
|
: map-find-last ( seq quot -- result elt )
|
|
|
|
[ find-last ] (map-find) ; inline
|
|
|
|
|
2008-09-17 19:37:57 -04:00
|
|
|
: unclip-last-slice ( seq -- butlast-slice last )
|
2009-05-25 17:38:33 -04:00
|
|
|
[ 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 )
|
2009-02-05 04:29:59 -05:00
|
|
|
dup slice? [ { } like ] when
|
|
|
|
[ drop 0 ] [ length ] [ ] tri <slice> ;
|
2007-09-20 18:09:08 -04:00
|
|
|
inline
|
|
|
|
|
2009-02-04 01:51:00 -05:00
|
|
|
<PRIVATE
|
2008-09-05 18:40:57 -04:00
|
|
|
|
2009-02-04 01:51:00 -05:00
|
|
|
: (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
|
|
|
|
|
2009-01-29 23:19:07 -05:00
|
|
|
: trim-head ( seq quot -- newseq )
|
2009-02-04 01:51:00 -05:00
|
|
|
(trim-head) tail ; inline
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2009-01-29 23:19:07 -05:00
|
|
|
: trim-tail-slice ( seq quot -- slice )
|
2009-02-04 01:51:00 -05:00
|
|
|
(trim-tail) head-slice ; inline
|
2008-09-05 18:40:57 -04:00
|
|
|
|
2009-01-29 23:19:07 -05:00
|
|
|
: trim-tail ( seq quot -- newseq )
|
2009-02-04 01:51:00 -05:00
|
|
|
(trim-tail) head ; inline
|
2008-09-05 18:40:57 -04:00
|
|
|
|
|
|
|
: trim-slice ( seq quot -- slice )
|
2009-01-29 23:19:07 -05:00
|
|
|
[ trim-head-slice ] [ trim-tail-slice ] bi ; inline
|
2007-09-20 18:09:08 -04:00
|
|
|
|
|
|
|
: trim ( seq quot -- newseq )
|
2009-02-04 01:51:00 -05:00
|
|
|
[ trim-slice ] [ drop ] 2bi like ; inline
|
2007-10-14 20:38:23 -04:00
|
|
|
|
2008-02-11 01:16:30 -05:00
|
|
|
: sum ( seq -- n ) 0 [ + ] binary-reduce ;
|
2008-08-18 21:13:24 -04:00
|
|
|
|
2008-02-11 01:16:30 -05:00
|
|
|
: product ( seq -- n ) 1 [ * ] binary-reduce ;
|
2007-10-14 20:38:23 -04:00
|
|
|
|
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 )
|
2009-02-05 04:29:59 -05:00
|
|
|
[ 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
|
2008-12-06 12:17:19 -05:00
|
|
|
|
|
|
|
! 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
|
2008-12-06 12:17:19 -05:00
|
|
|
[ [ 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
|
2008-12-06 12:17:19 -05:00
|
|
|
|
|
|
|
: 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
|
2009-07-28 22:33:12 -04:00
|
|
|
[ [ { array } declare array-nth ] with { } map-as ] curry { } map-as ;
|
2008-12-06 12:17:19 -05:00
|
|
|
|
|
|
|
PRIVATE>
|
|
|
|
|
|
|
|
: flip ( matrix -- newmatrix )
|
|
|
|
dup empty? [
|
|
|
|
dup array? [
|
|
|
|
dup [ array? ] all?
|
|
|
|
[ array-flip ] [ generic-flip ] if
|
|
|
|
] [ generic-flip ] if
|
|
|
|
] unless ;
|