www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - count until predicate returns false

reply Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
This will print the count of whitespace chars in a line:

writeln(count!isWhite(line));

What I need is the count of whitspace chars until the first
non-whitespace char is found, essentially I need a "countWhile"
template:

writeln(countWhile!isWhite(line));

Can I do this with existing templates somehow?
Aug 15 2011
parent "Simen Kjaeraas" <simen.kjaras gmail.com> writes:
On Mon, 15 Aug 2011 21:53:11 +0200, Andrej Mitrovic  
<andrej.mitrovich gmail.com> wrote:

 This will print the count of whitespace chars in a line:

 writeln(count!isWhite(line));

 What I need is the count of whitspace chars until the first
 non-whitespace char is found, essentially I need a "countWhile"
 template:

 writeln(countWhile!isWhite(line));

 Can I do this with existing templates somehow?
There is std.algorithm.countUntil, which does what's basically the opposite. Just negate your predicate, and presto's your uncle. -- Simen
Aug 15 2011