Fix Issue 4535 - std.range could have a takeWhile!pred(range) function#5563
Fix Issue 4535 - std.range could have a takeWhile!pred(range) function#5563RazvanN7 wants to merge 1 commit intodlang:masterfrom
Conversation
|
Thanks for your pull request, @RazvanN7! We are looking forward to reviewing it, and you should be hearing from a maintainer soon. Some tips to help speed things up:
Bear in mind that large or tricky changes may require multiple rounds of review and revision. Please see CONTRIBUTING.md for more information. Bugzilla references
|
72dcd4d to
e83a189
Compare
|
|
||
| See_Also: $(LREF chain) to chain ranges | ||
| */ | ||
| auto takeWhile(alias pred, R)(R range) if (isInputRange!R) |
There was a problem hiding this comment.
alias takeWhile = until;https://dlang.org/phobos/std_algorithm_searching.html#.until
This seems to be especially hard to find even though I tried to help people with #4985
Btw see also: #147
There was a problem hiding this comment.
Other common confusions:
dropWhile->findflatten->joinerany->existszipMap->assocArrayindexOfvs.(byChar).countUntilgroup->chunkBy
There was a problem hiding this comment.
You might also like this table: https://github.com/wilzbach/linq for a mapping from LINQ to D
There was a problem hiding this comment.
I think that takeWhile is much cleaner than until, in the sense that it is more generic and it doesn't have a sentinel. See discussion on bug report
There was a problem hiding this comment.
I like takeWhile as well, but I'm pretty sure that @andralex will veto this as it adds yet another symbol to std.algorithm
Hence to save you work, my advice is to look into incorporating your changes into until.
There was a problem hiding this comment.
IIRC, I tried to add takeWhile a number of years ago, and he vetoed it along with other stuff like dropWhile. Basically, the decision was that we could do what we needed to do with what we had, and you're always going to have problems with folks finding stuff, because they assume one name, whereas the person who designed the function thought of another - and changing the name often just results in a different set of people not finding what they're looking for (a prime example of this would be strip vs trim - some languages/libraries use one and some the other, and both are valid names, but if the library you're trying to use didn't use the one you're thinking of, you might not find it). And we really can't fix that problem. You try to pick a good name and then try and make sure that your documentation makes it easy to find.
In this case, until already does what this PR does - it even has an overload that doesn't take a sentinel. And honestly, the fact that is does have the ability to indicate a sentinel as well as whether it's open on the right makes it more useful than this implementation of takeWhile.
Certainly, I see no reason to add this code. It's just doing what until already does - only less. At most, it would make sense to create an alias or to make takeWhile a wrapper around until, but neither would help functionality at all. It's just bikeshedding over the name, and while it might make it easier for some people to find the function, it's also going to result in confusion over the difference between takeWhile and until. Our policy has generally been to avoid adding aliases to provide additional names.
So, while I think that this is ultimately up to Andrei, I don't think that it makes sense to add takeWhile given that we already have until, much as I agree that takeWhile would likely have been a better name.
There was a problem hiding this comment.
If the implementation is really short (one-liner or alias), perhaps a good compromise is to implement it as an example unittest (e.g. as here). This way we 1) make the name findable, 2) show off Phobos' composition capabilities, and 3) avoid cluttering the name space with trivial one-liners. Win-win-win.
There was a problem hiding this comment.
Got it. I only made the PR so that we move on with the bug (closing it as invalid or fixing it). Thanks
There was a problem hiding this comment.
@CyberShadow until's documentation actually already mentions takeWhile, saying
This is similar to `takeWhile` in other languages.
So, as far as searchability goes, if they search the documentation, they'll find it. It just won't be in the links, because it's not actually a symbol, just a comment in the documentation.
|
Is it REALLY necessary to implement a new range type for this? We have so many range primitives in Phobos, I can't imagine that you can't compose the existing ones to implement this function. |
The implementation is taken from the bug report with minor changes and additions + I added the documentation