2009-02-08 00:04:55 -05:00
|
|
|
! Copyright (C) 2007, 2009 Chris Double, Doug Coleman, Eduardo
|
2008-11-25 06:55:49 -05:00
|
|
|
! Cavazos, Slava Pestov.
|
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
2009-10-14 17:50:30 -04:00
|
|
|
USING: kernel kernel.private sequences sequences.private math
|
|
|
|
combinators macros math.order math.ranges quotations fry effects
|
|
|
|
memoize.private ;
|
2008-11-25 06:55:49 -05:00
|
|
|
IN: generalizations
|
|
|
|
|
2008-11-25 17:47:47 -05:00
|
|
|
<<
|
|
|
|
|
2009-10-08 13:55:52 -04:00
|
|
|
ALIAS: n*quot (n*quot)
|
2008-11-25 17:47:47 -05:00
|
|
|
|
|
|
|
: repeat ( n obj quot -- ) swapd times ; inline
|
|
|
|
|
|
|
|
>>
|
|
|
|
|
2008-11-27 22:53:53 -05:00
|
|
|
MACRO: nsequence ( n seq -- )
|
2009-10-08 13:55:52 -04:00
|
|
|
[ [nsequence] ] keep
|
2008-11-25 06:55:49 -05:00
|
|
|
'[ @ _ like ] ;
|
|
|
|
|
2008-11-27 22:53:53 -05:00
|
|
|
MACRO: narray ( n -- )
|
2008-11-25 06:55:49 -05:00
|
|
|
'[ _ { } nsequence ] ;
|
|
|
|
|
2009-02-08 00:04:55 -05:00
|
|
|
MACRO: nsum ( n -- )
|
2009-08-13 20:21:44 -04:00
|
|
|
1 - [ + ] n*quot ;
|
2009-02-08 00:04:55 -05:00
|
|
|
|
2009-04-26 22:22:06 -04:00
|
|
|
MACRO: firstn-unsafe ( n -- )
|
2009-10-08 13:55:52 -04:00
|
|
|
[firstn] ;
|
2009-04-26 22:22:06 -04:00
|
|
|
|
2008-11-25 06:55:49 -05:00
|
|
|
MACRO: firstn ( n -- )
|
|
|
|
dup zero? [ drop [ drop ] ] [
|
2009-08-13 20:21:44 -04:00
|
|
|
[ 1 - swap bounds-check 2drop ]
|
2009-04-26 22:22:06 -04:00
|
|
|
[ firstn-unsafe ]
|
|
|
|
bi-curry '[ _ _ bi ]
|
2008-11-25 06:55:49 -05:00
|
|
|
] if ;
|
|
|
|
|
2008-11-27 22:53:53 -05:00
|
|
|
MACRO: npick ( n -- )
|
2009-08-13 20:21:44 -04:00
|
|
|
1 - [ dup ] [ '[ _ dip swap ] ] repeat ;
|
2008-11-25 06:55:49 -05:00
|
|
|
|
2009-06-11 12:03:18 -04:00
|
|
|
MACRO: nover ( n -- )
|
2009-06-19 13:58:17 -04:00
|
|
|
dup 1 + '[ _ npick ] n*quot ;
|
2009-06-11 12:03:18 -04:00
|
|
|
|
2008-11-25 06:55:49 -05:00
|
|
|
MACRO: ndup ( n -- )
|
|
|
|
dup '[ _ npick ] n*quot ;
|
|
|
|
|
2009-10-14 17:50:30 -04:00
|
|
|
MACRO: dupn ( n -- )
|
|
|
|
[ [ drop ] ]
|
|
|
|
[ 1 - [ dup ] n*quot ] if-zero ;
|
|
|
|
|
2008-11-25 06:55:49 -05:00
|
|
|
MACRO: nrot ( n -- )
|
2009-08-13 20:21:44 -04:00
|
|
|
1 - [ ] [ '[ _ dip swap ] ] repeat ;
|
2008-11-25 06:55:49 -05:00
|
|
|
|
|
|
|
MACRO: -nrot ( n -- )
|
2009-08-13 20:21:44 -04:00
|
|
|
1 - [ ] [ '[ swap _ dip ] ] repeat ;
|
2008-11-25 06:55:49 -05:00
|
|
|
|
2009-10-08 15:42:59 -04:00
|
|
|
MACRO: set-firstn-unsafe ( n -- )
|
|
|
|
[ 1 + ]
|
|
|
|
[ iota [ '[ _ rot [ set-nth-unsafe ] keep ] ] map ] bi
|
|
|
|
'[ _ -nrot _ spread drop ] ;
|
|
|
|
|
|
|
|
MACRO: set-firstn ( n -- )
|
|
|
|
dup zero? [ drop [ drop ] ] [
|
|
|
|
[ 1 - swap bounds-check 2drop ]
|
|
|
|
[ set-firstn-unsafe ]
|
|
|
|
bi-curry '[ _ _ bi ]
|
|
|
|
] if ;
|
|
|
|
|
2008-11-25 06:55:49 -05:00
|
|
|
MACRO: ndrop ( n -- )
|
|
|
|
[ drop ] n*quot ;
|
|
|
|
|
2008-11-25 17:47:47 -05:00
|
|
|
MACRO: nnip ( n -- )
|
|
|
|
'[ [ _ ndrop ] dip ] ;
|
2008-11-25 06:55:49 -05:00
|
|
|
|
|
|
|
MACRO: ntuck ( n -- )
|
2008-11-25 17:47:47 -05:00
|
|
|
2 + '[ dup _ -nrot ] ;
|
2008-11-25 06:55:49 -05:00
|
|
|
|
2009-10-14 17:50:30 -04:00
|
|
|
MACRO: ndip ( n -- )
|
|
|
|
[ [ dip ] curry ] n*quot [ call ] compose ;
|
2008-11-25 06:55:49 -05:00
|
|
|
|
2008-11-25 17:47:47 -05:00
|
|
|
MACRO: nkeep ( quot n -- )
|
|
|
|
tuck '[ _ ndup _ _ ndip ] ;
|
2008-11-25 06:55:49 -05:00
|
|
|
|
|
|
|
MACRO: ncurry ( n -- )
|
|
|
|
[ curry ] n*quot ;
|
|
|
|
|
|
|
|
MACRO: nwith ( n -- )
|
|
|
|
[ with ] n*quot ;
|
|
|
|
|
2009-06-11 12:03:18 -04:00
|
|
|
MACRO: nbi ( n -- )
|
|
|
|
'[ [ _ nkeep ] dip call ] ;
|
|
|
|
|
2008-12-09 05:20:20 -05:00
|
|
|
MACRO: ncleave ( quots n -- )
|
|
|
|
[ '[ _ '[ _ _ nkeep ] ] map [ ] join ] [ '[ _ ndrop ] ] bi
|
|
|
|
compose ;
|
|
|
|
|
2009-02-08 00:04:55 -05:00
|
|
|
MACRO: nspread ( quots n -- )
|
|
|
|
over empty? [ 2drop [ ] ] [
|
|
|
|
[ [ but-last ] dip ]
|
2009-05-25 17:38:33 -04:00
|
|
|
[ [ last ] dip ] 2bi
|
2009-02-08 00:04:55 -05:00
|
|
|
swap
|
|
|
|
'[ [ _ _ nspread ] _ ndip @ ]
|
|
|
|
] if ;
|
|
|
|
|
2009-10-14 17:50:30 -04:00
|
|
|
MACRO: spread* ( n -- )
|
|
|
|
[ [ ] ] [
|
|
|
|
1 swap [a,b) [ '[ [ [ _ ndip ] curry ] dip compose ] ] map [ ] concat-as
|
|
|
|
[ call ] compose
|
|
|
|
] if-zero ;
|
|
|
|
|
|
|
|
MACRO: cleave* ( n -- )
|
|
|
|
[ [ ] ]
|
|
|
|
[ 1 - [ [ [ keep ] curry ] dip compose ] n*quot [ call ] compose ]
|
|
|
|
if-zero ;
|
|
|
|
|
2009-09-30 00:28:43 -04:00
|
|
|
MACRO: napply ( n -- )
|
|
|
|
[ [ drop ] ] dip [ '[ tuck _ 2dip call ] ] times ;
|
2008-11-27 22:07:50 -05:00
|
|
|
|
2009-10-14 17:50:30 -04:00
|
|
|
: apply-curry ( ...a quot n -- )
|
|
|
|
[ [curry] ] dip napply ; inline
|
|
|
|
|
|
|
|
: cleave-curry ( a ...quot n -- )
|
|
|
|
[ [curry] ] swap [ napply ] [ cleave* ] bi ; inline
|
|
|
|
|
|
|
|
: spread-curry ( ...a ...quot n -- )
|
|
|
|
[ [curry] ] swap [ napply ] [ spread* ] bi ; inline
|
|
|
|
|
2008-11-27 22:07:50 -05:00
|
|
|
MACRO: mnswap ( m n -- )
|
2009-08-13 20:21:44 -04:00
|
|
|
1 + '[ _ -nrot ] swap '[ _ _ napply ] ;
|
2009-02-08 00:04:55 -05:00
|
|
|
|
|
|
|
MACRO: nweave ( n -- )
|
2009-08-19 10:53:13 -04:00
|
|
|
[ dup iota <reversed> [ '[ _ _ mnswap ] ] with map ] keep
|
2009-02-08 00:04:55 -05:00
|
|
|
'[ _ _ ncleave ] ;
|
2009-01-18 21:26:58 -05:00
|
|
|
|
2009-06-11 12:03:18 -04:00
|
|
|
MACRO: nbi-curry ( n -- )
|
|
|
|
[ bi-curry ] n*quot ;
|
|
|
|
|
2009-01-18 21:26:58 -05:00
|
|
|
: nappend-as ( n exemplar -- seq )
|
|
|
|
[ narray concat ] dip like ; inline
|
|
|
|
|
2009-04-17 19:04:41 -04:00
|
|
|
: nappend ( n -- seq ) narray concat ; inline
|
2009-10-08 12:34:20 -04:00
|
|
|
|
|
|
|
MACRO: nspin ( n -- )
|
|
|
|
[ [ ] ] swap [ swap [ ] curry compose ] n*quot [ call ] 3append ;
|
2009-10-14 01:09:57 -04:00
|
|
|
|
|
|
|
MACRO: nmin-length ( n -- )
|
|
|
|
dup 1 - [ min ] n*quot
|
|
|
|
'[ [ length ] _ napply @ ] ;
|
|
|
|
|
2009-10-14 17:50:30 -04:00
|
|
|
: nnth-unsafe ( n ...seq n -- )
|
|
|
|
[ nth-unsafe ] swap [ apply-curry ] [ cleave* ] bi ; inline
|
|
|
|
MACRO: nset-nth-unsafe ( n -- )
|
|
|
|
[ [ drop ] ]
|
|
|
|
[ '[ [ set-nth-unsafe ] _ [ apply-curry ] [ cleave-curry ] [ spread* ] tri ] ]
|
|
|
|
if-zero ;
|
2009-10-14 01:09:57 -04:00
|
|
|
|
2009-10-14 17:50:30 -04:00
|
|
|
: (neach) ( ...seq quot n -- len quot' )
|
2009-10-14 01:09:57 -04:00
|
|
|
dup dup dup
|
2009-10-14 17:50:30 -04:00
|
|
|
'[ [ _ nmin-length ] _ nkeep [ _ nnth-unsafe ] _ ncurry ] dip compose ; inline
|
2009-10-14 01:09:57 -04:00
|
|
|
|
2009-10-14 01:38:51 -04:00
|
|
|
: neach ( ...seq quot n -- )
|
2009-10-14 01:09:57 -04:00
|
|
|
(neach) each-integer ; inline
|
2009-10-14 01:38:51 -04:00
|
|
|
|
|
|
|
: nmap-as ( ...seq quot exemplar n -- result )
|
|
|
|
'[ _ (neach) ] dip map-integers ; inline
|
|
|
|
|
|
|
|
: nmap ( ...seq quot n -- result )
|
|
|
|
dup '[ [ _ npick ] dip swap ] dip nmap-as ; inline
|
2009-10-14 17:50:30 -04:00
|
|
|
|
|
|
|
MACRO: nnew-sequence ( n -- )
|
|
|
|
[ [ drop ] ]
|
|
|
|
[ dup '[ [ new-sequence ] _ apply-curry _ cleave* ] ] if-zero ;
|
|
|
|
|
|
|
|
: nnew-like ( len ...exemplar quot n -- result... )
|
|
|
|
dup dup dup dup '[
|
|
|
|
_ nover
|
|
|
|
[ [ _ nnew-sequence ] dip call ]
|
|
|
|
_ ndip [ like ]
|
|
|
|
_ apply-curry
|
|
|
|
_ spread*
|
|
|
|
] call ; inline
|
|
|
|
|
|
|
|
MACRO: (ncollect) ( n -- )
|
|
|
|
dup dup 1 +
|
|
|
|
'[ [ [ keep ] _ ndip _ nset-nth-unsafe ] _ ncurry ] ;
|
|
|
|
|
|
|
|
: ncollect ( len quot ...into n -- )
|
|
|
|
(ncollect) each-integer ; inline
|
|
|
|
|
|
|
|
: nmap-integers ( len quot ...exemplar n -- result... )
|
|
|
|
dup dup dup
|
|
|
|
'[ [ over ] _ ndip [ [ _ ncollect ] _ nkeep ] _ nnew-like ] call ; inline
|
|
|
|
|
|
|
|
: mnmap-as ( m*seq quot n*exemplar m n -- result*n )
|
|
|
|
dup '[ [ _ (neach) ] _ ndip _ nmap-integers ] call ; inline
|
|
|
|
|
|
|
|
: mnmap ( m*seq quot m n -- result*n )
|
|
|
|
2dup '[ [ _ npick ] dip swap _ dupn ] 2dip mnmap-as ; inline
|
|
|
|
|