factor/unmaintained/sequences-lib/lib.factor

150 lines
4.5 KiB
Factor
Raw Normal View History

! Copyright (C) 2007 Slava Pestov, Chris Double, Doug Coleman,
! Eduardo Cavazos, Daniel Ehrenberg.
! See http://factorcode.org/license.txt for BSD license.
2008-09-10 23:11:40 -04:00
USING: combinators.lib kernel sequences math namespaces make
assocs random sequences.private shuffle math.functions arrays
math.parser math.private sorting strings ascii macros assocs.lib
2008-10-15 13:04:32 -04:00
quotations hashtables math.order locals generalizations
math.ranges random fry ;
2007-09-20 18:09:08 -04:00
IN: sequences.lib
: each-withn ( seq quot n -- ) nwith each ; inline
: each-with ( seq quot -- ) with each ; inline
: each-with2 ( obj obj list quot -- ) 2 each-withn ; inline
: map-withn ( seq quot n -- newseq ) nwith map ; inline
: map-with ( seq quot -- ) with map ; inline
: map-with2 ( obj obj list quot -- newseq ) 2 map-withn ; inline
! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2008-04-08 03:25:14 -04:00
: each-percent ( seq quot -- )
[
dup length
dup [ / ] curry
[ 1+ ] prepose
] dip compose
2008-04-08 03:25:14 -04:00
2each ; inline
! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2007-09-20 18:09:08 -04:00
: reduce* ( seq quot -- result ) [ ] swap map-reduce ; inline
! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2008-04-27 19:57:46 -04:00
: higher ( a b quot -- c ) [ compare +gt+ eq? ] curry most ; inline
2007-09-20 18:09:08 -04:00
2008-04-27 19:57:46 -04:00
: lower ( a b quot -- c ) [ compare +lt+ eq? ] curry most ; inline
2007-09-20 18:09:08 -04:00
! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
: longer ( a b -- c ) [ length ] higher ;
: shorter ( a b -- c ) [ length ] lower ;
! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
: longest ( seq -- item ) [ longer ] reduce* ;
: shortest ( seq -- item ) [ shorter ] reduce* ;
! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
: bigger ( a b -- c ) [ ] higher ;
: smaller ( a b -- c ) [ ] lower ;
! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
: biggest ( seq -- item ) [ bigger ] reduce* ;
: smallest ( seq -- item ) [ smaller ] reduce* ;
! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
: minmax ( seq -- min max )
#! find the min and max of a seq in one pass
1/0. -1/0. rot [ tuck max [ min ] dip ] each ;
2007-09-20 18:09:08 -04:00
! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2008-06-08 16:32:55 -04:00
: ,, ( obj -- ) building get peek push ;
: v, ( -- ) V{ } clone , ;
: ,v ( -- ) building get dup peek empty? [ dup pop* ] when drop ;
2008-06-27 20:40:46 -04:00
: (monotonic-split) ( seq quot -- newseq )
[
[ dup unclip suffix ] dip
v, [ pick ,, call [ v, ] unless ] curry 2each ,v
] { } make ;
2008-06-27 20:40:46 -04:00
: monotonic-split ( seq quot -- newseq )
over empty? [ 2drop { } ] [ (monotonic-split) ] if ;
ERROR: element-not-found ;
: split-around ( seq quot -- before elem after )
2008-06-27 20:40:46 -04:00
dupd find over [ element-not-found ] unless
[ cut rest ] dip swap ; inline
2007-11-30 21:23:27 -05:00
: map-until ( seq quot pred -- newseq )
'[ [ @ dup @ [ drop t ] [ , f ] if ] find 2drop ] { } make ;
2007-11-30 21:23:27 -05:00
: take-while ( seq quot -- newseq )
[ not ] compose
[ find drop [ head-slice ] when* ] curry
[ dup ] prepose keep like ;
! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
<PRIVATE
: translate-string ( n alphabet out-len -- seq )
2008-01-09 17:36:30 -05:00
[ drop /mod ] with map nip ;
: map-alphabet ( alphabet seq[seq] -- seq[seq] )
2008-01-09 17:36:30 -05:00
[ [ swap nth ] with map ] with map ;
: exact-number-strings ( n out-len -- seqs )
[ ^ ] 2keep [ translate-string ] 2curry map ;
: number-strings ( n max-length -- seqs )
2008-01-09 17:36:30 -05:00
1+ [ exact-number-strings ] with map concat ;
PRIVATE>
: exact-strings ( alphabet length -- seqs )
[ dup length ] dip exact-number-strings map-alphabet ;
: strings ( alphabet length -- seqs )
[ dup length ] dip number-strings map-alphabet ;
: switches ( seq1 seq -- subseq )
! seq1 is a sequence of ones and zeroes
[ [ length ] keep [ nth 1 = ] curry filter ] dip
[ nth ] curry { } map-as ;
: power-set ( seq -- subsets )
2 over length exact-number-strings swap [ switches ] curry map ;
2008-02-03 04:48:58 -05:00
! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
<PRIVATE
: (attempt-each-integer) ( i n quot -- result )
[
iterate-step roll
[ 3nip ] [ iterate-next (attempt-each-integer) ] if*
2008-08-27 18:02:54 -04:00
] [ 3drop f ] if-iterate? ; inline recursive
PRIVATE>
: attempt-each ( seq quot -- result )
(each) iterate-prep (attempt-each-integer) ; inline
! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
: randomize ( seq -- seq' )
2008-09-26 00:19:28 -04:00
dup length 1 (a,b] [ dup random pick exchange ] each ;
2008-10-15 13:04:32 -04:00
: enumerate ( seq -- seq' ) <enum> >alist ;