Or do a new version of reduce that stops the reducing on a certain condition
reduceWhile(alias reducer, alias predicate) {... reduce using reducer, as long as predicate holds }
That would be a nice addition to std.algo. The question is: what should test the predicate? The last returned value, the entire result being created?
As an asideJonathan, you may be interested in using some of the algorithms here:
http://www.dsource.org/projects/dranges'all' and 'some' are in the 'predicates' module. They accept predicates of any arity (even 0!), because I wanted to test for increasing numerical ranges, like this :
auto isGrowing = all ! "a<b" (range);
Note that the fact of accepting variable-arity predicates represents 90% of the complexity, because a simple all is:
import std.functional: unaryFun;
import std.range: isInputRange;
bool all(alias predicate, R)(R range) if (isInputRange!R)
{
foreach(elem; range)
{
if (!unaryFun!predicate(elem)) return false;
}
return true;
}
// warning : untested.
usage:
auto a = all ! "a<0" (range)
or
auto a = all ! foo (range)
Philippe