[Issue 4535] std.range could have a takeWhile!pred(range) function

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Tue Jul 11 03:02:24 PDT 2017


https://issues.dlang.org/show_bug.cgi?id=4535

--- Comment #8 from Jonathan M Davis <issues.dlang at jmdavisProg.com> ---
(In reply to timon.gehr from comment #7)
> (In reply to Jonathan M Davis from comment #6)
> > We already have until,
> > which does what takeWhile would do - it just has an arguably worse name. Is
> > there something about until that does not fulfill what takeWhile would do?
> 
> In many (most?) circumstances it is just silly to have to specify a
> condition to detect the first element that you do /not/ want included in
> your range. There is a reason 'filter' keeps the elements for which the
> predicate evaluates to 'true' and not those for which it evaluates to
> 'false'.

True, in may circumstances, having to negate your predicate is annoying, but
when I tried to get takeWhile and dropWhile added on that basis several years
ago, Andrei vetoed it, because it was trivial to just negate the predicate, and
he's right. It may be a bit annoying, but it is trivial.

There's still some value in takeWhile on the basis of user friendliness if you
have a predicate that would need to be negated to be used with until, and some
folks do go looking for takeWhile and fail to find until (though until's docs
do now mention that it's essentially takeWhile), but we do generally try to
avoid adding extremely simple functions to Phobos, and if takeWhile or
dropWhile were added, they would almost certainly be added as simple wrappers
around until and find respectively.

So, unless someone can convince Andrei that having to negate the predicate is
onerous enough to merit adding the opposite functions and/or the negated
versions are more common than what we have, I think that it's pretty clear that
neither takeWhile nor dropWhile are going to be added, because we already have
the functionality that they provide, even if it does require that you sometimes
negate your predicate.

--


More information about the Digitalmars-d-bugs mailing list