www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Random Suggestion: Swap Operator <=>?

reply "Julian Salazar" <julian ifeelrandom.com> writes:
I'm wondering, who here would use a swap operator if it were available?

Something that would be normally achieved through:
temp = a;
a = b;
b = temp;
or for the more bit-wise ;)
a ^= b;
b ^= a;
a ^= b;

It's something I've never actually seen implemented in any higher level 
language, but finds uses in linked lists, binary trees, and other data 
structures and algorithms. Temp wouldn't need to be an addressable value, so 
theoretically this operator (which I propose to be "<=>") could be 
compiler-optimized into an x86 xchg opcode for example. It could properly 
deal with references, data structures, class instances, etc. and not require 
the programmer to worry about these details.

I know that the chance of it actually being implemented is close to nil, but 
even a template in the standard library for it would be cool (or did I miss 
something?). Or am I being deluded? :) 
Jul 16 2009
next sibling parent reply Bill Baxter <wbaxter gmail.com> writes:
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
Jul 16 2009
parent "Julian Salazar" <julian ifeelrandom.com> writes:
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
Jul 17 2009
prev sibling next sibling parent Rainer Deyke <rainerd eldwood.com> writes:
Julian Salazar wrote:
 I know that the chance of it actually being implemented is close to nil,
 but even a template in the standard library for it would be cool (or did
 I miss something?). Or am I being deluded? :)
Swap is a common operation that belongs in the standard library, but I don't think it merits its own operator. (I think it's generally safe and efficient to perform a bitwise swap on structs in D, even if those same structs are expensive to copy, so swap definitely deserves language support.) -- Rainer Deyke - rainerd eldwood.com
Jul 16 2009
prev sibling next sibling parent reply "Lionello Lunesu" <lionello lunesu.remove.com> writes:
"Julian Salazar" <julian ifeelrandom.com> wrote in message 
news:h3ofpl$1rur$1 digitalmars.com...
 theoretically this operator (which I propose to be "<=>")
Nooooo! That's my imaginary opCmp operator! L.
Jul 16 2009
next sibling parent Jarrett Billingsley <jarrett.billingsley gmail.com> writes:
On Fri, Jul 17, 2009 at 1:05 AM, Lionello
Lunesu<lionello lunesu.remove.com> wrote:
 "Julian Salazar" <julian ifeelrandom.com> wrote in message
 news:h3ofpl$1rur$1 digitalmars.com...
 theoretically this operator (which I propose to be "<=>")
Nooooo! That's my imaginary opCmp operator!
And it's already a reality in Perl and MiniD ;) Gaww, it's so nice for writing opCmp overloads and sorting predicates.
Jul 17 2009
prev sibling parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Fri, 17 Jul 2009 01:05:09 -0400, Lionello Lunesu  
<lionello lunesu.remove.com> wrote:

 "Julian Salazar" <julian ifeelrandom.com> wrote in message  
 news:h3ofpl$1rur$1 digitalmars.com...
 theoretically this operator (which I propose to be "<=>")
Nooooo! That's my imaginary opCmp operator!
When I first read this I thought "for what, your fantasy programming language?" but I get it now :) -Steve
Jul 17 2009
prev sibling next sibling parent reply Michiel Helvensteijn <m.helvensteijn.remove gmail.com> writes:
Julian Salazar wrote:

 I'm wondering, who here would use a swap operator if it were available?
I have a swap operator in my new programming language: x <-> y; It's syntactically consistent with the assignment operator: x <- E; The swap operator is really syntactic sugar for the swap function, which can be overloaded and templated to your hearts desire (as long as each template type can be automatically deduced by the actual parameters). So yeah, I'd use it. -- Michiel Helvensteijn
Jul 17 2009
parent reply "Simen Kjaeraas" <simen.kjaras gmail.com> writes:
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
Jul 19 2009
parent Michiel Helvensteijn <m.helvensteijn.remove gmail.com> writes:
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
Jul 19 2009
prev sibling next sibling parent reply Leandro Lucarella <llucax gmail.com> writes:
Julian Salazar, el 16 de julio a las 18:14 me escribiste:
 I'm wondering, who here would use a swap operator if it were available?
 
 Something that would be normally achieved through:
 temp = a;
 a = b;
 b = temp;
 or for the more bit-wise ;)
 a ^= b;
 b ^= a;
 a ^= b;
 
 It's something I've never actually seen implemented in any higher level 
 language, but finds uses in linked lists, binary trees, and other data 
 structures and algorithms. Temp wouldn't need to be an addressable value, so 
 theoretically this operator (which I propose to be "<=>") could be 
 compiler-optimized into an x86 xchg opcode for example. It could properly deal 
 with references, data structures, class instances, etc. and not require the 
 programmer to worry about these details.
 
 I know that the chance of it actually being implemented is close to nil, but 
 even a template in the standard library for it would be cool (or did I miss 
 something?). Or am I being deluded? :) 
A more general solution is supporting tuples in the language, then you can do something like: a, b = b, a; There you go (that's actually valid Python code, for example). -- Leandro Lucarella (luca) | Blog colectivo: http://www.mazziblog.com.ar/blog/ ---------------------------------------------------------------------------- GPG Key: 5F5A8D05 (F8CD F9A7 BF00 5431 4145 104C 949E BFB6 5F5A 8D05) ---------------------------------------------------------------------------- En ese preciso instante ella con un leve gemido nos dice: "Ponla, Tito! Ponla!". -- Sidharta Kiwi
Jul 17 2009
next sibling parent reply Michiel Helvensteijn <m.helvensteijn.remove gmail.com> writes:
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); -- Michiel Helvensteijn
Jul 17 2009
parent =?ISO-8859-1?Q?=22J=E9r=F4me_M=2E_Berger=22?= <jeberger free.fr> writes:
Michiel Helvensteijn wrote:
 Leandro Lucarella wrote:
=20
 A more general solution is supporting tuples in the language, then you=
can
 do something like:
 a, b =3D b, a;
=20 That's a great feature. But even if it's available, I'd use swap, becau=
se:
=20
 * I wouldn't have to mention each variable twice
 * It would use move-operations rather than copy operations
=20
 Parallel assignment is still useful for other stuff, like traversing th=
e
 fibonachi sequence with two variables :-)
=20
 (a, b) <- (b, a + b);
=20
Or when you need to reorder more than two variables: a, b, c, d =3D b, d, a, c; Jerome --=20 mailto:jeberger free.fr http://jeberger.free.fr Jabber: jeberger jabber.fr
Jul 17 2009
prev sibling parent bearophile <bearophileHUGS lycos.com> writes:
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
Jul 17 2009
prev sibling parent reply Ary Borenszweig <ary esperanto.org.ar> writes:
Julian Salazar wrote:
 I'm wondering, who here would use a swap operator if it were available?
 
 Something that would be normally achieved through:
 temp = a;
 a = b;
 b = temp;
 or for the more bit-wise ;)
 a ^= b;
 b ^= a;
 a ^= b;
 
 It's something I've never actually seen implemented in any higher level 
 language, but finds uses in linked lists, binary trees, and other data 
 structures and algorithms. Temp wouldn't need to be an addressable 
 value, so theoretically this operator (which I propose to be "<=>") 
 could be compiler-optimized into an x86 xchg opcode for example. It 
 could properly deal with references, data structures, class instances, 
 etc. and not require the programmer to worry about these details.
 
 I know that the chance of it actually being implemented is close to nil, 
 but even a template in the standard library for it would be cool (or did 
 I miss something?). Or am I being deluded? :)
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? The tuple assignment suggestion is better because it can handle more interesting cases.
Jul 17 2009
parent reply Walter Bright <newshound1 digitalmars.com> writes:
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
Jul 17 2009
parent reply "Julian Salazar" <julian ifeelrandom.com> writes:
"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...
Jul 17 2009
next sibling parent Walter Bright <newshound1 digitalmars.com> writes:
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.
Jul 17 2009
prev sibling parent Ary Borenszweig <ary esperanto.org.ar> writes:
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. :-)
Jul 17 2009