Add `duplicate top n stack elements` function
luser-dr00g opened this issue · comments
M Joshua Ryan commented
Since you already have a function to rotate the nth item to the top, then with this new function you'd have the ability to iterate bottom-to-top non-destructively over a copy of (portions of) the stack.
More detail about how this works in postscript in my article here:
The count -1 1{-1 roll =}for
Idiom.
Specifically it enables you to write a 'print stack' function entirely in user code. And in postscript at least, it can often be used to avoid creation of an array.
M Joshua Ryan commented
Same idea is used in this postscript procedure to concatenate n strings without creating a temp array just to iterate through it. http://stackoverflow.com/a/12472783/733077