drop* and take* only for specific element values

"Nordlöw" via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Aug 14 00:30:57 PDT 2014


On Thursday, 14 August 2014 at 00:56:47 UTC, Jonathan M Davis 
wrote:
> You forgot the !, making the predicate a function argument. It

Great!

My solution:

auto dropWhile(R, E)(R range, E element) if (isInputRange!R &&
                                              is(ElementType!R == 
E))
{
     import std.algorithm: find;
     return range.find!(a => a != element);
}

unittest
{
     assert([1, 2, 3].dropWhile(1) == [2, 3]);
     assert([1, 1, 1, 2, 3].dropWhile(1) == [2, 3]);
     assert([1, 2, 3].dropWhile(2) == [1, 2, 3]);
     assert("abc".dropWhile(cast(dchar)'a') == "bc");
}


More information about the Digitalmars-d-learn mailing list