count until predicate returns false

Jonathan M Davis jmdavisProg at gmx.com
Mon Aug 15 15:22:48 PDT 2011


On Monday, August 15, 2011 15:15 Andrej Mitrovic wrote:
> That doesn't work:
> 
> import std.algorithm;
> import std.stdio;
> import std.functional;
> import std.uni;
> 
> void main()
> {
> auto line = " foo";
> writeln(countUntil!(not!isWhite)(line));
> }
> 
> test.d(9): Error: template std.algorithm.countUntil(alias pred = "a ==
> b",R1,R2) if (is(typeof(startsWith!(pred)(haystack,needle)))) does not
> match any function template declaration
> test.d(9): Error: template std.algorithm.countUntil(alias pred = "a ==
> b",R1,R2) if (is(typeof(startsWith!(pred)(haystack,needle)))) cannot
> deduce template function from argument types !(not)(string)
> test.d(9): Error: template instance errors instantiating template

No, it doesn't. Because the currently released countUntil requires a second 
range to compare against (the predicate defaulting to "a == b"). However, 
there _is_ a new overload for countUntil on github which takes the predicate 
and one range like you're trying to do. And _that_ should work. If it doesn't, 
then it needs to be fixed.

I'd actually added it the other day and then thought that I'd figured out that 
count did it already and so I reverted it - but I'd misread what count did, 
not realizing that it counted the _whole_ range not just until the predicate 
failed. So, I've reverted my revert, and if you use Phobos from github, you 
should get the version of countUntil that you want.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list