On 1/9/11 1:12 PM, Daniel Gibson wrote:
Am 09.01.2011 19:48, schrieb Robert M. Münch:
On 2011-01-09 19:42:22 +0100, Andrei Alexandrescu said:

I wrote a simple helper, in spirit with some recent discussions:

Hi, EITHER normaly means: This or that, so it's more like an IF/ELSE.
How about ANY? IMO

1==any(1, 2, 3)

describes pretty good what is meant.


I agree.

Problem with any/either is that it encodes the comparison inside the data. You can do == and !=, but algorithms want to work with general predicates, and any can't work with arbitrary predicates - unless the predicates themselves are designed to work with it.

For now I added this to std.algorithm:

/**
Consume all elements from $(D r) that are equal to one of the elements
$(D es).
 */
void skipAll(alias pred = "a == b", R, Es...)(ref R r, Es es)
{
  loop:
    for (; !r.empty; r.popFront())
    {
        foreach (i, E; Es)
        {
            if (binaryFun!pred(r.front, es[i]))
            {
                continue loop;
            }
        }
        break;
    }
}

unittest
{
    auto s1 = "Hello world";
    skipAll(s1, 'H', 'e');
    assert(s1 == "llo world");
}

I wonder if it's worthwhile encoding the predicate inside the compared elements. In that case, any would return a type that also has the predicate. Something like this:

struct Any(alias pred = "a == b", Es...) { ... }

Then you'd put the predicate by the call to any, not by the call to the algorithm:

skipAll(r, any!"a > b"(x, y, z));

Then skipAll doesn't need to worry about the predicate - it just uses == and !=.

If this paradigm has a landslide advantage over the current structure, we'd need to rewrite a lot of std.algorithm.


Andrei

Reply via email to