Slava Pestov 2008-07-08 12:34:52 -05:00
parent 400cde1fe6
commit 267a24c0de
6 changed files with 152 additions and 0 deletions

View File

@ -0,0 +1,2 @@
Chris Double
Doug Coleman

View File

@ -0,0 +1,84 @@
! Copyright (C) 2007 Chris Double.
! See http://factorcode.org/license.txt for BSD license.
USING: help.syntax help.markup kernel sequences ;
IN: shuffle
HELP: npick
{ $values { "n" "a number" } }
{ $description "A generalisation of " { $link dup } ", "
{ $link over } " and " { $link pick } " that can work "
"for any stack depth. The nth item down the stack will be copied and "
"placed on the top of the stack."
}
{ $examples
{ $example "USING: prettyprint shuffle ;" "1 2 3 4 4 npick .s" "1\n2\n3\n4\n1" }
}
{ $see-also dup over pick } ;
HELP: ndup
{ $values { "n" "a number" } }
{ $description "A generalisation of " { $link dup } ", "
{ $link 2dup } " and " { $link 3dup } " that can work "
"for any number of items. The n topmost items on the stack will be copied and "
"placed on the top of the stack."
}
{ $examples
{ $example "USING: prettyprint shuffle ;" "1 2 3 4 4 ndup .s" "1\n2\n3\n4\n1\n2\n3\n4" }
}
{ $see-also dup 2dup 3dup } ;
HELP: nnip
{ $values { "n" "a number" } }
{ $description "A generalisation of " { $link nip } " and " { $link 2nip }
" that can work "
"for any number of items."
}
{ $examples
{ $example "USING: prettyprint shuffle ;" "1 2 3 4 3 nnip .s" "4" }
}
{ $see-also nip 2nip } ;
HELP: ndrop
{ $values { "n" "a number" } }
{ $description "A generalisation of " { $link drop }
" that can work "
"for any number of items."
}
{ $examples
{ $example "USING: prettyprint shuffle ;" "1 2 3 4 3 ndrop .s" "1" }
}
{ $see-also drop 2drop 3drop } ;
HELP: nrot
{ $values { "n" "a number" } }
{ $description "A generalisation of " { $link rot } " that works for any "
"number of items on the stack. "
}
{ $examples
{ $example "USING: prettyprint shuffle ;" "1 2 3 4 4 nrot .s" "2\n3\n4\n1" }
}
{ $see-also rot -nrot } ;
HELP: -nrot
{ $values { "n" "a number" } }
{ $description "A generalisation of " { $link -rot } " that works for any "
"number of items on the stack. "
}
{ $examples
{ $example "USING: prettyprint shuffle ;" "1 2 3 4 4 -nrot .s" "4\n1\n2\n3" }
}
{ $see-also rot nrot } ;
ARTICLE: { "shuffle" "overview" } "Extra shuffle words"
"A number of stack shuffling words for those rare times when you "
"need to deal with tricky stack situations and can't refactor the "
"code to work around it."
{ $subsection ndup }
{ $subsection npick }
{ $subsection nrot }
{ $subsection -nrot }
{ $subsection nnip }
{ $subsection ndrop } ;
IN: shuffle
ABOUT: { "shuffle" "overview" }

View File

@ -0,0 +1,25 @@
USING: arrays shuffle kernel math tools.test inference words ;
[ 8 ] [ 5 6 7 8 3nip ] unit-test
{ 1 2 3 4 1 } [ 1 2 3 4 4 npick ] unit-test
{ 1 2 3 4 2 } [ 1 2 3 4 3 npick ] unit-test
{ 1 2 3 4 3 } [ 1 2 3 4 2 npick ] unit-test
{ 1 2 3 4 4 } [ 1 2 3 4 1 npick ] unit-test
{ t } [ [ 1 1 ndup ] infer >boolean ] unit-test
{ 1 1 } [ 1 1 ndup ] unit-test
{ 1 2 1 2 } [ 1 2 2 ndup ] unit-test
{ 1 2 3 1 2 3 } [ 1 2 3 3 ndup ] unit-test
{ 1 2 3 4 1 2 3 4 } [ 1 2 3 4 4 ndup ] unit-test
{ t } [ [ 1 2 2 nrot ] infer >boolean ] unit-test
{ 2 1 } [ 1 2 2 nrot ] unit-test
{ 2 3 1 } [ 1 2 3 3 nrot ] unit-test
{ 2 3 4 1 } [ 1 2 3 4 4 nrot ] unit-test
{ t } [ [ 1 2 2 -nrot ] infer >boolean ] unit-test
{ 2 1 } [ 1 2 2 -nrot ] unit-test
{ 3 1 2 } [ 1 2 3 3 -nrot ] unit-test
{ 4 1 2 3 } [ 1 2 3 4 4 -nrot ] unit-test
{ t } [ [ 1 2 3 4 3 nnip ] infer >boolean ] unit-test
{ 4 } [ 1 2 3 4 3 nnip ] unit-test
{ t } [ [ 1 2 3 4 4 ndrop ] infer >boolean ] unit-test
{ 0 } [ 0 1 2 3 4 4 ndrop ] unit-test
[ 3 1 2 3 ] [ 1 2 3 tuckd ] unit-test

View File

@ -0,0 +1,39 @@
! Copyright (C) 2007 Chris Double, Doug Coleman.
! See http://factorcode.org/license.txt for BSD license.
USING: kernel sequences namespaces math inference.transforms
combinators macros quotations math.ranges fry ;
IN: shuffle
MACRO: npick ( n -- ) 1- dup saver [ dup ] rot [ r> swap ] n*quot 3append ;
MACRO: ndup ( n -- ) dup [ npick ] curry 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 + '[ dup , -nrot ] ;
: 2swap ( x y z t -- z t x y ) rot >r rot r> ; inline
: nipd ( a b c -- b c ) rot drop ; inline
: 3nip ( a b c d -- d ) 3 nnip ; inline
: 4nip ( a b c d e -- e ) 4 nnip ; inline
: 4dup ( a b c d -- a b c d a b c d ) 4 ndup ; inline
: 4drop ( a b c d -- ) 3drop drop ; inline
: tuckd ( x y z -- z x y z ) 2 ntuck ; inline
MACRO: nrev ( n -- quot )
[ 1+ ] map
reverse
[ [ -nrot ] curry ] map concat ;

View File

@ -0,0 +1 @@
Additional shuffle words

1
extra/shuffle/tags.txt Normal file
View File

@ -0,0 +1 @@
extensions