factor/basis/generalizations/generalizations.factor

66 lines
1.6 KiB
Factor
Raw Normal View History

! Copyright (C) 2007, 2008 Chris Double, Doug Coleman, Eduardo
! Cavazos, Slava Pestov.
! See http://factorcode.org/license.txt for BSD license.
2008-07-13 20:55:42 -04:00
USING: kernel sequences sequences.private namespaces math
math.ranges combinators macros quotations fry arrays ;
IN: generalizations
2008-09-07 00:20:27 -04:00
MACRO: nsequence ( n seq -- quot )
2008-09-10 23:11:40 -04:00
[ drop <reversed> ] [ '[ _ _ new-sequence ] ] 2bi
[ '[ @ [ _ swap set-nth-unsafe ] keep ] ] reduce ;
2008-09-07 00:20:27 -04:00
MACRO: narray ( n -- quot )
2008-09-10 23:11:40 -04:00
'[ _ { } nsequence ] ;
2008-09-07 00:20:27 -04:00
MACRO: firstn ( n -- )
dup zero? [ drop [ drop ] ] [
2008-09-10 23:11:40 -04:00
[ [ '[ [ _ ] dip nth-unsafe ] ] map ]
[ 1- '[ [ _ ] dip bounds-check 2drop ] ]
bi prefix '[ _ cleave ]
] if ;
MACRO: npick ( n -- )
1- dup saver [ dup ] rot [ r> swap ] n*quot 3append ;
MACRO: ndup ( n -- )
2008-09-10 23:11:40 -04:00
dup '[ _ npick ] n*quot ;
MACRO: nrot ( n -- )
1- dup saver swap [ r> swap ] n*quot append ;
MACRO: -nrot ( n -- )
1- dup [ swap >r ] n*quot swap restorer append ;
MACRO: ndrop ( n -- )
[ drop ] n*quot ;
: nnip ( n -- )
swap >r ndrop r> ; inline
MACRO: ntuck ( n -- )
2 + [ dupd -nrot ] curry ;
MACRO: nrev ( n -- quot )
2008-09-10 23:11:40 -04:00
1 [a,b] [ ] [ '[ @ _ -nrot ] ] reduce ;
MACRO: ndip ( quot n -- )
dup saver -rot restorer 3append ;
MACRO: nslip ( n -- )
dup saver [ call ] rot restorer 3append ;
MACRO: nkeep ( n -- )
[ ] [ 1+ ] [ ] tri
2008-09-10 23:11:40 -04:00
'[ [ _ ndup ] dip _ -nrot _ nslip ] ;
2008-07-13 20:55:42 -04:00
MACRO: ncurry ( n -- )
[ curry ] n*quot ;
2008-07-13 20:55:42 -04:00
MACRO: nwith ( n -- )
[ with ] n*quot ;
MACRO: napply ( n -- )
2 [a,b]
2008-09-10 23:11:40 -04:00
[ [ 1- ] keep '[ _ ntuck _ nslip ] ]
map concat >quotation [ call ] append ;