Thread overview
RFE: Trinary comparison operators
Oct 14, 2004
Alex Vincent
Oct 14, 2004
Derek Parnell
Oct 14, 2004
Stewart Gordon
Oct 14, 2004
Derek
Oct 14, 2004
Sjoerd van Leent
Oct 14, 2004
Martin
Oct 14, 2004
Stewart Gordon
Oct 14, 2004
Sebastian Beschke
Oct 15, 2004
Niall FitzGibbon
October 14, 2004
As a programmer, one of the tests I end up doing more often than I want to is:

if ((a < b) && (b < c)) { /* ... */ }

It would be exceptionally nice if D would support an abbreviation of that, similar to what we learned in mathematics classes:

if (a < b < c) { /* ... */ }

Likewise, I'd want to do:

if (a <= b < c) { /* ... */ }
if (a < b <= c) { /* ... */ }
if (a <= b <= c) { /* ... */ }

I would not particularly care to see this sort of functionality implemented in other operators (for instance, a > b > c).  This simple test, to check that b is between two values, is fairly straightforward.  I'm just wondering if D can add support for this specific type of operation.
October 14, 2004
On Wed, 13 Oct 2004 20:25:29 -0700, Alex Vincent wrote:

> As a programmer, one of the tests I end up doing more often than I want to is:
> 
> if ((a < b) && (b < c)) { /* ... */ }
> 
> It would be exceptionally nice if D would support an abbreviation of that, similar to what we learned in mathematics classes:
> 
> if (a < b < c) { /* ... */ }
> 
> Likewise, I'd want to do:
> 
> if (a <= b < c) { /* ... */ }
> if (a < b <= c) { /* ... */ }
> if (a <= b <= c) { /* ... */ }
> 
> I would not particularly care to see this sort of functionality
> implemented in other operators (for instance, a > b > c).  This simple
> test, to check that b is between two values, is fairly straightforward.
>   I'm just wondering if D can add support for this specific type of
> operation.

Could you do ...

  if ( a < b ? b < c : 0 ) { /* ... */ }

-- 
Derek
Melbourne, Australia
14/10/2004 2:26:12 PM
October 14, 2004
Derek Parnell wrote:
<snip>
> Could you do ...
> 
>   if ( a < b ? b < c : 0 ) { /* ... */ }

What would that achieve over

    if ((a < b) && (b < c)) { /* ... */ }

?

Stewart.
October 14, 2004
I am in for it. Good idea.
I think that right now a<b<c   means (a<b)<c what  means (c>0 && a>=b) || (c>1
&& a<b)
so a<b<c = (a<b)<c
but it is used in very rare cases, so why not
a<b<c =  a<b && b<c

so checking if a is in 6..10 would be
if(6<=a<10)
instead of
if(a>=7 && a<10)

Easier to read and code.

D is for making programming easier and faster, I think this change serves this goal.

Martin




In article <ckkrk3$1agd$1@digitaldaemon.com>, Alex Vincent says...
>
>As a programmer, one of the tests I end up doing more often than I want to is:
>
>if ((a < b) && (b < c)) { /* ... */ }
>
>It would be exceptionally nice if D would support an abbreviation of that, similar to what we learned in mathematics classes:
>
>if (a < b < c) { /* ... */ }
>
>Likewise, I'd want to do:
>
>if (a <= b < c) { /* ... */ }
>if (a < b <= c) { /* ... */ }
>if (a <= b <= c) { /* ... */ }
>
>I would not particularly care to see this sort of functionality implemented in other operators (for instance, a > b > c).  This simple test, to check that b is between two values, is fairly straightforward.
>  I'm just wondering if D can add support for this specific type of
>operation.


October 14, 2004
Alex Vincent wrote:
> As a programmer, one of the tests I end up doing more often than I want to is:
> 
> if ((a < b) && (b < c)) { /* ... */ }

I'm for it only if it wouldn't make lexing and parsing harder. I'd rather type a few more characters than have a slow compiler.

Sebastian
October 14, 2004
Martin wrote:

> I am in for it. Good idea.
> I think that right now a<b<c   means (a<b)<c what  means (c>0 && a>=b) || (c>1
> && a<b)
<snip>

Actually, it means (a < b) ? (0 < c) : (1 < c)

They look equivalent, but they're not.  (a < b) < c evaluates each of a, b and c exactly once regardless of the outcome.  Your interpretation evaluates the subexpressions different numbers of times:

(c>0 && a>=b) || ( c>1 && a<b)	eval a	eval b	eval c
  F  F	 T    F     F  F   F	0	0	2
  T  T	 T    T     F  F   F	1	1	1
  T  T	 T    T     T  F   F	1	1	1
  F  F	 F    F     F  F   T	0	0	2
  T  F	 F    F     F  F   T	1	1	2
  T  F	 F    T     T  T   T	2	2	2

Stewart.
October 14, 2004
Alex Vincent wrote:

> It would be exceptionally nice if D would support an abbreviation of that, similar to what we learned in mathematics classes:
> 
> if (a < b < c) { /* ... */ }

I thought you meant the "starship operator" <=> that Perl has.

It returns -1, 0, or +1 depending on if they are <, == or >

Handy for writing sorting functions ?

--anders
October 14, 2004
On Thu, 14 Oct 2004 11:24:37 +0100, Stewart Gordon wrote:

> Derek Parnell wrote:
> <snip>
>> Could you do ...
>> 
>>   if ( a < b ? b < c : 0 ) { /* ... */ }
> 
> What would that achieve over
> 
>      if ((a < b) && (b < c)) { /* ... */ }
> 
> ?
> 
> Stewart.

Well I thought *that* would've been obvious - 4 keystrokes! Plus it looks more obscure so it must have been coded by a real guru ;-)

I later sat back and tried to use a mixin to implement this but I quickly ran into problems. There is something deep about mixins that I don't grok yet :-(

-- 
Derek
Melbourne, Australia
October 14, 2004
Derek wrote:
> On Thu, 14 Oct 2004 11:24:37 +0100, Stewart Gordon wrote:
> 
> 
>>Derek Parnell wrote:
>><snip>
>>
>>>Could you do ...
>>>
>>>  if ( a < b ? b < c : 0 ) { /* ... */ }
>>
>>What would that achieve over
>>
>>     if ((a < b) && (b < c)) { /* ... */ }
>>
>>?
>>
>>Stewart.
> 
> 
> Well I thought *that* would've been obvious - 4 keystrokes! Plus it looks
> more obscure so it must have been coded by a real guru ;-)
> 
> I later sat back and tried to use a mixin to implement this but I quickly
> ran into problems. There is something deep about mixins that I don't grok
> yet :-(
> 

How would you like to define a trinary operator. Since the BCL notation says that operator "<" always have to return a bit value (1 or 0). If you write something like the following:

a < b < c

It would legally evaluate to  a < b (which returns a 1 or 0) and then b < c (which goes wrong all the way).

b : c <=> a

This would be easier to implement I think. It uses an operator that is currently not defined and, in pseudo code says:

is b lesser than c and higher than a?

Also other options could be done:

b : a >=< b
b : b <> a
b : a >< b

This would solve the problem I think...

Regards,
Sjoerd
October 15, 2004
Alex Vincent wrote:
> As a programmer, one of the tests I end up doing more often than I want to is:
> 
> if ((a < b) && (b < c)) { /* ... */ }

Instead of creating ambiguity for the < and > operators, how about a new set of "bounding" operators that behaves similarly to the ?: operators?

This is an operation that graphical applications have to perform many times per frame for clipping, and I think it would be good to have it simplified to the form of a basic operator.

How about something like:
b#[a,c] for being inclusive on both bounds
b#(a,c) for being exclusive on both bounds
b#[a,c) for being inclusive on the lower bound
b#(a,c] for being inclusive on the upper bound

Maybe this would be too ambiguous for the parser though?