drop* and take* only for specific element values

"Nordlöw" via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Aug 13 14:22:30 PDT 2014


On Wednesday, 13 August 2014 at 14:45:43 UTC, Jonathan M Davis 
via Digitalmars-d-learn wrote:
> They're called find and until. You just have to give them the 
> opposite
> predicate that you'd give a function called dropIf or takeIf.

Ok, thx.

As an exercise I tried

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 { dln([1, 2, 3].dropWhile(1)); }

but it errors as

algorithm_ex.d(1452,22): Error: template std.algorithm.find 
cannot deduce function from argument types !()(int[], void), 
candidates are:
/home/per/opt/x86_64-unknown-linux-gnu/dmd/linux/bin64/src/phobos/std/algorithm.d(4261,12): 
        std.algorithm.find(alias pred = "a == b", InputRange, 
Element)(InputRange haystack, Element needle) if 
(isInputRange!InputRange && 
is(typeof(binaryFun!pred(haystack.front, needle)) : bool))
/home/per/opt/x86_64-unknown-linux-gnu/dmd/linux/bin64/src/phobos/std/algorithm.d(4541,4): 
        std.algorithm.find(alias pred = "a == b", R1, R2)(R1 
haystack, R2 needle) if (isForwardRange!R1 && isForwardRange!R2 
&& is(typeof(binaryFun!pred(haystack.front, needle.front)) : 
bool) && !isRandomAccessRange!R1)
/home/per/opt/x86_64-unknown-linux-gnu/dmd/linux/bin64/src/phobos/std/algorithm.d(4589,4): 
        std.algorithm.find(alias pred = "a == b", R1, R2)(R1 
haystack, R2 needle) if (isRandomAccessRange!R1 && 
isBidirectionalRange!R2 && 
is(typeof(binaryFun!pred(haystack.front, needle.front)) : bool))
/home/per/opt/x86_64-unknown-linux-gnu/dmd/linux/bin64/src/phobos/std/algorithm.d(4661,4): 
        std.algorithm.find(alias pred = "a == b", R1, R2)(R1 
haystack, R2 needle) if (isRandomAccessRange!R1 && 
isForwardRange!R2 && !isBidirectionalRange!R2 && 
is(typeof(binaryFun!pred(haystack.front, needle.front)) : bool))
/home/per/opt/x86_64-unknown-linux-gnu/dmd/linux/bin64/src/phobos/std/algorithm.d(4887,23): 
        std.algorithm.find(alias pred = "a == b", Range, 
Ranges...)(Range haystack, Ranges needles) if (Ranges.length > 1 
&& is(typeof(startsWith!pred(haystack, needles))))
algorithm_ex.d(1452,22):        ... (2 more, -v to show) ...
algorithm_ex.d(1456,35): Error: template instance 
algorithm_ex.dropWhile!(int[], int) error instantiating
/home/per/Work/justd/msgpack.d(4614,25): Warning: calling 
msgpack.pack!(false, string).pack without side effects discards 
return value of type ubyte[], prepend a cast(void) if intentional
/home/per/Work/justd/msgpack.d(4614,25): Warning: calling 
msgpack.pack!(false, string).pack without side effects discards 
return value of type ubyte[], prepend a cast(void) if intentional

What have I done wrong?


More information about the Digitalmars-d-learn mailing list