digitalmars.D - Random Suggestion: Swap Operator <=>?
- Julian Salazar (19/19) Jul 16 2009 I'm wondering, who here would use a swap operator if it were available?
- Bill Baxter (4/8) Jul 16 2009 Yes, I think you've just missed something:
- Julian Salazar (5/15) Jul 17 2009 Thanks for the info! Can't believe I missed it...
- Rainer Deyke (8/11) Jul 16 2009 Swap is a common operation that belongs in the standard library, but I
- Lionello Lunesu (4/5) Jul 16 2009 Nooooo! That's my imaginary opCmp operator!
- Jarrett Billingsley (4/9) Jul 17 2009 And it's already a reality in Perl and MiniD ;)
- Steven Schveighoffer (5/9) Jul 17 2009 When I first read this I thought "for what, your fantasy programming
- Michiel Helvensteijn (11/12) Jul 17 2009 I have a swap operator in my new programming language:
- Simen Kjaeraas (5/9) Jul 19 2009 Clearly, this should be generalized to allow both right-to-left and
- Michiel Helvensteijn (7/17) Jul 19 2009 I thought about that. Of course it has a certain symmetry. But having tw...
- Leandro Lucarella (13/35) Jul 17 2009 A more general solution is supporting tuples in the language, then you c...
- Michiel Helvensteijn (9/12) Jul 17 2009 That's a great feature. But even if it's available, I'd use swap, becaus...
- =?ISO-8859-1?Q?=22J=E9r=F4me_M=2E_Berger=22?= (11/27) Jul 17 2009 se:
- bearophile (5/8) Jul 17 2009 I have implemented that time ago for ShedSkin, here George Sakkis (first...
- Ary Borenszweig (6/28) Jul 17 2009 Oh, come on, It's just three lines of code! And it's not *that* common.
- Walter Bright (5/8) Jul 17 2009 A request for a swap operators comes up now and then in the C/C++ forums...
- Julian Salazar (18/26) Jul 17 2009 Well, when was the last time you used "!<="? Ah well, my ignorance of th...
- Walter Bright (3/18) Jul 17 2009 It looks nice, but doesn't seem to offer a big improvement.
- Ary Borenszweig (2/18) Jul 17 2009 Don't worry. In this newsgroup *any* topic sparks debate. :-)
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
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
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
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
"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
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...And it's already a reality in Perl and MiniD ;) Gaww, it's so nice for writing opCmp overloads and sorting predicates.theoretically this operator (which I propose to be "<=>")Nooooo! That's my imaginary opCmp operator!
Jul 17 2009
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...When I first read this I thought "for what, your fantasy programming language?" but I get it now :) -Stevetheoretically this operator (which I propose to be "<=>")Nooooo! That's my imaginary opCmp operator!
Jul 17 2009
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
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
Simen Kjaeraas wrote: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 HelvensteijnI 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
Jul 19 2009
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
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
Michiel Helvensteijn wrote:Leandro Lucarella wrote: =20canA more general solution is supporting tuples in the language, then you=se: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==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=efibonachi sequence with two variables :-) =20 (a, b) <- (b, a + b); =20Or 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
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
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
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
"Walter Bright" <newshound1 digitalmars.com> wrote in message news:4A610016.2040104 digitalmars.com...Ary Borenszweig wrote: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...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
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
Julian Salazar escribió:"Walter Bright" <newshound1 digitalmars.com> wrote in message news:4A610016.2040104 digitalmars.com...Don't worry. In this newsgroup *any* topic sparks debate. :-)Ary Borenszweig wrote:Well, when was the last time you used "!<="? Ah well, my ignorance of the swap library template has sparked debate.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