Jump to page: 1 24  
Page
Thread overview
Idea: swap with multiple arguments
May 23, 2016
Tourist
May 23, 2016
Q. Schroll
May 23, 2016
Jonathan M Davis
May 24, 2016
Observer
May 24, 2016
Jonathan M Davis
May 24, 2016
Observer
May 24, 2016
Adrian Matoga
May 23, 2016
Ali Çehreli
May 24, 2016
Jonathan M Davis
May 24, 2016
ixid
May 24, 2016
H. S. Teoh
May 24, 2016
Wyatt
May 24, 2016
NynnFR
May 24, 2016
Era Scarecrow
May 23, 2016
Era Scarecrow
May 25, 2016
Nordlöw
May 23, 2016
Xinok
May 24, 2016
H. S. Teoh
May 24, 2016
Piotr Szturmaj
May 24, 2016
Xinok
May 24, 2016
Xinok
May 25, 2016
Brian Schott
May 26, 2016
Observer
May 26, 2016
Seb
May 23, 2016
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
May 23, 2016
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.

-Steve
May 23, 2016
On Monday, 23 May 2016 at 20:27:43 UTC, 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.
>
> -Steve

Unless it swaps randomly :)
May 23, 2016
On Monday, 23 May 2016 at 20:27:43 UTC, 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.
>
> -Steve

Just name Andrei's function rotate and make swap just an alias of it with exactly two parameters. No confusion and everyone is happy.
May 23, 2016
On Monday, May 23, 2016 16:27:43 Steven Schveighoffer via Digitalmars-d 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.

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 and 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.

- Jonathan M Davis

May 23, 2016
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.
>
> -Steve

Yes, rotate(), but then I would never remember what direction it rotates.

Ali

May 23, 2016
On Monday, 23 May 2016 at 20:01:08 UTC, 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

It doesn't make sense for "swap" to make take than two arguments and it's unintuitive how it should rearrange the elements when you write swap(a1, a2, a3, ...). Who's to say that it should shift the elements to the left?

I'm not saying this is useless but it would really need a better name. "swap" only makes intuitive sense when it takes two arguments, no more and no less.

While this is technically a rotation, a "rotate" function generally takes an extra argument which is an element/index to rotate on. See: https://www.sgi.com/tech/stl/rotate.html
May 23, 2016
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

-Steve

May 23, 2016
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
May 23, 2016
On Monday, 23 May 2016 at 21:47:31 UTC, Ali Çehreli wrote:
> Yes, rotate(), but then I would never remember what direction it rotates.

 If we take a cue from assembly instructions, there's rol and ror (rotate left/right). These are other instructions are normally unreachable in languages; Although it's usefulness can be wagered from time to time.

 There's also it's cousins rotate with carry (rcl and rcr).

« First   ‹ Prev
1 2 3 4