factor/basis/generalizations/generalizations.factor

139 lines
3.0 KiB
Factor
Raw Normal View History

! Copyright (C) 2007, 2009 Chris Double, Doug Coleman, Eduardo
! Cavazos, Slava Pestov.
! See http://factorcode.org/license.txt for BSD license.
2009-02-02 17:35:30 -05:00
USING: kernel sequences sequences.private math combinators
2009-10-14 01:09:57 -04:00
macros math.order quotations fry effects memoize.private ;
IN: generalizations
<<
ALIAS: n*quot (n*quot)
: repeat ( n obj quot -- ) swapd times ; inline
>>
2008-11-27 22:53:53 -05:00
MACRO: nsequence ( n seq -- )
[ [nsequence] ] keep
'[ @ _ like ] ;
2008-11-27 22:53:53 -05:00
MACRO: narray ( n -- )
'[ _ { } nsequence ] ;
MACRO: nsum ( n -- )
1 - [ + ] n*quot ;
MACRO: firstn-unsafe ( n -- )
[firstn] ;
MACRO: firstn ( n -- )
dup zero? [ drop [ drop ] ] [
[ 1 - swap bounds-check 2drop ]
[ firstn-unsafe ]
bi-curry '[ _ _ bi ]
] if ;
2008-11-27 22:53:53 -05:00
MACRO: npick ( n -- )
1 - [ dup ] [ '[ _ dip swap ] ] repeat ;
MACRO: nover ( n -- )
2009-06-19 13:58:17 -04:00
dup 1 + '[ _ npick ] n*quot ;
MACRO: ndup ( n -- )
dup '[ _ npick ] n*quot ;
MACRO: nrot ( n -- )
1 - [ ] [ '[ _ dip swap ] ] repeat ;
MACRO: -nrot ( n -- )
1 - [ ] [ '[ swap _ dip ] ] repeat ;
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 ;
MACRO: ndrop ( n -- )
[ drop ] n*quot ;
MACRO: nnip ( n -- )
'[ [ _ ndrop ] dip ] ;
MACRO: ntuck ( n -- )
2 + '[ dup _ -nrot ] ;
MACRO: ndip ( quot n -- )
[ '[ _ dip ] ] times ;
MACRO: nkeep ( quot n -- )
tuck '[ _ ndup _ _ ndip ] ;
MACRO: ncurry ( n -- )
[ curry ] n*quot ;
MACRO: nwith ( n -- )
[ with ] n*quot ;
MACRO: nbi ( n -- )
'[ [ _ nkeep ] dip call ] ;
MACRO: ncleave ( quots n -- )
[ '[ _ '[ _ _ nkeep ] ] map [ ] join ] [ '[ _ ndrop ] ] bi
compose ;
MACRO: nspread ( quots n -- )
over empty? [ 2drop [ ] ] [
[ [ but-last ] dip ]
[ [ last ] dip ] 2bi
swap
'[ [ _ _ nspread ] _ ndip @ ]
] if ;
MACRO: napply ( n -- )
[ [ drop ] ] dip [ '[ tuck _ 2dip call ] ] times ;
2008-11-27 22:07:50 -05:00
MACRO: mnswap ( m n -- )
1 + '[ _ -nrot ] swap '[ _ _ napply ] ;
MACRO: nweave ( n -- )
2009-08-19 10:53:13 -04:00
[ dup iota <reversed> [ '[ _ _ mnswap ] ] with map ] keep
'[ _ _ ncleave ] ;
MACRO: nbi-curry ( n -- )
[ bi-curry ] n*quot ;
: nappend-as ( n exemplar -- seq )
[ narray concat ] dip like ; inline
: 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 @ ] ;
MACRO: nnth-unsafe ( n -- )
'[ [ '[ _ nth-unsafe ] keep ] _ napply drop ] ;
MACRO: (neach) ( n -- )
dup dup dup
'[ [ [ _ nmin-length ] _ nkeep [ _ nnth-unsafe ] _ ncurry ] dip compose ] ;
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