handful and interval

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Mon Sep 3 15:44:14 PDT 2012


On 9/3/12 9:15 PM, Timon Gehr wrote:
> This would eventually lead to a solution like
>
> bool among(S,T...)(S needle, auto enum T haystack){
> ...
> foreach(h;haystack) static if(__traits(isConstant, h)){ ... }
> ...
> }
>
> Which is still rather specific.
>
> Anyway, I don't consider among trying to be clever crucial at all.

I'd be happy with:

uint among(S, T...)(S needle, T haystack)
{
     foreach (i, straw; haystack)
     {
         if (needle == straw) return i + 1;
     }
     return 0;
}

The only issue is that all straws in the haystack are evaluated eagerly, 
even if not needed. I think it's okay to live with that.


Andrei


More information about the Digitalmars-d mailing list