factor/library/collections/sequence-combinators.factor

179 lines
4.5 KiB
Factor
Raw Normal View History

2006-05-17 14:55:46 -04:00
! Copyright (C) 2005, 2006 Slava Pestov.
2005-12-31 20:51:58 -05:00
! See http://factorcode.org/license.txt for BSD license.
IN: sequences-internals
2006-01-06 02:04:42 -05:00
USING: arrays generic kernel kernel-internals math sequences
vectors ;
2005-09-07 17:21:11 -04:00
2006-08-15 16:29:35 -04:00
: collect ( n quot -- array )
2005-12-28 20:25:17 -05:00
>r [ f <array> ] keep r> swap [
[ rot >r [ swap call ] keep r> set-array-nth ] 3keep
] repeat drop ; inline
2006-07-28 03:54:46 -04:00
: (map) ( seq quot i -- quot seq value )
-rot [ >r nth-unsafe r> call ] 2keep rot ; inline
: (2each) ( quot seq seq i -- quot seq seq i )
[ 2nth-unsafe rot dup slip ] 3keep ; inline
: (2map) ( quot seq seq i -- quot seq seq value )
pick pick >r >r 2nth-unsafe rot dup slip
swap r> swap r> swap ; inline
: (monotonic) ( quot seq i -- ? )
2005-09-16 22:47:28 -04:00
2dup 1+ swap nth-unsafe >r swap nth-unsafe r> rot call ;
inline
2005-12-28 20:25:17 -05:00
: (interleave) ( n -- array )
2006-01-28 15:49:31 -05:00
dup zero? [
2005-12-28 20:25:17 -05:00
drop { }
] [
t <array> f 0 pick set-nth-unsafe
] if ;
IN: sequences
2006-08-15 16:29:35 -04:00
: each ( seq quot -- )
2005-09-07 17:21:11 -04:00
swap dup length [
[ swap nth-unsafe swap call ] 3keep
2006-05-17 14:55:46 -04:00
] repeat 2drop ; inline
2005-09-07 17:21:11 -04:00
2006-08-15 16:29:35 -04:00
: each-with ( obj seq quot -- )
2005-09-07 17:21:11 -04:00
swap [ with ] each 2drop ; inline
2006-08-15 21:23:05 -04:00
: reduce ( seq identity quot -- result )
2005-09-07 17:21:11 -04:00
swapd each ; inline
2006-08-15 21:23:05 -04:00
: map ( seq quot -- newseq )
2006-07-28 03:54:46 -04:00
over >r over length [ (map) ] collect r> like 2nip ;
2006-05-17 14:55:46 -04:00
inline
2005-09-07 17:21:11 -04:00
2006-08-15 21:23:05 -04:00
: map-with ( obj list quot -- newseq )
2005-09-07 17:21:11 -04:00
swap [ with rot ] map 2nip ; inline
: accumulate ( seq identity quot -- final newseq )
rot [ pick >r swap call r> ] map-with ; inline
2005-09-07 17:21:11 -04:00
2006-07-28 03:54:46 -04:00
: change-nth ( i seq quot -- )
-rot [ nth swap call ] 2keep set-nth ; inline
2006-08-15 16:29:35 -04:00
: inject ( seq quot -- )
over length
2006-07-28 03:54:46 -04:00
[ [ -rot change-nth ] 3keep ] repeat 2drop ;
2005-09-07 17:21:11 -04:00
inline
2006-08-15 16:29:35 -04:00
: inject-with ( obj seq quot -- )
2006-07-14 01:00:59 -04:00
swap [ with rot ] inject 2drop ; inline
2006-08-15 21:23:05 -04:00
: min-length ( seq1 seq2 -- n )
[ length ] 2apply min ;
2006-08-15 21:23:05 -04:00
: max-length ( seq1 seq2 -- n )
[ length ] 2apply max ;
2005-10-23 18:33:40 -04:00
2006-08-15 21:23:05 -04:00
: 2each ( seq1 seq2 quot -- )
-rot 2dup min-length [ (2each) ] repeat 3drop ; inline
2005-09-07 17:21:11 -04:00
2006-08-15 21:23:05 -04:00
: 2reduce ( seq seq identity quot -- result )
2005-09-07 17:21:11 -04:00
>r -rot r> 2each ; inline
2006-08-15 21:23:05 -04:00
: 2map ( seq1 seq2 quot -- newseq )
-rot
[ 2dup min-length [ (2map) ] collect ] keep like
>r 3drop r> ; inline
2005-09-07 17:21:11 -04:00
2005-10-01 01:44:49 -04:00
: if-bounds ( i seq quot -- )
>r pick pick bounds-check? r> [ 3drop -1 f ] if ; inline
2006-08-15 21:23:05 -04:00
: find* ( n seq quot -- i elt )
2005-10-01 01:44:49 -04:00
[
3dup >r >r >r >r nth-unsafe r> call [
r> dup r> nth-unsafe r> drop
2005-09-07 17:21:11 -04:00
] [
2005-09-16 22:47:28 -04:00
r> 1+ r> r> find*
2005-09-24 15:21:17 -04:00
] if
2005-10-01 01:44:49 -04:00
] if-bounds ; inline
2005-09-07 17:21:11 -04:00
2006-08-15 21:23:05 -04:00
: find-with* ( obj n seq quot -- i elt )
2005-09-07 17:21:11 -04:00
-rot [ with rot ] find* 2swap 2drop ; inline
2006-08-15 16:29:35 -04:00
: find ( seq quot -- i elt )
2006-05-17 14:55:46 -04:00
0 -rot find* ; inline
2006-08-15 16:29:35 -04:00
: find-with ( obj seq quot -- i elt )
2006-05-17 14:55:46 -04:00
swap [ with rot ] find 2swap 2drop ; inline
2005-09-07 17:21:11 -04:00
2006-08-15 21:23:05 -04:00
: find-last* ( n seq quot -- i elt )
2005-10-01 01:44:49 -04:00
[
3dup >r >r >r >r nth-unsafe r> call [
r> dup r> nth-unsafe r> drop
] [
r> 1- r> r> find-last*
] if
] if-bounds ; inline
2006-08-15 21:23:05 -04:00
: find-last-with* ( obj n seq quot -- i elt )
2005-10-01 01:44:49 -04:00
-rot [ with rot ] find-last* 2swap 2drop ; inline
: find-last ( seq quot -- i elt )
>r [ length 1- ] keep r> find-last* ; inline
2006-08-15 16:29:35 -04:00
: find-last-with ( obj seq quot -- i elt )
swap [ with rot ] find-last 2swap 2drop ; inline
2005-09-07 17:21:11 -04:00
: contains? ( seq quot -- ? )
find drop -1 > ; inline
: contains-with? ( obj seq quot -- ? )
find-with drop -1 > ; inline
: all? ( seq quot -- ? )
swap [ swap call not ] contains-with? not ; inline
2006-08-15 16:29:35 -04:00
: all-with? ( obj seq quot -- ? )
2005-09-07 17:21:11 -04:00
swap [ with rot ] all? 2nip ; inline
2006-08-15 21:23:05 -04:00
: subset ( seq quot -- subseq )
2006-07-28 03:54:46 -04:00
over >r over length <vector> rot [
-rot [
>r over >r call [ r> r> push ] [ r> r> 2drop ] if
] 2keep
] each r> like nip ; inline
2005-09-07 17:21:11 -04:00
2006-08-15 21:23:05 -04:00
: subset-with ( obj seq quot -- subseq )
2005-09-07 17:21:11 -04:00
swap [ with rot ] subset 2nip ; inline
2006-08-15 16:29:35 -04:00
: monotonic? ( seq quot -- ? )
2005-09-16 22:47:28 -04:00
swap dup length 1- [
2005-09-07 17:21:11 -04:00
pick pick >r >r (monotonic) r> r> rot
] all? 2nip ; inline
2005-10-14 04:05:02 -04:00
2005-12-28 20:25:17 -05:00
: interleave ( seq quot between -- )
rot dup length (interleave) [
[ -rot [ -rot 2slip call ] 2keep ]
[ -rot [ drop call ] 2keep ]
if
] 2each 2drop ; inline
2006-08-15 16:29:35 -04:00
: cache-nth ( i seq quot -- elt )
2005-10-14 04:05:02 -04:00
pick pick ?nth dup [
>r 3drop r>
] [
drop swap >r over >r call dup r> r> set-nth
] if ; inline
2006-08-15 21:23:05 -04:00
: copy-into-check ( n dest src -- n dest src )
pick over length + pick 2dup length >
2006-08-06 22:30:52 -04:00
[ set-length ] [ 2drop ] if ; inline
2006-08-15 21:23:05 -04:00
: copy-into ( n dest src -- )
copy-into-check dup length
[ >r pick r> + pick set-nth-unsafe ] 2each 2drop ;
inline
2006-07-24 00:20:08 -04:00
: >sequence ( seq pred quot -- newseq )
pick rot call [
drop clone
] [
2006-07-28 03:54:46 -04:00
over >r >r length r> call 0 over r> copy-into
2006-07-24 00:20:08 -04:00
] if ; inline