commonLength

"Nordlöw" via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Jan 15 10:23:23 PST 2015


On Thursday, 15 January 2015 at 18:16:36 UTC, Nordlöw wrote:
> On Thursday, 15 January 2015 at 13:59:04 UTC, Tobias Pankrath 
> wrote:
>> StoppingPolicy is not a template parameter, it is this one: 
>> http://dlang.org/phobos/std_range.html#.StoppingPolicy
>
> Ok, great!
>
> However, none of the variants of zip, including all the three 
> policies, fulfills my needs in
>
>     assert(commonPrefixLength([1, 2, 3, 10],
>                               [1, 2, 3]), 3);
>
> which fails because
>
>      commonPrefixLength([1, 2, 3, 10],
>                         [1, 2, 3])
>
> evaluates to -1.
>
> This seems like a serious limitation that should be realized in 
> yet another policy.

In the mean while how should I generalize

auto commonPrefixLength(S, T)(S a, T b)
{
     import std.range: zip;
     import std.algorithm: countUntil;
     return zip(a, b).countUntil!(ab => ab[0] != ab[1]);
}

to

- work as expected with the unittest above
- work with 3 or more arguments like zip do. How do check that 
all elements of a tuple are equal?

I believe this is an important issue because the function

     commonPrefixLength

together with sort can be used to implement

- completions (in widgets and intellisense)
- find rhymes (retro + commonPrefixLength + sort)


More information about the Digitalmars-d-learn mailing list