How about a Hash template?
Andrei Alexandrescu
SeeWebsiteForEmail at erdani.org
Fri Apr 29 12:12:46 PDT 2011
On 4/29/11 8:50 AM, Denis Koroskin wrote:
> On Thu, 28 Apr 2011 03:10:15 +0400, Andrej Mitrovic
> <andrej.mitrovich at gmail.com> wrote:
>
>> I often see code written like this:
>>
>> if (value == somevalue ||
>> value == someothervalue ||
>> value == yetanothervalue);
>>
>> You could use the switch statement. But that introduces indentation,
>> and is rarely used for a couple of values.
>>
>> I really like the "in" keyword, and I also like hashes since they can
>> potentially speed up look-ups compared to conventional arrays. So I
>> thought it would be cool to have a Hash template that constructs an
>> associative array which you can use especially in if statements when
>> you just want to know if a runtime value matches some predetermined
>> value.
>>
>> Here's my attempt at writing it:
>> http://codepad.org/c4sYDSyR
>>
>> Whaddya think?
>
> Try this:
[snip]
Nice work. I think this code could earn a place in Phobos:
auto either(Keys...)(Keys keys)
{
static struct Result
{
bool opIn_r(Key)(Key key)
{
foreach (k; keys) {
if (k == key) {
return true;
}
}
return false;
}
private Keys keys;
}
return Result(keys);
}
Andrei
More information about the Digitalmars-d
mailing list