Andrei Alexandrescu napisał:

> I wrote a simple helper, in spirit with some recent discussions:
> 
> // either
> struct Either(Ts...)
> {
>      Tuple!Ts data_;
>      bool opEquals(E)(E e)
>      {
>          foreach (i, T; Ts)
>          {
>              if (data_[i] == e) return true;
>          }
>          return false;
>      }
> }
> 
> auto either(Ts...)(Ts args)
> {
>      return Either!Ts(tuple(args));
> }
> 
> unittest
> {
>      assert(1 == either(1, 2, 3));
>      assert(4 != either(1, 2, 3));
>      assert("abac" != either("aasd", "s"));
>      assert("abac" == either("aasd", "abac", "s"));
> }

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().

-- 
Tomek

Reply via email to