July 17, 2009
Michiel Helvensteijn wrote:
> Leandro Lucarella wrote:
> 
>> A more general solution is supporting tuples in the language, then you can
>> do something like:
>> a, b = b, a;
> 
> That's a great feature. But even if it's available, I'd use swap, because:
> 
> * I wouldn't have to mention each variable twice
> * It would use move-operations rather than copy operations
> 
> Parallel assignment is still useful for other stuff, like traversing the fibonachi sequence with two variables :-)
> 
> (a, b) <- (b, a + b);
> 
	Or when you need to reorder more than two variables:

a, b, c, d = b, d, a, c;

		Jerome
-- 
mailto:jeberger@free.fr
http://jeberger.free.fr
Jabber: jeberger@jabber.fr



July 17, 2009
Leandro Lucarella:
> A more general solution is supporting tuples in the language, then you can
> do something like:
> a, b = b, a;

I have implemented that time ago for ShedSkin, here George Sakkis (first answer) suggests how to minimize the number of auxiliary variables:
http://groups.google.com/group/comp.lang.python/browse_thread/thread/a218f066bef2fc9e/931a4331b18bc5f5

Bye,
bearophile
July 17, 2009
Ary Borenszweig wrote:
> Oh, come on, It's just three lines of code! And it's not *that* common. I know it's kind of basic and primitive, but there's a standard library function for it. Why add an operator *just* for that?

A request for a swap operators comes up now and then in the C/C++ forums as well. The answer given, which applies to D as well, is that:

1. it is too rarely used to merit a special operator just for it

2. it is handled adequately with library code or even just inlining it
July 18, 2009
Thanks for the info! Can't believe I missed it...

(I think I accidentally clicked reply to sender instead of reply to group, sorry 'bout that.)

"Bill Baxter" <wbaxter@gmail.com> wrote in message news:mailman.105.1247790231.14071.digitalmars-d@puremagic.com...
> On Thu, Jul 16, 2009 at 5:14 PM, Julian Salazar<julian@ifeelrandom.com> wrote:
>> I'm wondering, who here would use a swap operator if it were available?
>> ...
>> even a template in the standard library for it would be cool (or did I miss
>> something?). Or am I being deluded? :)
>
>
> Yes, I think you've just missed something:
> http://www.digitalmars.com/d/2.0/phobos/std_algorithm#swap
>
> --bb 

July 18, 2009
"Walter Bright" <newshound1@digitalmars.com> wrote in message news:4A610016.2040104@digitalmars.com...
> Ary Borenszweig wrote:
>> Oh, come on, It's just three lines of code! And it's not *that* common. I know it's kind of basic and primitive, but there's a standard library function for it. Why add an operator *just* for that?
>
> A request for a swap operators comes up now and then in the C/C++ forums as well. The answer given, which applies to D as well, is that:
>
> 1. it is too rarely used to merit a special operator just for it
>
> 2. it is handled adequately with library code or even just inlining it

Well, when was the last time you used "!<="? Ah well, my ignorance of the swap library template has sparked debate. Sorry, I'll make sure to search better next time.

But I'm wondering, what's your opinion of the generalized tuple assignment syntax talked about above? Or more generally, supporting tuples in the language?
// Swap
a, b = b, a;
// Fibonacci!
a, b = b, a+b;
// Switching positions with the previous item in a doubly linked list, just cause ;)
item.prev.prev, item.prev.next, item.next, item.prev, item.prev.prev.next, item.next.prev = item.prev.next, item.next, item.prev, item.prev.prev, item, item.prev;

Okay, maybe I'll stop now... 

July 18, 2009
Julian Salazar wrote:
> Well, when was the last time you used "!<="?

As I remarked to Derek, there is no such thing as absolutes in engineering.

> But I'm wondering, what's your opinion of the generalized tuple assignment syntax talked about above? Or more generally, supporting tuples in the language?
> // Swap
> a, b = b, a;
> // Fibonacci!
> a, b = b, a+b;
> // Switching positions with the previous item in a doubly linked list, just cause ;)
> item.prev.prev, item.prev.next, item.next, item.prev, item.prev.prev.next, item.next.prev = item.prev.next, item.next, item.prev, item.prev.prev, item, item.prev;
> 
> Okay, maybe I'll stop now...

It looks nice, but doesn't seem to offer a big improvement.
July 18, 2009
Julian Salazar escribió:
> "Walter Bright" <newshound1@digitalmars.com> wrote in message news:4A610016.2040104@digitalmars.com...
>> Ary Borenszweig wrote:
>>> Oh, come on, It's just three lines of code! And it's not *that* common. I know it's kind of basic and primitive, but there's a standard library function for it. Why add an operator *just* for that?
>>
>> A request for a swap operators comes up now and then in the C/C++ forums as well. The answer given, which applies to D as well, is that:
>>
>> 1. it is too rarely used to merit a special operator just for it
>>
>> 2. it is handled adequately with library code or even just inlining it
> 
> Well, when was the last time you used "!<="? Ah well, my ignorance of the swap library template has sparked debate.

Don't worry. In this newsgroup *any* topic sparks debate. :-)
July 19, 2009
Michiel Helvensteijn wrote:

> I have a swap operator in my new programming language:
>
> x <-> y;
>
> It's syntactically consistent with the assignment operator:
>
> x <- E;

Clearly, this should be generalized to allow both right-to-left and left-to-right assignment. :p

-- 
  Simen
July 19, 2009
Simen Kjaeraas wrote:

>> I have a swap operator in my new programming language:
>>
>> x <-> y;
>>
>> It's syntactically consistent with the assignment operator:
>>
>> x <- E;
> 
> Clearly, this should be generalized to allow both right-to-left and left-to-right assignment. :p

I thought about that. Of course it has a certain symmetry. But having two different syntaxes for standard assignment isn't right.

And right-to-left has its advantages. You can see at a glance which variable is going to change value.

-- 
Michiel Helvensteijn

1 2
Next ›   Last »