www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Fun with opPos()

reply Arcane Jill <Arcane_member pathlink.com> writes:
Well, now we have opPos() to complement opNeg(). We can overload the unary plus
operator.

The obvious implementation is:

#    T opPos() { return this; }

(or .dup, or similar). But such trivial functions do seem a little pointless.
What /else/ could we do with opPlus()? 

This is a non-serious thread, so humorous replies are welcome!
Jill
Jul 29 2004
next sibling parent Juanjo =?ISO-8859-15?Q?=C1lvarez?= <juanjuxNO SPAMyahoo.es> writes:
Arcane Jill wrote:

 Well, now we have opPos() to complement opNeg(). We can overload the unary
 plus operator.
 
 The obvious implementation is:
 
 #    T opPos() { return this; }
 
 (or .dup, or similar). But such trivial functions do seem a little
 pointless. What /else/ could we do with opPlus()?
 
 This is a non-serious thread, so humorous replies are welcome!
 Jill

You can use it to put some random obfuscated and lengthy code to impress your boss and made him think you work a lot instead of reading the D newsgroups.
Jul 29 2004
prev sibling next sibling parent reply pragma <EricAnderton at yahoo dot com> <pragma_member pathlink.com> writes:
In article <ceamke$1bfo$1 digitaldaemon.com>, Arcane Jill says...
(or .dup, or similar). But such trivial functions do seem a little pointless.
What /else/ could we do with opPlus()? 

This is a non-serious thread, so humorous replies are welcome!
Jill

Actually, this might be the answer Matthew was looking for since we cannot overload the pointer-to operator (*).
 class Boxed(T){
     T value;
     this(T value){ this.value = value; }
     T opPos(){ return(this.value); }
 }

In this way, opPos now returns the underlying value, which could be quite a boon for template programming as this helps objects look more like primitives.
 Boxed!(int) intBox = new Boxed!(int)(42);
 int fortyTwo = 42;
 assert(+intBox == +fortyTwo);

- Pragma
Jul 29 2004
parent h3r3tic <h3r3tic_member pathlink.com> writes:
In article <ceavql$1ej2$1 digitaldaemon.com>, pragma <EricAnderton at yahoo dot
com> says...
Actually, this might be the answer Matthew was looking for since we cannot
overload the pointer-to operator (*).

 class Boxed(T){
     T value;
     this(T value){ this.value = value; }
     T opPos(){ return(this.value); }
 }

In this way, opPos now returns the underlying value, which could be quite a boon for template programming as this helps objects look more like primitives.
 Boxed!(int) intBox = new Boxed!(int)(42);
 int fortyTwo = 42;
 assert(+intBox == +fortyTwo);


Let's think: 1. it's barely readable 2. hmmm, ++ for double dereference ? by the way, why can't we overload the * unary operator ?
Jul 29 2004
prev sibling next sibling parent reply Andy Friesen <andy ikagames.com> writes:
Arcane Jill wrote:
 Well, now we have opPos() to complement opNeg(). We can overload the unary plus
 operator.
 
 The obvious implementation is:
 
 #    T opPos() { return this; }
 
 (or .dup, or similar). But such trivial functions do seem a little pointless.
 What /else/ could we do with opPlus()? 
 
 This is a non-serious thread, so humorous replies are welcome!

T opPos() { return opNeg(); } It also might be useful in situations where one wants to use overloaded operators to create syntax that resembles another language. (like how Boost.Spirit apes BNF to create parsers) -- andy
Jul 29 2004
parent Norbert Nemec <Norbert.Nemec gmx.de> writes:
Andy Friesen wrote:

 Arcane Jill wrote:
 Well, now we have opPos() to complement opNeg(). We can overload the
 unary plus operator.
 
 The obvious implementation is:
 
 #    T opPos() { return this; }
 
 (or .dup, or similar). But such trivial functions do seem a little
 pointless. What /else/ could we do with opPlus()?
 
 This is a non-serious thread, so humorous replies are welcome!

T opPos() { return opNeg(); } It also might be useful in situations where one wants to use overloaded operators to create syntax that resembles another language. (like how Boost.Spirit apes BNF to create parsers)

Walter makes it very clear in the specs that he is against that practice. So be prepared for some major obstacles trying to follow that road...
Jul 29 2004
prev sibling next sibling parent reply Regan Heath <regan netwin.co.nz> writes:
Sorry for being serious'n'all but aren't opPos and opNeg perfectly useful 
if you're implementing something that can be positive or negative?

Like your Int class Jill?

eg.

class SimpleInt {
   int value;
   SimpleInt opPos() { if (value < 0) value = -value; return this; }
   SimpleInt opNeg() { if (value > 0) value = -value; return this; }
}

or am I missing something?

Regan

On Thu, 29 Jul 2004 11:21:50 +0000 (UTC), Arcane Jill 
<Arcane_member pathlink.com> wrote:
 Well, now we have opPos() to complement opNeg(). We can overload the 
 unary plus
 operator.

 The obvious implementation is:

 #    T opPos() { return this; }

 (or .dup, or similar). But such trivial functions do seem a little 
 pointless.
 What /else/ could we do with opPlus()?

 This is a non-serious thread, so humorous replies are welcome!
 Jill

-- Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Jul 29 2004
next sibling parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
*techincally* opPos returns the number.  it's like multiplying by 1.  it
does nothing.  but what you've just done is made it operate like abs(n) ;)

also opNeg is like multiplying by -1, what you've done is like 0-abs(n).

or we could overload opPos to do something horrible, like cause an access
violation.  or play some country music  :o
Jul 29 2004
parent Regan Heath <regan netwin.co.nz> writes:
On Fri, 30 Jul 2004 00:59:06 -0400, Jarrett Billingsley 
<kb3ctd2 yahoo.com> wrote:
 *techincally* opPos returns the number.  it's like multiplying by 1.  it
 does nothing.  but what you've just done is made it operate like abs(n) 
 ;)

Thanks, I have never used opPos (operator+) nor have I used it on an int or anything like that so I had no idea what it did. I assumed it did something useful. It appears to be useless?
 also opNeg is like multiplying by -1, what you've done is like 0-abs(n).

Oops so I have, I should have realised that.
 or we could overload opPos to do something horrible, like cause an access
 violation.  or play some country music  :o

If it's as useless as it appears to me.. the only thing stopping you is the sheer lunacy of it. Regan. -- Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Jul 29 2004
prev sibling parent Arcane Jill <Arcane_member pathlink.com> writes:
In article <opsbxhk9m95a2sq9 digitalmars.com>, Regan Heath says...
Sorry for being serious'n'all but aren't opPos and opNeg perfectly useful 
if you're implementing something that can be positive or negative?

Like your Int class Jill?

eg.

class SimpleInt {
   int value;
   SimpleInt opPos() { if (value < 0) value = -value; return this; }
   SimpleInt opNeg() { if (value > 0) value = -value; return this; }
}

or am I missing something?

Clearly. (Sorry). I'd want x = -y; to assign x with minus y, not with -abs(y). If y is -3 then the statement x = -y; should (and will) result in (x == 3). And there is no way I'm going to change that. Int does provide the function abs(). In math, the main use for unary plus is to turn a double-valued result into a single-valued result. For instance sqrt(2) is double-valued, but +sqrt(2) is single-valued. It's hard to see how that could be useful in D. Jill
Jul 30 2004
prev sibling next sibling parent reply Andy Friesen <andy ikagames.com> writes:
Arcane Jill wrote:
 Well, now we have opPos() to complement opNeg(). We can overload the unary plus
 operator.
 
 The obvious implementation is:
 
 #    T opPos() { return this; }
 
 (or .dup, or similar). But such trivial functions do seem a little pointless.
 What /else/ could we do with opPlus()? 
 
 This is a non-serious thread, so humorous replies are welcome!

The most obvious thing in the world: void opPos() { this += 0.5; } -- andy
Jul 29 2004
parent Arcane Jill <Arcane_member pathlink.com> writes:
In article <cecmgc$26dk$1 digitaldaemon.com>, Andy Friesen says...
The most obvious thing in the world:

     void opPos() { this += 0.5; }

LOL. Excellent! Jill
Jul 30 2004
prev sibling next sibling parent Mike Parker <aldacron71 yahoo.com> writes:
Arcane Jill wrote:
 Well, now we have opPos() to complement opNeg(). We can overload the unary plus
 operator.
 
 The obvious implementation is:
 
 #    T opPos() { return this; }
 
 (or .dup, or similar). But such trivial functions do seem a little pointless.
 What /else/ could we do with opPlus()? 

char[] opPos() { return "The glass is half full" };
Jul 29 2004
prev sibling next sibling parent "Martin M. Pedersen" <martin moeller-pedersen.dk> writes:
"Arcane Jill" <Arcane_member pathlink.com> skrev i en meddelelse
news:ceamke$1bfo$1 digitaldaemon.com...
 Well, now we have opPos() to complement opNeg(). We can overload the unary

 operator.
 (or .dup, or similar). But such trivial functions do seem a little

 What /else/ could we do with opPlus()?
 This is a non-serious thread, so humorous replies are welcome!

It is an obvious candidate for hiding easter eggs in libraries. Regards, Martin M. Pedersen
Jul 30 2004
prev sibling next sibling parent J Anderson <REMOVEanderson badmama.com.au> writes:
Arcane Jill wrote:

Well, now we have opPos() to complement opNeg(). We can overload the unary plus
operator.

The obvious implementation is:

#    T opPos() { return this; }

(or .dup, or similar). But such trivial functions do seem a little pointless.
What /else/ could we do with opPlus()? 

This is a non-serious thread, so humorous replies are welcome!
Jill

struct Test { Test opPos() { printf("+\n"); return *this; } Test opNeg() { printf("-\n"); return *this; } } void main() { Test t; + - + + +t; } I guess it could have all kinds of evil-non-mathematical uses. -- -Anderson: http://badmama.com.au/~anderson/
Jul 30 2004
prev sibling next sibling parent reply "Carlos Santander B." <carlos8294 msn.com> writes:
"Arcane Jill" <Arcane_member pathlink.com> escribió en el mensaje
news:ceamke$1bfo$1 digitaldaemon.com
| Well, now we have opPos() to complement opNeg(). We can overload the unary
plus
| operator.
|
| The obvious implementation is:
|
| #    T opPos() { return this; }
|
| (or .dup, or similar). But such trivial functions do seem a little pointless.
| What /else/ could we do with opPlus()?
|
| This is a non-serious thread, so humorous replies are welcome!
| Jill

Why is it opPos? Shouldn't it be opUnaryPlus, or even opPlus?

-----------------------
Carlos Santander Bernal
Jul 31 2004
parent reply J Anderson <REMOVEanderson badmama.com.au> writes:
Carlos Santander B. wrote:

"Arcane Jill" <Arcane_member pathlink.com> escribió en el mensaje
news:ceamke$1bfo$1 digitaldaemon.com
| Well, now we have opPos() to complement opNeg(). We can overload the unary
plus
| operator.
|
| The obvious implementation is:
|
| #    T opPos() { return this; }
|
| (or .dup, or similar). But such trivial functions do seem a little pointless.
| What /else/ could we do with opPlus()?
|
| This is a non-serious thread, so humorous replies are welcome!
| Jill

Why is it opPos? Shouldn't it be opUnaryPlus, or even opPlus?

-----------------------
Carlos Santander Bernal

  

opNeg means negative. -- -Anderson: http://badmama.com.au/~anderson/
Jul 31 2004
parent reply "Carlos Santander B." <carlos8294 msn.com> writes:
"J Anderson" <REMOVEanderson badmama.com.au> escribió en el mensaje
news:cegcuo$13du$1 digitaldaemon.com
| Carlos Santander B. wrote:
|| Why is it opPos? Shouldn't it be opUnaryPlus, or even opPlus?
||
|| -----------------------
|| Carlos Santander Bernal
||
||
||
| opPlus implies that we are adding something.  opPos means positive like
| opNeg means negative.
|
| --
| -Anderson: http://badmama.com.au/~anderson/

But as Jill already pointed: +x = x, -x = -abs(x). (mathematically speaking), so
it's not positive: it's the same

-----------------------
Carlos Santander Bernal
Jul 31 2004
next sibling parent reply James McComb <alan jamesmccomb.id.au> writes:
Carlos Santander B. wrote:

 But as Jill already pointed: +x = x, -x = -abs(x). (mathematically speaking),
so
 it's not positive: it's the same

It's not true that -x = -abs(x). If x = -5, then -x = 5, but -abs(x) = -5. WHAT AN IMPORTANT RESULT! ;) James McComb
Jul 31 2004
parent "Carlos Santander B." <carlos8294 msn.com> writes:
"James McComb" <alan jamesmccomb.id.au> escribió en el mensaje
news:cehlgr$1ju0$1 digitaldaemon.com
| It's not true that -x = -abs(x).
|
| If x = -5, then -x = 5, but -abs(x) = -5.
|
| WHAT AN IMPORTANT RESULT! ;)
|
| James McComb

(where do I hide, where do I hide????)
Yes, sorry, my mistake. But I still think opPos is not the right name, for the
same reason that +x != abs(x). But I won't argue for that anymore.
(and to think that until a couple of years ago I was a very good
mathemathician...)

-----------------------
Carlos Santander Bernal
Aug 01 2004
prev sibling parent reply Arcane Jill <Arcane_member pathlink.com> writes:
In article <cegg4j$14ng$1 digitaldaemon.com>, Carlos Santander B. says...

But as Jill already pointed: ... -x = -abs(x).

I refute that accusation most ephatically. I am not that stupid. I said: "I'd want x = -y; to assign x with minus y, not with -abs(y).". Misquotation is one thing; misquotation in a way which impunes my intelligence is quite another. By the way, regarding your recent discussions on the name opPos(), are you guys /seriously/ arguing over the name of a function whose only reasonable implementation is to do absolutely nothing whatsoever? Jill
Aug 01 2004
next sibling parent Andy Friesen <andy ikagames.com> writes:
Arcane Jill wrote:
 In article <cegg4j$14ng$1 digitaldaemon.com>, Carlos Santander B. says...
 
 
But as Jill already pointed: ... -x = -abs(x).

I refute that accusation most ephatically. I am not that stupid. I said: "I'd want x = -y; to assign x with minus y, not with -abs(y).". Misquotation is one thing; misquotation in a way which impunes my intelligence is quite another. By the way, regarding your recent discussions on the name opPos(), are you guys /seriously/ arguing over the name of a function whose only reasonable implementation is to do absolutely nothing whatsoever?

On the plus side, (pun honestly not intended) it's probably more important that we can define it at all, just so that the compiler will allow something that otherwise looks like a number to behave like one in this respect as well. -- andy
Aug 01 2004
prev sibling parent "Carlos Santander B." <carlos8294 msn.com> writes:
"Arcane Jill" <Arcane_member pathlink.com> escribió en el mensaje
news:ceiv8s$24lj$1 digitaldaemon.com
| I refute that accusation most ephatically. I am not that stupid. I said: "I'd
| want x = -y; to assign x with minus y, not with -abs(y).". Misquotation is one
| thing; misquotation in a way which impunes my intelligence is quite another.
|

I already admitted mistake. Sorry for any involving you in my lack of focus.

| By the way, regarding your recent discussions on the name opPos(), are you
guys
| /seriously/ arguing over the name of a function whose only reasonable
| implementation is to do absolutely nothing whatsoever?
|
| Jill

Yes, I gave up on that too.

-----------------------
Carlos Santander Bernal
Aug 01 2004
prev sibling parent reply parabolis <parabolis softhome.net> writes:
Arcane Jill wrote:

 Well, now we have opPos() to complement opNeg(). We can overload the unary plus
 operator.
 
 The obvious implementation is:
 
 #    T opPos() { return this; }
 
 (or .dup, or similar). But such trivial functions do seem a little pointless.
 What /else/ could we do with opPlus()? 
 
 This is a non-serious thread, so humorous replies are welcome!
 Jill
 

So what do you do with opMod() for a type that has not integer part? What is 0.95 % 0.25?
Aug 01 2004
next sibling parent reply Sean Kelly <sean f4.ca> writes:
parabolis wrote:
 
 So what do you do with opMod() for a type that has not integer part?
 
 What is 0.95 % 0.25?

Zero. Or perhaps the margin of error for floating point on type double. Sean
Aug 01 2004
parent parabolis <parabolis softhome.net> writes:
Sean Kelly wrote:

 parabolis wrote:
 
 So what do you do with opMod() for a type that has not integer part?

 What is 0.95 % 0.25?

Zero. Or perhaps the margin of error for floating point on type double. Sean

Double is zero, but then double can also represent integers: 13.0 % 5.0 == 3.0...
Aug 01 2004
prev sibling next sibling parent reply Arcane Jill <Arcane_member pathlink.com> writes:
In article <cejim8$2c3n$1 digitaldaemon.com>, parabolis says...


So what do you do with opMod() for a type that has not integer part?

I'd implement fmod().
What is 0.95 % 0.25?

0.2 (since 0.95 = 3 * 0.25 + 0.2) Arcane Jill
Aug 01 2004
next sibling parent parabolis <parabolis softhome.net> writes:
Arcane Jill wrote:

What is 0.95 % 0.25?

0.2 (since 0.95 = 3 * 0.25 + 0.2)

Yes I suppose you have a point.
Aug 01 2004
prev sibling parent h3r3tic <h3r3tic dev.null> writes:
Arcane Jill wrote:
 In article <cejim8$2c3n$1 digitaldaemon.com>, parabolis says...
So what do you do with opMod() for a type that has not integer part?


No way. fmod is already implemented -> www.fmod.org
Aug 01 2004
prev sibling parent reply Nick <Nick_member pathlink.com> writes:
In article <cejim8$2c3n$1 digitaldaemon.com>, parabolis says...
So what do you do with opMod() for a type that has not integer part?

What is 0.95 % 0.25?

It's 0.2, according to dmd ;-) It's defined as a % b = a - floor(a/b) * b It makes sense when you think about it. Nick
Aug 01 2004
parent Sean Kelly <sean f4.ca> writes:
Nick wrote:

 In article <cejim8$2c3n$1 digitaldaemon.com>, parabolis says...
 
So what do you do with opMod() for a type that has not integer part?

What is 0.95 % 0.25?

It's 0.2, according to dmd ;-) It's defined as a % b = a - floor(a/b) * b It makes sense when you think about it.

Oops, yeah it does. Very nice. Sean
Aug 01 2004