Jump to page: 1 2
Thread overview
'in' for arrays?
Dec 06, 2006
Bill Baxter
Dec 06, 2006
Lionello Lunesu
Dec 06, 2006
Egor Starostin
Dec 06, 2006
Bill Baxter
Dec 06, 2006
Lionello Lunesu
Dec 06, 2006
Alexander Panek
Dec 06, 2006
Lionello Lunesu
Dec 06, 2006
Alexander Panek
Dec 06, 2006
Hasan Aljudy
Dec 07, 2006
Alexander Panek
Dec 06, 2006
Alexander Panek
Dec 07, 2006
Pragma
Dec 06, 2006
Lionello Lunesu
Dec 06, 2006
Chad J
Dec 06, 2006
Bill Baxter
Dec 07, 2006
Stewart Gordon
Dec 07, 2006
Derek Parnell
Dec 08, 2006
Bill Baxter
December 06, 2006
I keep wanting to say
  if (val in somearray) {
  }

but 'in' doesn't work for arrays...  Is there any reason why not?
Basically I just expect it to do something like this function (untested, modified from a similar function in Cashew):

T* item_in (T) (T[] haystack, T cmp)
{
  foreach (index, inout needle; haystack) {
    if (needle==cmp) {
      return &needle;
    }
  }
  return null;
}

I.e. I just expect it to do a plain old linear search through the list, nothing fancy, but it would be handy.

--bb
December 06, 2006
Bill Baxter wrote:
> I keep wanting to say
>   if (val in somearray) {
>   }

It makes sense to make "in" behave as if an int[] is a int[int], but the problem is that "in" for normal arrays is O(n), slow. If you need to search the array, you probably should have been using another container in the first place.

My philosophy is always that CPU-expensive actions should also appear as such in code. A small "in" that actual does a linear search might seem a like a good idea, but people should be discouraged from using it.

L.
December 06, 2006
> I keep wanting to say
>    if (val in somearray) {
>    }
Me too.

For example, in Python I can write

if val in ('off','disable','no')

and in D I have to write

if (val == "off" || val == "disable" || val == "no")

which is less readable (to me, at least).


--
Egor
December 06, 2006
Egor Starostin wrote:
>> I keep wanting to say
>>    if (val in somearray) {
>>    }
> Me too.
> 
> For example, in Python I can write
> 
> if val in ('off','disable','no')
> 
> and in D I have to write
> 
> if (val == "off" || val == "disable" || val == "no")
> 
> which is less readable (to me, at least).
> 
> 
> --
> Egor

Yep.  That's the kind of usage case I have in mind.

About "it's expensive but it doesn't look expensive"...
Expensive is all relative.  If you tend to have lists with tens or even hundreds of elements in them then a linear search is a perfectly reasonable way to do it.  And in terms of "looks inexpensive", well to me   (val in somearray) looks exactly like it's going to do a linear search.  What else *could* it do when handed a general array?

--bb
December 06, 2006
Egor Starostin wrote:
>> I keep wanting to say
>>    if (val in somearray) {
>>    }
> Me too.
> 
> For example, in Python I can write
> 
> if val in ('off','disable','no')

OK, granted, if (val in ["off","disable","no"]) does look cool, but a switch(val) would still be faster (uses binary search if I'm not mistaken).

Maybe D should get an alternative "if" for this construct:

switch(val) {
  case "off","disable","no":
     // code here
  default:
     break;
}

Anyway, supporting "in" for normal arrays has my vote. In my post I just mentioned a possible concern.

L.
December 06, 2006
Lionello Lunesu wrote:
> Egor Starostin wrote:
>>> I keep wanting to say
>>>    if (val in somearray) {
>>>    }
>> Me too.
>>
>> For example, in Python I can write
>>
>> if val in ('off','disable','no')
> 
> OK, granted, if (val in ["off","disable","no"]) does look cool, but a switch(val) would still be faster (uses binary search if I'm not mistaken).
> 
> Maybe D should get an alternative "if" for this construct:
> 
> switch(val) {
>   case "off","disable","no":
>      // code here
>   default:
>      break;
> }
May I ask in what way this is different from:

switch ( val ) {
    case "off":
    case "disable":
    case "no":
        // code here
        break;
}

?

> 
> Anyway, supporting "in" for normal arrays has my vote. In my post I just mentioned a possible concern.
> 
> L.

Kind regards,
Alex
December 06, 2006
Alexander Panek wrote:
> Lionello Lunesu wrote:
>> switch(val) {
>>   case "off","disable","no":
>>      // code here
>>   default:
>>      break;
>> }
> May I ask in what way this is different from:
> 
> switch ( val ) {
>     case "off":
>     case "disable":
>     case "no":
>         // code here
>         break;
> }

case x,y: is the same as case x: case y:
Apart from that, if you don't provide a 'default' statement, D inserts one with assert(0), meaning that your code would trip if val were anything other than those 3 mentioned cases.

L.
December 06, 2006
Lionello Lunesu wrote:
> Alexander Panek wrote:
>> Lionello Lunesu wrote:
>>> switch(val) {
>>>   case "off","disable","no":
>>>      // code here
>>>   default:
>>>      break;
>>> }
>> May I ask in what way this is different from:
>>
>> switch ( val ) {
>>     case "off":
>>     case "disable":
>>     case "no":
>>         // code here
>>         break;
>> }
> 
> case x,y: is the same as case x: case y:
> Apart from that, if you don't provide a 'default' statement, D inserts one with assert(0), meaning that your code would trip if val were anything other than those 3 mentioned cases.
> 
> L.

Yea..I've seen that I forgot the default: break; after replying.. :P
December 06, 2006
"Egor Starostin" <egorst@gmail.com> wrote in message news:el5vpc$9nt$1@digitaldaemon.com...
> Me too.
>
> For example, in Python I can write
>
> if val in ('off','disable','no')
>
> and in D I have to write
>
> if (val == "off" || val == "disable" || val == "no")
>
> which is less readable (to me, at least).

struct _Tuple(T...)
{
 bool opIn_r(U)(U value)
 {
  foreach(val; T)
  {
   static if(is(typeof(val) : U))
   {
    if(val == value)
     return true;
   }
  }

  return false;
 }
}

template Tuple(T...)
{
 const _Tuple!(T) Tuple;
}

void main()
{
 if(4 in Tuple!("hi", 5.5, 4))
  writefln("yep");
 else
  writefln("nope");

 if("no" in Tuple!("hi", 5.5, 4))
  writefln("yep");
 else
  writefln("nope");
}


Meh.  :)


December 06, 2006
Ha! Very nice.

I start to like those mysterious Tuples. ;)

Jarrett Billingsley wrote:
> "Egor Starostin" <egorst@gmail.com> wrote in message news:el5vpc$9nt$1@digitaldaemon.com...
>> Me too.
>>
>> For example, in Python I can write
>>
>> if val in ('off','disable','no')
>>
>> and in D I have to write
>>
>> if (val == "off" || val == "disable" || val == "no")
>>
>> which is less readable (to me, at least).
> 
> struct _Tuple(T...)
> {
>  bool opIn_r(U)(U value)
>  {
>   foreach(val; T)
>   {
>    static if(is(typeof(val) : U))
>    {
>     if(val == value)
>      return true;
>    }
>   }
> 
>   return false;
>  }
> }
> 
> template Tuple(T...)
> {
>  const _Tuple!(T) Tuple;
> }
> 
> void main()
> {
>  if(4 in Tuple!("hi", 5.5, 4))
>   writefln("yep");
>  else
>   writefln("nope");
> 
>  if("no" in Tuple!("hi", 5.5, 4))
>   writefln("yep");
>  else
>   writefln("nope");
> }
> 
> 
> Meh.  :) 
> 
> 
« First   ‹ Prev
1 2