Skip to content

Implementations of takeWhile, drop, and dropWhile.#147

Closed
jmdavis wants to merge 8 commits intodlang:masterfrom
jmdavis:takeWhile
Closed

Implementations of takeWhile, drop, and dropWhile.#147
jmdavis wants to merge 8 commits intodlang:masterfrom
jmdavis:takeWhile

Conversation

@jmdavis
Copy link
Member

@jmdavis jmdavis commented Jul 17, 2011

This fulfills http://d.puremagic.com/issues/show_bug.cgi?id=4535 and partially fulfills http://d.puremagic.com/issues/show_bug.cgi?id=5645

drop and dropWhile in particular should help allow a more functional style of programming when dealing with ranges.

jmdavis added 5 commits July 16, 2011 19:01
This partially fulfills http://d.puremagic.com/issues/show_bug.cgi?id=5645

I'm not sure if it makes any sense to implement the slice function
asked for in there though. But then again, I also don't quite understand
what it does either. Those two items probably should have been on
separate enhancement requests.
std/range.d Outdated
Copy link
Contributor

Choose a reason for hiding this comment

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

@property? (pure nothrow const @safe too, if you like :) )

Copy link
Member Author

Choose a reason for hiding this comment

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

You're right. I forgot @Property, and I can put const on it, but I can't put pure, nothrow, or @safe on it because that depends on the range being passed in. Those will have to be inferred.

Copy link
Contributor

Choose a reason for hiding this comment

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

return _done; is pure nothrow @safe no matter if the range's annotation. But you're right, it's better to let the compiler infer that.

@jmdavis
Copy link
Member Author

jmdavis commented Jul 28, 2011

Any more comments on this before I merge it in?

jmdavis added 2 commits July 28, 2011 01:00
I also made it so that takeWhile's Result isn't static anymore. It
didn't work with delegates that way.
@jmdavis
Copy link
Member Author

jmdavis commented Aug 6, 2011

Any more comments, suggestions, or objections? I'd like to merge this in as it stands, and Andrei now seems to be okay with takeWhile being added to Phobos, which appears to have been his main objection. drop borders on a necessity IMHO, since it's a huge boon for functional-style programming, and if we have drop and takeWhile, then it makes sense to have dropWhile. So, it seems to me that they should all be added to Phobos, but I don't want to merge them and then have complaints about it, and it's not entirely clear to me that everyone's concerns about these have been addressed.

So, does anyone have a good reason as to why any of these shouldn't be merged into Phobos?

@andralex
Copy link
Member

Upon looking at this again, I think this addition doesn't pull its weight. takeWhile is isomorphic with until, drop is a lossy version of popFrontN, and dropWhile is isomorphic with find. Jonathan, that's not to diminish your work -- sorry. You may want to put this up for a community vote or something. My opinion is that we don't need such redundancy in std.algorithm.

@jmdavis
Copy link
Member Author

jmdavis commented Aug 14, 2011

Well, if you can do exactly the same thing as takeWhile and dropWhile with until and find, then I think that there are solid arguments for not adding them. However, as far as I know, ou can't do the same thing as drop with anything else. popFrontN can't be used in an expression while drop can. drop is really useful when chaining functions together in way that popFrontN can't be.

So, if you really think that takeWhile and dropWhile don't pull their weight, then I can live with that. Other functions can be made to do the same thing fairly easily. But the same isn't true for drop. So, if you really don't think that drop is worth adding, then I guess that I'll have to go for a community vote on that one. I can live without having takeWhile or dropWhile, but it's very annoying not to have drop, since popFrontN just be used in a functional style.

@andralex
Copy link
Member

You may want to ask for a vote on drop. I'd use (popFrontN(r), r) for doing what drop does, and overall I prefer popFrontN because it returns the number of elements popped - information which is lost with drop.

@jmdavis
Copy link
Member Author

jmdavis commented Aug 14, 2011

I'll ask for a vote on it then. (popFrontN(r), r) is a solution that I'd never thought of, but then again I never use the comma operator aside from the 3rd portion of a for loop, and I believe that most programmers agree with me an that one. Regardless, there's no point in keeping this pull request open. I'd need to redo it to just have drop anyway.

On a related note, we might want to look at how we can improve the documentation to make it clearer as to what functions can be used to do the same thing that other languages do. For instance, reduce already mentions foldl. Perhaps until and find should mention takeWhile and dropWhile. It's not entirely uncommon for someone to think that there's no function in Phobos which does what they want simply because none of them have a name that they recognize as doing what they want. It's not an entirely solvable problem, but we can probably find ways to improve the situation.

@jmdavis jmdavis closed this Aug 14, 2011
kuettler pushed a commit to kuettler/phobos that referenced this pull request Feb 6, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants