Jump to page: 1 2 3
Thread overview
Comparing Multiple Values
Mar 11, 2008
okibi
Mar 11, 2008
oliver
Mar 11, 2008
Bill Baxter
Mar 11, 2008
downs
Mar 11, 2008
Derek Parnell
Mar 11, 2008
BCS
Mar 11, 2008
Bill Baxter
Mar 12, 2008
BCS
Mar 11, 2008
Bill Baxter
Mar 11, 2008
downs
Mar 11, 2008
Bill Baxter
Mar 11, 2008
downs
Mar 11, 2008
BCS
Mar 11, 2008
Bill Baxter
Re: Comparing Multiple Values (the story so far/summary)
Mar 11, 2008
downs
Mar 11, 2008
Bill Baxter
Mar 12, 2008
okibi
Mar 11, 2008
Bill Baxter
Mar 11, 2008
Frank Benoit
Mar 11, 2008
okibi
Mar 11, 2008
Bill Baxter
Mar 11, 2008
Derek Parnell
Mar 11, 2008
Alexander Panek
March 11, 2008
I've looked all over the site but can't seem to find if D supports any kind of like IN statement for comparisons. For example, I can do the following in SQL:

select *
from table
where value in (1,2,3)

And it will compare it to 1, 2, and 3. Is this possible to do within D's if statement? I hate to go about it as such:

if (value == 1 || value == 2 || value == 3)
    dosomething();

Just seems like this could be written better. Can anyone give me any pointers?

Thanks!
March 11, 2008
okibi Wrote:

> I've looked all over the site but can't seem to find if D supports any kind of like IN statement for comparisons. For example, I can do the following in SQL:
> 
> select *
> from table
> where value in (1,2,3)
> 
> And it will compare it to 1, 2, and 3. Is this possible to do within D's if statement? I hate to go about it as such:
> 
> if (value == 1 || value == 2 || value == 3)
>     dosomething();
> 
> Just seems like this could be written better. Can anyone give me any pointers?
> 
> Thanks!

Probably not quite what you are looking for but you could use AAs.

Something like:

int foo[char[]];
...
if ("hello" in foo)

Oliver
March 11, 2008
okibi wrote:
> I've looked all over the site but can't seem to find if D supports any kind of like IN statement for comparisons. For example, I can do the following in SQL:
> 
> select *
> from table
> where value in (1,2,3)
> 
> And it will compare it to 1, 2, and 3. Is this possible to do within D's if statement? I hate to go about it as such:
> 
> if (value == 1 || value == 2 || value == 3)
>     dosomething();
> 
> Just seems like this could be written better. Can anyone give me any pointers?

I can point you to a bunch of discussions where certain people argued tooth and nail that  "if(value in [1,2,3])" should mean "if(value==0||value==1||value==2)", leading basically to a stalemate. So, no.  Nothing like that is in the language.

But you can write a little "contains" function that will do the trick.

Or ask Downs how to make "if(x /In/ [1,2,3])" work.

--bb
March 11, 2008
okibi schrieb:
> I've looked all over the site but can't seem to find if D supports any kind of like IN statement for comparisons. For example, I can do the following in SQL:
> 
> select *
> from table
> where value in (1,2,3)
> 
> And it will compare it to 1, 2, and 3. Is this possible to do within D's if statement? I hate to go about it as such:
> 
> if (value == 1 || value == 2 || value == 3)
>     dosomething();
> 
> Just seems like this could be written better. Can anyone give me any pointers?
> 
> Thanks!

*untested*

bool isOneOf(T)( T value, T[] compares ... ){
	foreach( c; compares ){
		if( value == c ) return true;
	}
	return false;
}
if ( isOneOf( value, 1, 2, 3 ))
    dosomething();
March 11, 2008
On Tue, 11 Mar 2008 06:45:14 -0400, okibi wrote:

> I've looked all over the site but can't seem to find if D supports any kind of like IN statement for comparisons. For example, I can do the following in SQL:
> 
> select *
> from table
> where value in (1,2,3)
> 
> And it will compare it to 1, 2, and 3. Is this possible to do within D's if statement? I hate to go about it as such:
> 
> if (value == 1 || value == 2 || value == 3)
>     dosomething();
> 
> Just seems like this could be written better. Can anyone give me any pointers?

Without thinking too deeply about it, you could use this technique ...

bool[int] goodvalues;
static this()
{
    goodvalues = [1:true, 2:true, 3:true];
}
.
.
.
   if (value in goodvalues)
      dosomething();


-- 
Derek Parnell
Melbourne, Australia
skype: derek.j.parnell
March 11, 2008
Frank Benoit Wrote:

> okibi schrieb:
> > I've looked all over the site but can't seem to find if D supports any kind of like IN statement for comparisons. For example, I can do the following in SQL:
> > 
> > select *
> > from table
> > where value in (1,2,3)
> > 
> > And it will compare it to 1, 2, and 3. Is this possible to do within D's if statement? I hate to go about it as such:
> > 
> > if (value == 1 || value == 2 || value == 3)
> >     dosomething();
> > 
> > Just seems like this could be written better. Can anyone give me any pointers?
> > 
> > Thanks!
> 
> *untested*
> 
> bool isOneOf(T)( T value, T[] compares ... ){
> 	foreach( c; compares ){
> 		if( value == c ) return true;
> 	}
> 	return false;
> }
> if ( isOneOf( value, 1, 2, 3 ))
>      dosomething();

This is what I was looking for, thanks!

Is there any chance of the in [] method getting into D or is it a definite no?
March 11, 2008
okibi wrote:
> Frank Benoit Wrote:
> 
>> okibi schrieb:
>>> I've looked all over the site but can't seem to find if D supports any kind of like IN statement for comparisons. For example, I can do the following in SQL:
>>>
>>> select *
>>> from table
>>> where value in (1,2,3)
>>>
>>> And it will compare it to 1, 2, and 3. Is this possible to do within D's if statement? I hate to go about it as such:
>>>
>>> if (value == 1 || value == 2 || value == 3)
>>>     dosomething();
>>>
>>> Just seems like this could be written better. Can anyone give me any pointers?
>>>
>>> Thanks!
>> *untested*
>>
>> bool isOneOf(T)( T value, T[] compares ... ){
>> 	foreach( c; compares ){
>> 		if( value == c ) return true;
>> 	}
>> 	return false;
>> }
>> if ( isOneOf( value, 1, 2, 3 ))
>>      dosomething();
> 
> This is what I was looking for, thanks!
> 
> Is there any chance of the in [] method getting into D or is it a definite no?

Probably not much chance, since Walter is one of the ones who thinks A in [x,y,z] should return true if A is 0 1 or 2, regardless of the values of x,y, and z.

See big long argument here:
  http://d.puremagic.com/issues/show_bug.cgi?id=1323

But Walter has been known to change his mind on occasion.

--bb
March 11, 2008
Bill Baxter wrote:
> okibi wrote:
>> I've looked all over the site but can't seem to find if D supports any kind of like IN statement for comparisons. For example, I can do the following in SQL:
>>
>> select *
>> from table
>> where value in (1,2,3)
>>
>> And it will compare it to 1, 2, and 3. Is this possible to do within D's if statement? I hate to go about it as such:
>>
>> if (value == 1 || value == 2 || value == 3)
>>     dosomething();
>>
>> Just seems like this could be written better. Can anyone give me any pointers?
> 
> I can point you to a bunch of discussions where certain people argued tooth and nail that  "if(value in [1,2,3])" should mean "if(value==0||value==1||value==2)", leading basically to a stalemate. So, no.  Nothing like that is in the language.
> 
> But you can write a little "contains" function that will do the trick.
> 
> Or ask Downs how to make "if(x /In/ [1,2,3])" work.
> 
> --bb

There's a better way actually.

import std.stdio;

// bottom-inclusive, top-exclusive, like slices.
struct _Range(T) {
  T from, to;
  bool opIn_r(U)(U u) {
    return u < to && u !< from;
  }
}

struct Range {
  static _Range!(T) opSlice(T, U)(T from, U to) {
    return _Range!(T)(from, to);
  }
}

void main() {
  writefln(3 in Range[2..4], " -- ", 4 in Range[2..4]);
}

Have funs!

 --downs
March 11, 2008
On Tue, 11 Mar 2008 13:39:55 +0100, downs wrote:
> There's a better way actually.
But that is only good for ranges ;-)

Not so good for ...

  if value in set(2,4,7,10,23,445)

I'm pretty sure this will be simple to organize once AST macros are implemented.

-- 
Derek Parnell
Melbourne, Australia
skype: derek.j.parnell
March 11, 2008
Reply to Derek,

> On Tue, 11 Mar 2008 13:39:55 +0100, downs wrote:
> 
>> There's a better way actually.
>> 
> But that is only good for ranges ;-)
> 
> Not so good for ...
> 
> if value in set(2,4,7,10,23,445)
> 
> I'm pretty sure this will be simple to organize once AST macros are
> implemented.
> 

struct _Set(T) {
 T[] set;
 bool opIn_r(U)(U u) {
   foreach(v; set) if(v==u) return true;
   return false;
 }
}

struct Set {
 static _Set!(T) opIndex(T, U)(T[] a...) {
   _Set!(T) ret;
   ret.set = a.dup; //need the dup???
   return ret;
 }
}

I /think/ that will work. But I haven't tested it.

if(1 in Set[1,2,3,5,9,23])


« First   ‹ Prev
1 2 3