zachgates / Gogh

An esoteric, stack-based language
8 stars 1 forks source link

Add `duplicate top n stack elements` function #4

Closed luser-dr00g closed 8 years ago

luser-dr00g commented 8 years ago

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.

luser-dr00g commented 8 years ago

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