On 1/9/11 3:13 PM, Tomek Sowiński wrote:
Andrei Alexandrescu napisał:

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

Yes.

This looks promising and well integrated with the rest of Phobos. Should
I add it?

Please do. Oh, thinking about it 10 more minutes, an improvement struck me:

bool any(alias pred = "a==b", E, Ts...)(E e, Ts args) if(Ts.length>  1 || 
!isTuple!Ts) {
      foreach (a; args)
          if (binaryFun!pred(a, e))
             return true;
      return false;
}

unittest
{
      assert(!"abac".any("s"));
      assert(!"abac".any("aasd", "s"));
      assert("abac".any("aasd", "abac", "s"));
}

/// Unpacks a single tuple.
bool any(alias pred = "a==b", E, T)(E e, T t) if(isTuple!T) {
     return any!(pred, E, T.Types)(e, t.field);
}

unittest
{
      assert(any("abac", tuple("aasd", "abac", "s")));
      assert(find!any([5,3,7,9,4,5,7], tuple(2,4,6)) == [4,5,7]);    // cool!
}

Hm, I don't see why not asking the owner of the tuple to type .expand.

And if I do, is "any" the name?

For a start. We can always vote it to sth more descriptive e.g. 'isAny' in case 
'any' turns out misleading.

At best we'd minimize such jitter.


Andrei

Reply via email to