On Thursday, 15 January 2015 at 13:01:50 UTC, Nordlöw wrote:
I want a variant of commonPrefix(a, b) at

    http://dlang.org/phobos/std_algorithm.html#commonPrefix

that only counts returns the length of commonPrefix(a, b)

Is commonPrefix lazy enough to make

    commonPrefix(a, b).count

as fast as

    zip(a, b).count!(ab => ab[0] == ab[1])

?

I just discovered that zip has StoppingPolicy so why does

auto commonPrefixLength(R...)(R ranges) if (ranges.length == 2)
{
    import std.range: zip;
    return zip!((a, b) => a[0] != b[1])(ranges);
}

unittest
{
    assert(commonPrefixLength([1, 2, 3, 10],
                              [1, 2, 4, 10]) == 2);
}

error as

algorithm_ex.d(1709,40): Error: template std.range.zip cannot deduce function from argument types !((a, b) => a[0] != b[1])(int[], int[]), candidates are: std/range/package.d(3247,6): std.range.zip(Ranges...)(Ranges ranges) if (Ranges.length && allSatisfy!(isInputRange, Ranges)) std/range/package.d(3265,6): std.range.zip(Ranges...)(StoppingPolicy sp, Ranges ranges) if (Ranges.length && allSatisfy!(isInputRange, Ranges)) algorithm_ex.d(1714,30): Error: template instance algorithm_ex.commonPrefixLength!(int[], int[]) error instantiating

Reply via email to