View mode: basic / threaded / horizontal-split · Log in · Help
July 17, 2009
Random Suggestion: Swap Operator <=>?
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? :)
July 17, 2009
Re: Random Suggestion: Swap Operator <=>?
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 17, 2009
Re: Random Suggestion: Swap Operator <=>?
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
July 17, 2009
Re: Random Suggestion: Swap Operator <=>?
"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.
July 17, 2009
Re: Random Suggestion: Swap Operator <=>?
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.
July 17, 2009
Re: Random Suggestion: Swap Operator <=>?
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
July 17, 2009
Re: Random Suggestion: Swap Operator <=>?
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
July 17, 2009
Re: Random Suggestion: Swap Operator <=>?
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
July 17, 2009
Re: Random Suggestion: Swap Operator <=>?
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
July 17, 2009
Re: Random Suggestion: Swap Operator <=>?
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.
« First   ‹ Prev
1 2
Top | Discussion index | About this forum | D home