either
Andrei Alexandrescu
SeeWebsiteForEmail at erdani.org
Sun Jan 9 12:28:23 PST 2011
On 1/9/11 2:09 PM, Tomek Sowiński wrote:
> I really don't dig the whole helper structs with overloaded operators thing. It complicates the implementation (more work for compiler to grok and inline) and you're never really sure what it does unless you read the docs (or the complicated implementation).
>
> It should be as simple as this:
>
> bool any(E, Ts...)(E e, Ts args) {
> foreach (a; args)
> if (a == e)
> return true;
> return false;
> }
>
> unittest
> {
> assert(!"abac".any("aasd", "s"));
> assert("abac".any("aasd", "abac", "s"));
> // assert(1.any(1,2,3)); // doesn't compile for now, bug 3382
> }
>
>> Turns out this is very useful in a variety of algorithms.
>
> Very!
>
>> I just don't
>> know where in std this helper belongs! Any ideas?
>
> Maybe std.algorithm? It bears vague resemblance to max().
Aha, so this encodes the predicate in the operation. With a general
predicate, that would be:
if (any!"a != b"(expr, 1, 2, 5)) { ... }
The advantage over
if (expr != 1 || expr != 2 || expr != 5)) { ... }
is terseness and the guarantee that expr is evaluated once (which is
nice at least for my code).
This looks promising and well integrated with the rest of Phobos. Should
I add it? And if I do, is "any" the name?
Andrei
More information about the Digitalmars-d
mailing list