Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Issue 4535 - std.range could have a takeWhile!pred(range) function #5563

Closed
wants to merge 1 commit into from

Conversation

RazvanN7
Copy link
Collaborator

@RazvanN7 RazvanN7 commented Jul 7, 2017

The implementation is taken from the bug report with minor changes and additions + I added the documentation

@dlang-bot
Copy link
Contributor

dlang-bot commented Jul 7, 2017

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:

  • smaller, focused PRs are easier to review than big ones

  • try not to mix up refactoring or style changes with bug fixes or feature enhancements

  • provide helpful commit messages explaining the rationale behind each change

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

Auto-close Bugzilla Description
4535 std.range could have a takeWhile!pred(range) function

@RazvanN7 RazvanN7 force-pushed the Issue_4535 branch 2 times, most recently from 72dcd4d to e83a189 Compare July 7, 2017 08:25

See_Also: $(LREF chain) to chain ranges
*/
auto takeWhile(alias pred, R)(R range) if (isInputRange!R)
Copy link
Member

@wilzbach wilzbach Jul 7, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Member

@wilzbach wilzbach Jul 7, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Other common confusions:

  • dropWhile -> find
  • flatten -> joiner
  • any -> exists
  • zipMap -> assocArray
  • indexOf vs. (byChar).countUntil
  • group -> chunkBy

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You might also like this table: https://github.com/wilzbach/linq for a mapping from LINQ to D

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it. I only made the PR so that we move on with the bug (closing it as invalid or fixing it). Thanks

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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.

@CyberShadow
Copy link
Member

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.

@RazvanN7 RazvanN7 closed this Jul 10, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants