| 
									
										
										
										
											2008-02-11 02:19:53 -05:00
										 |  |  | ! Copyright (C) 2005, 2008 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
										 |  |  | 
 | 
					
						
							|  |  |  | M: sequence like drop ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 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 ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-12 02:08:30 -04:00
										 |  |  | M: sequence shorten 2dup length < [ set-length ] [ 2drop ] if ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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-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 -- )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: sequence nth bounds-check nth-unsafe ;
 | 
					
						
							|  |  |  | M: sequence set-nth bounds-check set-nth-unsafe ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: sequence nth-unsafe nth ;
 | 
					
						
							|  |  |  | M: sequence set-nth-unsafe set-nth ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ! The f object supports the sequence protocol trivially | 
					
						
							|  |  |  | M: f length drop 0 ;
 | 
					
						
							|  |  |  | M: f nth-unsafe nip ;
 | 
					
						
							| 
									
										
										
										
											2008-09-06 18:10:32 -04:00
										 |  |  | M: f like drop [ f ] when-empty ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | INSTANCE: f immutable-sequence | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ! Integers support the sequence protocol | 
					
						
							|  |  |  | M: integer length ;
 | 
					
						
							|  |  |  | M: integer nth-unsafe drop ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | INSTANCE: integer 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 -- )
 | 
					
						
							| 
									
										
										
										
											2008-11-23 03:44:56 -05:00
										 |  |  |     [ tuck [ nth-unsafe ] 2bi@ ] | 
					
						
							|  |  |  |     [ tuck [ set-nth-unsafe ] 2bi@ ] 3bi ; inline
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-12-29 11:36:20 -05:00
										 |  |  | : (head) ( seq n -- from to seq ) 0 spin ; inline
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : (tail) ( seq n -- from to seq ) over length rot ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-15 20:44:56 -05:00
										 |  |  | : (2sequence) ( obj1 obj2 seq -- seq )
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  |     tuck 1 swap set-nth-unsafe | 
					
						
							|  |  |  |     tuck 0 swap set-nth-unsafe ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-15 20:44:56 -05:00
										 |  |  | : (3sequence) ( obj1 obj2 obj3 seq -- seq )
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  |     tuck 2 swap set-nth-unsafe | 
					
						
							|  |  |  |     (2sequence) ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-15 20:44:56 -05:00
										 |  |  | : (4sequence) ( obj1 obj2 obj3 obj4 seq -- seq )
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  |     tuck 3 swap set-nth-unsafe | 
					
						
							|  |  |  |     (3sequence) ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | PRIVATE>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : 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 )
 | 
					
						
							|  |  |  |     1 swap bounds-check nip first2-unsafe ; flushable
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : first3 ( seq -- first second third )
 | 
					
						
							|  |  |  |     2 swap bounds-check nip first3-unsafe ; flushable
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : first4 ( seq -- first second third fourth )
 | 
					
						
							|  |  |  |     3 swap bounds-check nip first4-unsafe ; flushable
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : ?nth ( n seq -- elt/f )
 | 
					
						
							|  |  |  |     2dup bounds-check? [ nth-unsafe ] [ 2drop f ] if ; flushable
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | MIXIN: virtual-sequence | 
					
						
							|  |  |  | GENERIC: virtual-seq ( seq -- seq' )
 | 
					
						
							|  |  |  | GENERIC: virtual@ ( n seq -- n' seq' )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: virtual-sequence nth virtual@ nth ;
 | 
					
						
							|  |  |  | M: virtual-sequence set-nth virtual@ set-nth ;
 | 
					
						
							|  |  |  | M: virtual-sequence nth-unsafe virtual@ nth-unsafe ;
 | 
					
						
							|  |  |  | M: virtual-sequence set-nth-unsafe virtual@ set-nth-unsafe ;
 | 
					
						
							|  |  |  | M: virtual-sequence like virtual-seq like ;
 | 
					
						
							| 
									
										
										
										
											2008-04-13 13:54:58 -04:00
										 |  |  | M: virtual-sequence new-sequence virtual-seq new-sequence ;
 | 
					
						
							| 
									
										
										
										
											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 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-06-29 22:37:57 -04:00
										 |  |  | M: reversed virtual-seq seq>> ;
 | 
					
						
							| 
									
										
										
										
											2008-04-07 21:07:03 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-06-29 22:37:57 -04:00
										 |  |  | M: reversed virtual@ seq>> [ length swap - 1- ] keep ;
 | 
					
						
							| 
									
										
										
										
											2008-04-07 21:07:03 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-06-29 22:37:57 -04:00
										 |  |  | M: reversed length seq>> length ;
 | 
					
						
							| 
									
										
										
										
											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 )
 | 
					
						
							| 
									
										
										
										
											2008-11-23 03:44:56 -05:00
										 |  |  |     [ from>> ] [ seq>> ] bi [ tuck [ + ] 2bi@ ] dip ; inline
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-21 22:00:30 -05:00
										 |  |  | ERROR: slice-error from to seq reason ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-09-25 21:09:46 -04:00
										 |  |  | : check-slice ( from to seq -- from to seq )
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  |     pick 0 < [ "start < 0" slice-error ] when
 | 
					
						
							| 
									
										
										
										
											2007-09-25 21:09:46 -04:00
										 |  |  |     dup length pick < [ "end > sequence" slice-error ] when
 | 
					
						
							| 
									
										
										
										
											2008-01-11 17:02:44 -05:00
										 |  |  |     2over > [ "start > end" slice-error ] when ; 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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-06-29 22:37:57 -04:00
										 |  |  | M: slice virtual-seq seq>> ;
 | 
					
						
							| 
									
										
										
										
											2008-04-07 21:07:03 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-06-29 22:37:57 -04:00
										 |  |  | M: slice virtual@ [ from>> + ] [ seq>> ] bi ;
 | 
					
						
							| 
									
										
										
										
											2008-04-07 21:07:03 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-06-29 22:37:57 -04:00
										 |  |  | M: slice length [ to>> ] [ from>> ] bi - ;
 | 
					
						
							| 
									
										
										
										
											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 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-06-29 22:37:57 -04:00
										 |  |  | M: repetition length len>> ;
 | 
					
						
							|  |  |  | M: repetition nth-unsafe nip elt>> ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | INSTANCE: repetition immutable-sequence | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <PRIVATE
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-09-12 09:18:57 -04:00
										 |  |  | : check-length ( n -- n )
 | 
					
						
							|  |  |  |     #! Ricing. | 
					
						
							|  |  |  |     dup integer? [ "length not an integer" throw ] unless ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | : ((copy)) ( dst i src j n -- dst i src j n )
 | 
					
						
							|  |  |  |     dup -roll [ | 
					
						
							|  |  |  |         + swap nth-unsafe -roll [ | 
					
						
							|  |  |  |             + swap set-nth-unsafe | 
					
						
							|  |  |  |         ] 3keep drop
 | 
					
						
							|  |  |  |     ] 3keep ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : (copy) ( dst i src j n -- dst )
 | 
					
						
							| 
									
										
										
										
											2008-07-18 20:22:59 -04:00
										 |  |  |     dup 0 <= [ 2drop 2drop ] [ 1- ((copy)) (copy) ] if ;
 | 
					
						
							|  |  |  |     inline recursive
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : prepare-subseq ( from to seq -- dst i src j n )
 | 
					
						
							| 
									
										
										
										
											2008-09-12 09:18:57 -04:00
										 |  |  |     #! The check-length call forces partial dispatch | 
					
						
							| 
									
										
										
										
											2008-11-23 03:44:56 -05:00
										 |  |  |     [ [ swap - ] dip new-sequence dup 0 ] 3keep
 | 
					
						
							| 
									
										
										
										
											2008-09-12 09:18:57 -04:00
										 |  |  |     -rot drop roll length check-length ; inline
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : check-copy ( src n dst -- )
 | 
					
						
							|  |  |  |     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 )
 | 
					
						
							| 
									
										
										
										
											2008-04-26 00:12:44 -04:00
										 |  |  |     [ check-slice prepare-subseq (copy) ] [ like ] bi ;
 | 
					
						
							| 
									
										
										
										
											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 | 
					
						
							| 
									
										
										
										
											2008-11-23 03:44:56 -05:00
										 |  |  |     pick length check-length [ 3dup check-copy spin 0 ] dip
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  |     (copy) drop ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: sequence clone-like | 
					
						
							| 
									
										
										
										
											2008-11-23 03:44:56 -05:00
										 |  |  |     [ dup length ] dip new-sequence [ 0 swap copy ] keep ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | M: immutable-sequence clone-like like ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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 )
 | 
					
						
							|  |  |  |     [ pick length pick length pick length + + ] dip [ | 
					
						
							|  |  |  |         [ [ pick length pick length + ] dip copy ] | 
					
						
							|  |  |  |         [ (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
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : (each) ( seq quot -- n quot' )
 | 
					
						
							| 
									
										
										
										
											2008-12-15 22:20:32 -05:00
										 |  |  |     [ [ length ] keep [ nth-unsafe ] 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 )
 | 
					
						
							| 
									
										
										
										
											2008-12-10 18:26:54 -05:00
										 |  |  |     [ over ] dip [ nth-unsafe ] 2bi@ ; inline
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : (2each) ( seq1 seq2 quot -- n quot' )
 | 
					
						
							| 
									
										
										
										
											2008-11-23 03:44:56 -05:00
										 |  |  |     [ [ min-length ] 2keep ] dip
 | 
					
						
							|  |  |  |     [ [ 2nth-unsafe ] dip call ] 3curry ; inline
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-22 05:44:33 -04:00
										 |  |  | : 2map-into ( seq1 seq2 quot into -- newseq )
 | 
					
						
							| 
									
										
										
										
											2008-11-23 03:44:56 -05:00
										 |  |  |     [ (2each) ] dip collect ; inline
 | 
					
						
							| 
									
										
										
										
											2008-07-22 05:44:33 -04: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
										 |  |  | 
 | 
					
						
							|  |  |  | : (monotonic) ( seq quot -- ? )
 | 
					
						
							|  |  |  |     [ 2dup nth-unsafe rot 1+ rot nth-unsafe ] | 
					
						
							| 
									
										
										
										
											2008-04-26 00:12:44 -04:00
										 |  |  |     prepose curry ; inline
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : (interleave) ( n elt between quot -- )
 | 
					
						
							| 
									
										
										
										
											2008-10-02 07:47:20 -04:00
										 |  |  |     roll 0 = [ nip ] [ swapd 2slip ] if call ; 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-as ( seq quot exemplar -- newseq )
 | 
					
						
							| 
									
										
										
										
											2008-11-23 03:44:56 -05:00
										 |  |  |     [ over length ] dip [ [ map-into ] keep ] new-like ; 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 )
 | 
					
						
							|  |  |  |     [ drop ] prepose map ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : 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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | : change-each ( seq quot -- )
 | 
					
						
							|  |  |  |     over map-into ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : accumulate ( seq identity quot -- final newseq )
 | 
					
						
							|  |  |  |     swapd [ pick slip ] curry map ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : 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 )
 | 
					
						
							| 
									
										
										
										
											2008-11-23 03:44:56 -05:00
										 |  |  |     [ 2over min-length ] dip
 | 
					
						
							| 
									
										
										
										
											2008-07-22 05:44:33 -04:00
										 |  |  |     [ [ 2map-into ] keep ] new-like ; 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
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-09-17 19:37:57 -04:00
										 |  |  | : 2change-each ( seq1 seq2 quot -- )
 | 
					
						
							| 
									
										
										
										
											2008-07-22 05:44:33 -04:00
										 |  |  |     pick 2map-into ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | : 2all? ( seq1 seq2 quot -- ? )
 | 
					
						
							|  |  |  |     (2each) all-integers? ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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 )
 | 
					
						
							| 
									
										
										
										
											2008-11-23 03:44:56 -05: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
										 |  |  | 
 | 
					
						
							|  |  |  | : pusher ( quot -- quot accum )
 | 
					
						
							|  |  |  |     V{ } clone [ [ push-if ] 2curry ] keep ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-04-26 00:12:44 -04:00
										 |  |  | : filter ( seq quot -- subseq )
 | 
					
						
							| 
									
										
										
										
											2008-11-23 03:44:56 -05:00
										 |  |  |     over [ pusher [ each ] dip ] dip like ; 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 )
 | 
					
						
							| 
									
										
										
										
											2008-11-23 03:44:56 -05:00
										 |  |  |     over [ 2pusher [ each ] 2dip ] dip tuck [ like ] 2bi@ ; inline
 | 
					
						
							| 
									
										
										
										
											2008-09-05 20:32:19 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | : monotonic? ( seq quot -- ? )
 | 
					
						
							| 
									
										
										
										
											2008-12-15 22:20:32 -05:00
										 |  |  |     [ [ length 1- ] keep ] dip (monotonic) all? ; inline
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : interleave ( seq between quot -- )
 | 
					
						
							| 
									
										
										
										
											2008-12-15 22:20:32 -05:00
										 |  |  |     [ (interleave) ] 2curry [ [ length ] keep ] dip 2each ; inline
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-06-15 01:32:48 -04:00
										 |  |  | : accumulator ( quot -- quot' vec )
 | 
					
						
							|  |  |  |     V{ } clone [ [ push ] curry compose ] keep ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-09-27 18:54:44 -04:00
										 |  |  | : produce-as ( pred quot tail exemplar -- seq )
 | 
					
						
							| 
									
										
										
										
											2008-11-23 03:44:56 -05:00
										 |  |  |     [ swap accumulator [ swap while ] dip ] dip like ; inline
 | 
					
						
							| 
									
										
										
										
											2008-09-27 18:54:44 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-10 02:00:27 -04:00
										 |  |  | : produce ( pred quot tail -- 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 )
 | 
					
						
							| 
									
										
										
										
											2008-11-23 03:44:56 -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
										 |  |  | : prepare-index ( seq quot -- seq n quot )
 | 
					
						
							| 
									
										
										
										
											2008-11-23 03:44:56 -05:00
										 |  |  |     [ dup length ] dip ; inline
 | 
					
						
							| 
									
										
										
										
											2008-07-07 20:36:33 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : each-index ( seq quot -- )
 | 
					
						
							|  |  |  |     prepare-index 2each ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : map-index ( seq quot -- )
 | 
					
						
							|  |  |  |     prepare-index 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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-09-12 17:03:47 -04:00
										 |  |  | : indices ( obj seq -- indices )
 | 
					
						
							|  |  |  |     V{ } clone spin | 
					
						
							|  |  |  |     [ rot = [ over push ] [ drop ] if ] | 
					
						
							|  |  |  |     curry each-index ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-09-12 19:08:19 -04:00
										 |  |  | : nths ( indices seq -- seq' )
 | 
					
						
							|  |  |  |     [ nth ] curry map ;
 | 
					
						
							| 
									
										
										
										
											2008-08-13 19:56:41 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | : contains? ( seq quot -- ? )
 | 
					
						
							|  |  |  |     find drop >boolean ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-10-19 04:34:58 -04:00
										 |  |  | : member? ( elt seq -- ? )
 | 
					
						
							| 
									
										
										
										
											2008-01-09 17:36:30 -05:00
										 |  |  |     [ = ] with contains? ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-10-19 04:34:58 -04:00
										 |  |  | : memq? ( elt seq -- ? )
 | 
					
						
							| 
									
										
										
										
											2008-01-09 17:36:30 -05:00
										 |  |  |     [ eq? ] with contains? ;
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-10-19 04:34:58 -04:00
										 |  |  | : remq ( elt seq -- newseq )
 | 
					
						
							|  |  |  |     [ 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 ] 2keep
 | 
					
						
							|  |  |  |     [ 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
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-10-19 04:34:58 -04:00
										 |  |  | : (filter-here) ( 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
 | 
					
						
							|  |  |  |         (filter-here) | 
					
						
							|  |  |  |     ] [ nip set-length drop ] if ; inline recursive
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-09-17 19:37:57 -04:00
										 |  |  | PRIVATE>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-10-19 04:34:58 -04:00
										 |  |  | : filter-here ( seq quot -- )
 | 
					
						
							|  |  |  |     0 0 roll (filter-here) ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : delete ( elt seq -- )
 | 
					
						
							|  |  |  |     [ = not ] with filter-here ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : delq ( elt seq -- )
 | 
					
						
							|  |  |  |     [ eq? not ] with filter-here ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-03-31 17:28:21 -04:00
										 |  |  | : prefix ( seq elt -- newseq )
 | 
					
						
							| 
									
										
										
										
											2008-11-23 03:44:56 -05: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 )
 | 
					
						
							| 
									
										
										
										
											2008-11-23 03:44:56 -05:00
										 |  |  |     over [ over length 1+ ] dip [ | 
					
						
							|  |  |  |         [ [ 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 ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-04-26 00:12:44 -04:00
										 |  |  | : peek ( seq -- elt ) [ length 1- ] [ nth ] bi ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-12 02:08:30 -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
 | 
					
						
							|  |  |  |     ] [ | 
					
						
							| 
									
										
										
										
											2008-11-23 03:44:56 -05: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
 | 
					
						
							|  |  |  |     ] [ | 
					
						
							| 
									
										
										
										
											2008-11-23 03:44:56 -05: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 ? -- )
 | 
					
						
							|  |  |  |     [ | 
					
						
							| 
									
										
										
										
											2008-11-23 03:44:56 -05: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 ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-09-17 19:37:57 -04:00
										 |  |  | PRIVATE>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | : 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 ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : 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
										 |  |  | 
 | 
					
						
							|  |  |  | : delete-nth ( n seq -- )
 | 
					
						
							| 
									
										
										
										
											2008-11-23 03:44:56 -05:00
										 |  |  |     [ dup 1+ ] dip delete-slice ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : replace-slice ( new from to seq -- )
 | 
					
						
							| 
									
										
										
										
											2008-11-23 03:44:56 -05:00
										 |  |  |     [ [ [ dup pick length + ] dip - over ] dip open-slice ] keep
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  |     copy ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-09-05 20:29:14 -04:00
										 |  |  | : remove-nth ( n seq -- seq' )
 | 
					
						
							|  |  |  |     [ swap head-slice ] [ swap 1+ tail-slice ] 2bi append ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | : pop ( seq -- elt )
 | 
					
						
							| 
									
										
										
										
											2008-07-12 02:08:30 -04:00
										 |  |  |     [ length 1- ] [ [ nth ] [ shorten ] 2bi ] bi ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : all-equal? ( seq -- ? ) [ = ] monotonic? ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : all-eq? ( seq -- ? ) [ eq? ] monotonic? ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : exchange ( m n seq -- )
 | 
					
						
							|  |  |  |     pick over bounds-check 2drop 2dup bounds-check 2drop
 | 
					
						
							|  |  |  |     exchange-unsafe ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : reverse-here ( seq -- )
 | 
					
						
							|  |  |  |     dup length dup 2/ [ | 
					
						
							| 
									
										
										
										
											2008-11-23 03:44:56 -05:00
										 |  |  |         [ 2dup ] dip
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  |         tuck - 1- rot exchange-unsafe | 
					
						
							|  |  |  |     ] each 2drop ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-08-18 21:13:24 -04:00
										 |  |  | : reverse ( seq -- newseq )
 | 
					
						
							|  |  |  |     [ | 
					
						
							|  |  |  |         dup [ length ] keep new-sequence
 | 
					
						
							|  |  |  |         [ 0 swap copy ] keep
 | 
					
						
							|  |  |  |         [ reverse-here ] keep
 | 
					
						
							|  |  |  |     ] keep like ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | : sum-lengths ( seq -- n )
 | 
					
						
							|  |  |  |     0 [ length + ] reduce ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : concat ( seq -- newseq )
 | 
					
						
							| 
									
										
										
										
											2008-09-06 18:10:32 -04:00
										 |  |  |     [ | 
					
						
							|  |  |  |         { } | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  |     ] [ | 
					
						
							|  |  |  |         [ sum-lengths ] keep
 | 
					
						
							|  |  |  |         [ first new-resizable ] keep
 | 
					
						
							|  |  |  |         [ [ over push-all ] each ] keep
 | 
					
						
							|  |  |  |         first like
 | 
					
						
							| 
									
										
										
										
											2008-09-06 18:10:32 -04:00
										 |  |  |     ] 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 )
 | 
					
						
							|  |  |  |     [ | 
					
						
							| 
									
										
										
										
											2007-12-29 11:36:20 -05:00
										 |  |  |         2dup joined-length over new-resizable spin | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  |         [ dup pick push-all ] [ pick push-all ] interleave drop
 | 
					
						
							|  |  |  |     ] keep like ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : 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-left ( 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
										 |  |  | 
 | 
					
						
							|  |  |  | : pad-right ( seq n elt -- padded )
 | 
					
						
							|  |  |  |     [ 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
 | 
					
						
							|  |  |  |     ] [ | 
					
						
							|  |  |  |         tuck length head-slice sequence=
 | 
					
						
							|  |  |  |     ] if ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : tail? ( seq end -- ? )
 | 
					
						
							|  |  |  |     2dup shorter? [ | 
					
						
							|  |  |  |         2drop f
 | 
					
						
							|  |  |  |     ] [ | 
					
						
							|  |  |  |         tuck length tail-slice* sequence=
 | 
					
						
							|  |  |  |     ] 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 ? )
 | 
					
						
							|  |  |  |     pick length [ | 
					
						
							| 
									
										
										
										
											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 )
 | 
					
						
							|  |  |  |     pick length pick length swap - 1+ | 
					
						
							| 
									
										
										
										
											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*
 | 
					
						
							| 
									
										
										
										
											2008-11-23 03:44:56 -05:00
										 |  |  |     tuck [ tail-slice ] 2bi@ ;
 | 
					
						
							| 
									
										
										
										
											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 )
 | 
					
						
							| 
									
										
										
										
											2008-05-07 02:38:34 -04:00
										 |  |  |     [ but-last ] [ peek ] 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
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-09-17 19:37:57 -04:00
										 |  |  | : unclip-last-slice ( seq -- butlast-slice last )
 | 
					
						
							| 
									
										
										
										
											2008-09-10 00:38:40 -04:00
										 |  |  |     [ but-last-slice ] [ peek ] 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 0 over length rot <slice> ;
 | 
					
						
							|  |  |  |     inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-09-05 18:40:57 -04:00
										 |  |  | : trim-left-slice ( seq quot -- slice )
 | 
					
						
							| 
									
										
										
										
											2008-11-23 03:44:56 -05:00
										 |  |  |     over [ [ not ] compose find drop ] dip swap
 | 
					
						
							| 
									
										
										
										
											2008-09-05 18:40:57 -04:00
										 |  |  |     [ tail-slice ] [ dup length tail-slice ] if* ; inline
 | 
					
						
							|  |  |  |      | 
					
						
							|  |  |  | : trim-left ( seq quot -- newseq )
 | 
					
						
							|  |  |  |     over [ trim-left-slice ] dip like ; inline
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-09-05 18:40:57 -04:00
										 |  |  | : trim-right-slice ( seq quot -- slice )
 | 
					
						
							| 
									
										
										
										
											2008-11-23 03:44:56 -05:00
										 |  |  |     over [ [ not ] compose find-last drop ] dip swap
 | 
					
						
							| 
									
										
										
										
											2008-09-05 18:40:57 -04:00
										 |  |  |     [ 1+ head-slice ] [ 0 head-slice ] if* ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : trim-right ( seq quot -- newseq )
 | 
					
						
							|  |  |  |     over [ trim-right-slice ] dip like ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : trim-slice ( seq quot -- slice )
 | 
					
						
							| 
									
										
										
										
											2008-09-05 21:10:47 -04:00
										 |  |  |     [ trim-left-slice ] [ trim-right-slice ] bi ; inline
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : trim ( seq quot -- newseq )
 | 
					
						
							| 
									
										
										
										
											2008-09-05 18:40:57 -04:00
										 |  |  |     over [ trim-slice ] dip 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
										 |  |  | 
 | 
					
						
							|  |  |  | : infimum ( seq -- n ) dup first [ min ] reduce ;
 | 
					
						
							| 
									
										
										
										
											2008-08-18 21:13:24 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-10-14 20:38:23 -04:00
										 |  |  | : supremum ( seq -- n ) dup first [ max ] reduce ;
 | 
					
						
							| 
									
										
										
										
											2007-11-21 03:13:23 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-03 13:24:16 -04:00
										 |  |  | : sigma ( seq quot -- n ) [ + ] compose 0 swap reduce ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : count ( seq quot -- n ) [ 1 0 ? ] compose sigma ; 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 )
 | 
					
						
							|  |  |  |     [ dup first length [ length min ] reduce ] 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
 | 
					
						
							| 
									
										
										
										
											2008-12-06 12:17:19 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | : array-flip ( matrix -- newmatrix )
 | 
					
						
							| 
									
										
										
										
											2008-12-09 22:50:31 -05:00
										 |  |  |     { array } declare | 
					
						
							| 
									
										
										
										
											2008-12-06 12:17:19 -05:00
										 |  |  |     [ dup first array-length [ array-length min ] reduce ] keep
 | 
					
						
							|  |  |  |     [ [ 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 ;
 |