Idea: swap with multiple arguments

Observer via Digitalmars-d digitalmars-d at puremagic.com
Mon May 23 19:40:24 PDT 2016


On Monday, 23 May 2016 at 21:38:54 UTC, Jonathan M Davis wrote:
>> One thing that screams out to me: this should be called 
>> rotate, not swap.
>
> That would probably be better. My immediate thought on reading 
> Andrei's suggestion for swap was that it would be way too easy 
> to forget what's actually being swapped when there are more 
> than two arguments. Rotate at least implies an ordering to it.
>
> Though I confess that I've never heard of a swap function (or 
> anything like a swap function) that's swapped more than two 
> arguments, and I'm not aware of any point in time when I 
> thought that such a thing might be useful. So, I'm not exactly 
> clamoring for a function like this, but that obviously doesn't 
> mean that it's not worth adding, just that I don't personally 
> see any particular value in it.

If you don't see value in it, you've neglected to learn 
PostScript.
There, a similar capability is used to "rotate" a certain number
of the top elements on the operand stack.  However, because
PostScript is a graphical language, the "rotate" operator instead
does a 2D coordinate-system rotation.  For the functionality that
Andrei wants, PostScript uses the "roll" operator, which performs
a circular shift of the top operands on the stack.  Aside from
those operands themselves, it takes an argument n (the number of
operands to roll) and an argument j (the number of positions to
roll, which can be either positive ("upward" motion on the stack)
or negative ("downward" motion on the stack)).  To be more visual
about it, if j is 1, the top element of the stack is popped and
inserted further into the stack.

https://www.adobe.com/products/postscript/pdfs/PLRM.pdf (page 483
by printed-page numbering, page 489 within the PDF file) has all
the details for this operator.

The point is that alternative terms like "roll" and "circular
shift" might be useful in naming this function, instead of either
swap (which I agree is confusing) or rotate.  And while n can
be imputed from the number of arguments passed, it might be
useful to pass j explicitly, allowing shifts of more than one
position and in either direction.

(Speaking of graphical contexts, if you were working in 3D, you
might consider the meaning of "roll" to be "reserved",  in the
sense of roll/pitch/yaw.)

As for utility, if you're a PostScript programmer, where keeping
track of data is done via a stack-oriented model, this capability
gets used all the time, to bring relevant arguments to the top
of the stack to be operated upon, or to shove arguments deeper
into the stack for later use.


More information about the Digitalmars-d mailing list