Handbook
Glossary
npick ( n -- quot )
Factor handbook
»
The language
»
Combinators
»
Generalized shuffle words and combinators
»
Generalized shuffle words
Prev:
dupn ( n -- quot )
Next:
nrot ( n -- quot )
Vocabulary
generalizations
Inputs
n
an
integer
Outputs
None
Word description
A generalization of
dup
,
over
and
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
USING: kernel generalizations prettyprint sequences.generalizations ; 1 2 3 4 4 npick 5 narray .
{ 1 2 3 4 1 }
Some core words expressed in terms of
npick
:
dup
1 npick
over
2 npick
pick
3 npick
Definition
USING:
combinators
kernel
math
;
IN:
generalizations
MACRO:
npick
( n -- quot )
{
{
[
dup
0
<=
]
[
positive-number-expected
]
}
{
[
dup
1
=
]
[
drop
[
dup
]
]
}
[
1
-
[
dup
]
[
[
dip
swap
]
curry
]
swapd
times
]
}
cond
;