factor/library/collections/sequences-epilogue.factor

150 lines
3.9 KiB
Factor
Raw Normal View History

2006-01-09 01:34:23 -05:00
! Copyright (C) 2005, 2006 Slava Pestov.
2005-12-31 20:51:58 -05:00
! See http://factorcode.org/license.txt for BSD license.
2005-04-02 02:39:33 -05:00
IN: sequences
USING: errors generic kernel kernel-internals math
2006-01-09 01:34:23 -05:00
sequences-internals strings vectors words ;
2005-04-02 02:39:33 -05:00
: first2 ( { x y } -- x y )
2006-05-02 06:05:58 -04:00
1 swap bounds-check nip first2-unsafe ; flushable
: first3 ( { x y z } -- x y z )
2006-05-02 06:05:58 -04:00
2 swap bounds-check nip first3-unsafe ; flushable
: first4 ( { x y z w } -- x y z w )
2006-05-02 06:05:58 -04:00
3 swap bounds-check nip first4-unsafe ; flushable
2005-07-16 22:16:18 -04:00
M: object like drop ;
2006-03-27 22:20:42 -05:00
: index ( obj seq -- n )
[ = ] find-with drop ; flushable
: index* ( obj i seq -- n )
[ = ] find-with* drop ; flushable
: last-index ( obj seq -- n )
[ = ] find-last-with drop ; flushable
: last-index* ( obj i seq -- n )
[ = ] find-last-with* drop ; flushable
: member? ( obj seq -- ? )
[ = ] contains-with? ; flushable
: memq? ( obj seq -- ? )
[ eq? ] contains-with? ; flushable
: remove ( obj list -- list )
[ = not ] subset-with ; flushable
2005-04-06 21:41:49 -04:00
2005-09-17 04:15:05 -04:00
: (subst) ( newseq oldseq elt -- new/elt )
[ swap index ] keep
2005-09-24 15:21:17 -04:00
over -1 > [ drop swap nth ] [ 2nip ] if ;
2005-09-17 04:15:05 -04:00
: subst ( newseq oldseq seq -- )
[ >r 2dup r> (subst) ] inject 2drop ;
2005-09-14 00:37:50 -04:00
: move ( to from seq -- )
pick pick number=
2005-09-24 15:21:17 -04:00
[ 3drop ] [ [ nth swap ] keep set-nth ] if ; inline
2005-09-14 00:37:50 -04:00
: (delete) ( elt store scan seq -- )
2dup length < [
3dup move
>r pick over r> dup >r nth = r> swap
2005-09-16 22:47:28 -04:00
[ >r >r 1+ r> r> ] unless >r 1+ r> (delete)
2005-09-14 00:37:50 -04:00
] when ;
: delete ( elt seq -- ) 0 0 rot (delete) nip set-length drop ;
2005-09-14 00:37:50 -04:00
2006-01-02 00:51:03 -05:00
: copy-into-check ( start to from -- start to from )
pick over length + pick 2dup length >
[ set-length ] [ 2drop ] if ;
2005-08-04 12:58:07 -04:00
: copy-into ( start to from -- )
2006-01-02 00:51:03 -05:00
copy-into-check dup length
[ >r pick r> + pick set-nth-unsafe ] 2each 2drop ;
inline
2005-08-04 12:58:07 -04:00
2006-01-02 00:51:03 -05:00
: >sequence ( seq quot -- )
over >r >r length r> call dup 0 swap r> copy-into ; inline
2005-08-04 12:58:07 -04:00
: nappend ( to from -- )
2006-01-02 00:51:03 -05:00
>r [ length ] keep r> copy-into ; inline
: >resizable ( seq -- seq ) [ thaw dup ] keep nappend ;
2005-04-06 21:41:49 -04:00
2005-12-31 20:51:58 -05:00
: immutable ( seq quot -- seq | quot: seq -- )
2006-01-02 00:51:03 -05:00
swap [ >resizable [ swap call ] keep ] keep like ; inline
2005-12-31 20:51:58 -05:00
: append ( s1 s2 -- s1+s2 )
swap [ swap nappend ] immutable ; flushable
: add ( seq elt -- seq )
swap [ push ] immutable ; flushable
2005-04-17 21:59:11 -04:00
2005-12-04 22:06:12 -05:00
: diff ( seq1 seq2 -- seq2-seq1 )
[ swap member? not ] subset-with ; flushable
: append3 ( s1 s2 s3 -- s1+s2+s3 )
rot [ [ rot nappend ] keep swap nappend ] immutable ; flushable
2005-04-17 21:59:11 -04:00
: peek ( sequence -- element ) dup length 1- swap nth ;
2005-04-06 21:41:49 -04:00
2005-09-14 00:37:50 -04:00
: pop* ( sequence -- )
[ length 1- ] keep
2005-11-19 04:09:30 -05:00
[ 0 -rot set-nth ] 2keep
2006-05-02 06:05:58 -04:00
set-length ;
2005-09-14 00:37:50 -04:00
2005-04-06 21:41:49 -04:00
: pop ( sequence -- element )
2006-05-02 06:05:58 -04:00
dup peek swap pop* ;
2005-04-06 21:41:49 -04:00
2005-07-17 00:21:10 -04:00
M: object reverse-slice ( seq -- seq ) <reversed> ;
2005-07-16 23:01:51 -04:00
2005-07-16 22:16:18 -04:00
M: object reverse ( seq -- seq ) [ <reversed> ] keep like ;
2005-04-11 23:05:05 -04:00
2005-09-18 01:37:28 -04:00
: all-equal? ( seq -- ? ) [ = ] monotonic? ;
: all-eq? ( seq -- ? ) [ eq? ] monotonic? ;
2005-10-09 21:27:14 -04:00
: mismatch ( seq1 seq2 -- i )
2dup min-length
[ >r 2dup r> 2nth-unsafe = not ] find
swap >r 3drop r> ; flushable
2005-07-30 02:08:59 -04:00
: flip ( seq -- seq )
2005-07-31 23:38:33 -04:00
dup empty? [
dup first [ length ] keep like
[ swap [ nth ] map-with ] map-with
] unless ; flushable
2006-03-14 16:51:09 -05:00
: exchange ( n n seq -- )
pick over bounds-check 2drop 2dup bounds-check 2drop
exchange-unsafe ;
: assoc ( key assoc -- value )
[ first = ] find-with nip second ;
2005-04-06 21:41:49 -04:00
IN: kernel
2006-01-09 01:34:23 -05:00
M: object <=>
2dup mismatch dup -1 =
[ drop [ length ] 2apply - ] [ 2nth-unsafe <=> ] if ;
2005-12-31 20:51:58 -05:00
: depth ( -- n ) datastack length ;
2005-08-08 15:21:14 -04:00
2005-09-17 22:25:18 -04:00
: no-cond "cond fall-through" throw ;
2005-08-08 15:21:14 -04:00
: cond ( conditions -- )
#! Conditions is a sequence of quotation pairs.
#! { { [ X ] [ Y ] } { [ Z ] [ T ] } }
2005-09-24 15:21:17 -04:00
#! => X [ Y ] [ Z [ T ] [ ] if ] if
2005-08-08 15:21:14 -04:00
#! The last condition should be a catch-all 't'.
2005-08-30 18:24:53 -04:00
[ first call ] find nip dup
2005-09-24 15:21:17 -04:00
[ second call ] [ no-cond ] if ;
2005-08-08 15:21:14 -04:00
: with-datastack ( stack word -- stack )
datastack >r >r set-datastack r> execute
datastack r> [ push ] keep set-datastack 2nip ;
2006-03-20 00:05:04 -05:00
: unix? os { "freebsd" "linux" "macosx" "solaris" } member? ;