-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Std::iter::once #771
Std::iter::once #771
Conversation
+1, I've wanted this before and disliked the IMO relatively ugly solutions. |
👍 I think this would be nice. As the RFC states, and as I'm sure droves of people will be tripping over each other to point out, this thing is already possible in a variety of different ways. The point is that this would make the intent clear and its use concise. The most common trick used that I've seen is |
I don't really see what's wrong with the |
@pythonesque Discoverability, ergonomics, and looking strange (coming across it for the first time, it looks odd and you might be tempted to think it's an iterator over Option). |
When someone asks how to do this, |
Let's just say I've written |
I made an issue about this before, but was discouraged: #437. |
+1, I had to define this in html5ever to get reasonable APIs. |
Could you add some examples where this is useful to the RFC? I've done at least some stuff with iterators and have never run into this. It is non-obvious to me why iterators are even relevant if you've only got one thing. |
bikeshedding on the name, iterators (and only iterators?) yield things. If you want and iterator that yields a value once, how about: somevalue.yield_once()? This would fit in more nicely with the language of the other iterator functions: If this is something that will be defined on literally every value in your code, I think you probably want it to be at least a bit specific about what it does, even if it's a few more characters... |
Adding a method on every single value type is unacceptable. |
@drewm1980: For example, in one of my library's tests I perform a globbing operation to get a list of paths to fixtures, but there's one fixture in a non-standard location (beyond my control), so what I do is I get the iterator for the glob, then I chain onto it the single extra path, so that then I can iterate over the composed iterator as one thing, instead of iterating over the glob, then doing it again only for the extra path that wasn't present in the glob results. Keep in mind this is just one example of many. Perhaps you have some code that operates on iterators, but you only have one item, so you create an iterator consisting solely of that item. |
It is useful when chaining together iterators. |
This could potentially replace (You can't replace this with |
html5ever accepts an iterator of |
+0.5. While (I do suspect that if we add this, we'll want |
Thanks for the example use cases! RE iterator chaining, don't iterators already chain nicely by design? Are there API's taking iterators of iterators that force you to increase the level of iteratorness in the middle of the chain? |
Anyway, +1 if it's not in the prelude, or if it's in the prelude with a name that at least hints it's related to iterators, i.e. "yield_once" or something. Is "yield" is already reserved as a keyword, perhaps for future pythonic ways of making generators/closures? IMHO, "once" alone is too generic for the prelude; at first guess I would expect it to call a closure without using () syntax, like the "twice" example from the docs: http://doc.rust-lang.org/book/closures.html#accepting-closures-as-arguments |
👍 I've wanted this from time to time for certain. |
+1 to this and @aturon's |
lgtm |
👍 |
We've got a lot of enthusiasm for this RFC, but before we merge, I wonder: is it worth adding |
Do we want the name |
|
How about |
One and zero? Or empty seems fine. Problem with one and none is its only 1 On Wed, Mar 4, 2015, 9:12 PM Aaron Turon notifications@github.com wrote:
|
@seanmonstar I'd be very surprised to see someone do such a typo. I mean, I didn't even notice they were that close... they look very distinct. |
@tshepang They'd also have different signatures. std::iter::one(42);
std::iter::none(); |
Good point, I retract my comment. On Thu, Mar 5, 2015, 8:20 AM Nathaniel Theis notifications@github.com
|
I actually found out about the |
@XMPPwocky Can you update the RFC with |
@aturon Done. Sorry, email from GitHub found its way into the great Gmail tab of spammy mailing lists, for some reason. |
Thanks @XMPPwocky! After getting stalled on some bikeshedding and lost emails, this RFC (which always has strong support) has been merged at last! |
Rendered