Stack Shuffling
Factor handbook ยป Guided tour of Factor

Prev:Parsing Words
Next:Combinators


Now that you know the basics of Factor, you may want to start assembling more complex words. This may sometimes require you to use variables that are not on top of the stack, or to use variables more than once. There are a few words that can be used to help with this. I mention them now since you need to be aware of them, but I warn you that using too many of these words to manipulate the stack will cause your code to quickly become harder to read and write. Stack shuffling requires mentally simulating moving values on a stack, which is not a natural way to program. In the next section we'll see a much more effective way to handle most needs.

Here is a list of the most common shuffling words together with their effect on the stack. Try them in the listener to get a feel for how they manipulate the stack.
dup ( x -- x x )

drop ( x -- )

swap ( x y -- y x )

over ( x y -- x y x )

dupd ( x y -- x x y )

swapd ( x y z -- y x z )

nip ( x y -- y )

rot ( x y z -- y z x )

-rot ( x y z -- z x y )

2dup ( x y -- x y x y )


For a deeper look at stack shuffling, see the Shuffle word and definition cookbook.