May 23, 2016 Re: Idea: swap with multiple arguments | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrei Alexandrescu | On 5/23/16 6:22 PM, Andrei Alexandrescu wrote:
> On 05/23/2016 04:27 PM, Steven Schveighoffer wrote:
>> On 5/23/16 4:01 PM, Andrei Alexandrescu wrote:
>>> So swap(a, b) swaps the contents of a and b. This could be easily
>>> generalized to multiple arguments such that swap(a1, a2, ..., an)
>>> arranges things such that a1 gets an, a2 gets a1, a3 gets a2, etc. I do
>>> know applications for three arguments. Thoughts? -- Andrei
>>
>> One thing that screams out to me: this should be called rotate, not swap.
>
> Problem is the usefulness peaks at 2-3 args, and for 2 and 3 arguments
> "rotate" is overkill. -- Andrei
OK, let me rephrase. For 3 arguments, swap is a totally unintuitive name :)
I personally think rotate is fine, is describing pretty much exactly what is happening, and scales well. But I guess there is some existing meaning in Phobos for it, so that may not fly.
-Steve
|
May 23, 2016 Re: Idea: swap with multiple arguments | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrei Alexandrescu | On Mon, May 23, 2016 at 04:01:08PM -0400, Andrei Alexandrescu via Digitalmars-d wrote: > So swap(a, b) swaps the contents of a and b. This could be easily generalized to multiple arguments such that swap(a1, a2, ..., an) arranges things such that a1 gets an, a2 gets a1, a3 gets a2, etc. I do know applications for three arguments. Thoughts? -- Andrei As others have said, 'swap' is a horrible name for this. I'd go with 'rotate', but then there's the question of which direction it rotates. Conceivably, it's equally valid for an to get a1, a1 to get a2, etc.. So 'rotateRight' would be most unambiguous, though a handful to type. T -- What's an anagram of "BANACH-TARSKI"? BANACH-TARSKI BANACH-TARSKI. |
May 23, 2016 Re: Idea: swap with multiple arguments | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | On Monday, May 23, 2016 18:10:02 Steven Schveighoffer via Digitalmars-d wrote:
> On 5/23/16 5:47 PM, Ali Çehreli wrote:
> > On 05/23/2016 01:27 PM, Steven Schveighoffer wrote:
> >> On 5/23/16 4:01 PM, Andrei Alexandrescu wrote:
> >>> So swap(a, b) swaps the contents of a and b. This could be easily generalized to multiple arguments such that swap(a1, a2, ..., an) arranges things such that a1 gets an, a2 gets a1, a3 gets a2, etc. I do know applications for three arguments. Thoughts? -- Andrei
> >>
> >> One thing that screams out to me: this should be called rotate, not swap.
> >
> > Yes, rotate(), but then I would never remember what direction it rotates.
>
> Use the law of UFCS:
>
> x.rotate(y, z); // x-> y, y -> z, z -> x
Hmmm. And I would have assumed that it rotated in the other direction. This is really going to need a very specific name like rotateLeft or rotateRight in order for it not to be error-prone.
- Jonathan M Davis
|
May 24, 2016 Re: Idea: swap with multiple arguments | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jonathan M Davis | 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. |
May 24, 2016 Re: Idea: swap with multiple arguments | ||||
---|---|---|---|---|
| ||||
Posted in reply to Observer | On Tuesday, May 24, 2016 02:40:24 Observer via Digitalmars-d wrote:
> If you don't see value in it, you've neglected to learn PostScript.
I wasn't aware that postscript was a programming language. All I know about it is that it's what printers talk (sometimes), and I assumed that it was a protocol.
- Jonathan M Davis
|
May 24, 2016 Re: Idea: swap with multiple arguments | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jonathan M Davis | On Tuesday, 24 May 2016 at 01:18:05 UTC, Jonathan M Davis wrote:
> Hmmm. And I would have assumed that it rotated in the other direction. This is really going to need a very specific name like rotateLeft or rotateRight in order for it not to be error-prone.
>
> - Jonathan M Davis
Why would you assume it would rotate left? As a general rule to avoid verbosity we could have a default assumption of things moving in one direction and the inverse gets named. So we would have rotate and rotateLeft.
|
May 24, 2016 Re: Idea: swap with multiple arguments | ||||
---|---|---|---|---|
| ||||
Posted in reply to H. S. Teoh | On 2016-05-24 02:16, H. S. Teoh via Digitalmars-d wrote:
> On Mon, May 23, 2016 at 04:01:08PM -0400, Andrei Alexandrescu via Digitalmars-d wrote:
>> So swap(a, b) swaps the contents of a and b. This could be easily
>> generalized to multiple arguments such that swap(a1, a2, ..., an)
>> arranges things such that a1 gets an, a2 gets a1, a3 gets a2, etc. I
>> do know applications for three arguments. Thoughts? -- Andrei
>
> As others have said, 'swap' is a horrible name for this. I'd go with
> 'rotate', but then there's the question of which direction it rotates.
> Conceivably, it's equally valid for an to get a1, a1 to get a2, etc..
> So 'rotateRight' would be most unambiguous, though a handful to type.
I would do it as rotate(RandomAccessRange, ptrdiff_t shift).
shift >= 1 means rotate right by amount of shift
shift <= -1 means rotate left
shift == 0 does nothing
This way it's more general, it's possible to rotate by any amount, left or right.
|
May 24, 2016 Re: Idea: swap with multiple arguments | ||||
---|---|---|---|---|
| ||||
Posted in reply to Observer | On Tuesday, 24 May 2016 at 02:40:24 UTC, Observer wrote:
> 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.
Forth also has "swap" and "rot". The former moves 2nd value to the top of the stack (i.e. a, b becomes b, a). The latter moves 3rd value to the top (i.e. a, b, c becomes b, c, a).
|
May 24, 2016 Re: Idea: swap with multiple arguments | ||||
---|---|---|---|---|
| ||||
Posted in reply to ixid | On Tue, May 24, 2016 at 09:21:07AM +0000, ixid via Digitalmars-d wrote: > On Tuesday, 24 May 2016 at 01:18:05 UTC, Jonathan M Davis wrote: > > Hmmm. And I would have assumed that it rotated in the other direction. This is really going to need a very specific name like rotateLeft or rotateRight in order for it not to be error-prone. > > > > - Jonathan M Davis > > Why would you assume it would rotate left? As a general rule to avoid verbosity we could have a default assumption of things moving in one direction and the inverse gets named. So we would have rotate and rotateLeft. And why would you assume that it would rotate right? The default assumption, unless stated clearly and widely followed everywhere, is unclear and leads to misunderstandings. I'd rather unambiguously name them rotateRight and rotateLeft than the asymmetric rotate / rotateLeft. T -- What is Matter, what is Mind? Never Mind, it doesn't Matter. |
May 24, 2016 Re: Idea: swap with multiple arguments | ||||
---|---|---|---|---|
| ||||
Posted in reply to H. S. Teoh | On Tuesday, 24 May 2016 at 12:22:01 UTC, H. S. Teoh wrote:
>
> And why would you assume that it would rotate right? The default assumption, unless stated clearly and widely followed everywhere, is unclear and leads to misunderstandings. I'd rather unambiguously name them rotateRight and rotateLeft than the asymmetric rotate / rotateLeft.
>
In the APL family, we have dyadic ⌽ and ⊖:
3⌽⍳9 ⍝ left
4 5 6 7 8 9 1 2 3
¯3⌽⍳9 ⍝ right
7 8 9 1 2 3 4 5 6
3 3⍴⍳9
1 2 3
4 5 6
7 8 9
1⊖3 3⍴⍳9 ⍝ up
4 5 6
7 8 9
1 2 3
¯1⊖3 3⍴⍳9 ⍝ down
7 8 9
1 2 3
4 5 6
They rotate left and up in the positive case because that yields the natural indexing order. Right rotation gives you a reverse ordering.
-Wyatt
|
Copyright © 1999-2021 by the D Language Foundation