Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upAdd `Iterator::for_each` #42782
Conversation
rust-highfive
assigned
alexcrichton
Jun 20, 2017
This comment has been minimized.
This comment has been minimized.
|
(rust_highfive has picked a reviewer for you, use r? to override) |
This comment has been minimized.
This comment has been minimized.
|
@bluss I'm curious about your "interesting reasons" to use |
This comment has been minimized.
This comment has been minimized.
|
@rust-lang/libs, any others have thoughts? |
alexcrichton
added
S-waiting-on-review
T-libs
labels
Jun 20, 2017
This comment has been minimized.
This comment has been minimized.
|
Works for me! |
This comment has been minimized.
This comment has been minimized.
|
I seem to remember there being philosophical objections to this back in the day, but I don't feel particularly strongly. |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
Interesting - I'll have to play around with that. And if we do want this
implemented on fold, that needs to happen before stabilization.
|
This comment has been minimized.
This comment has been minimized.
|
Woah, |
cuviper
force-pushed the
cuviper:iterator_for_each
branch
from
c5c238b
to
4a8ddac
Jun 21, 2017
frewsxcv
reviewed
Jun 22, 2017
| /// #![feature(iterator_for_each)] | ||
| /// | ||
| /// let mut v = vec![]; | ||
| /// (0..5).for_each(|x| v.push(x * 100)); |
This comment has been minimized.
This comment has been minimized.
frewsxcv
Jun 22, 2017
Member
i'm not necessarily opposed to the current example you have written, but i find the current example slightly less idiomatic (subjectively) than something like:
let v: Vec<_> = (0..5).map(|x| x * 100).collect();
This comment has been minimized.
This comment has been minimized.
cuviper
Jun 22, 2017
Author
Member
Sure, I was just aiming for something simple and testable. I would definitely use collect or extend for that in real code. Any ideas for something more meaningful?
Similarly, the added benchmarks are just sums.
This comment has been minimized.
This comment has been minimized.
budziq
Jun 24, 2017
Contributor
Any ideas for something more meaningful?
New cookbook contributors usually have problem with consuming functional flow they have built just for the sake of side effects (if they do not wish to obtain any value like in fold or collect). Switching to imperative for just to obtain side effects feels not idiomatic.
Some artificial examples that might not be any better
let (tx, rx) = channel();
(0..5).map(|x| x * 2 + 1).for_each(|x| { tx.send(x).unwrap(); } );["1", "2", "lol", "baz", "55"]
.iter()
.filter_map(|s| s.parse::<u16>().ok())
.map(|v| v * v)
.for_each(|v| println!("{}", v));
This comment has been minimized.
This comment has been minimized.
cuviper
Jun 26, 2017
Author
Member
@budziq I'm glad to hear of more motivation for this!
Your additional examples are OK, but still not testing anything besides successfully compiling. Note that my first example has an assert_eq with the for-loop result, so we actually get some sanity check that it really works, as trivial as that is. Your channel example could read the rx side to check the result though.
This comment has been minimized.
This comment has been minimized.
budziq
Jun 27, 2017
•
Contributor
@cuviper so something like that might be ok?
use std::sync::mpsc::channel;
let (tx, rx) = channel();
(0..5).map(|x| x * 2 + 1).for_each(|x| { tx.send(x).unwrap(); } );
assert_eq!(vec![1, 3, 5, 7, 9], rx.iter().take(5).collect::<Vec<_>>());
This comment has been minimized.
This comment has been minimized.
cuviper
Jun 27, 2017
Author
Member
That would work. Do folks like that better? Or perhaps as one more example?
This comment has been minimized.
This comment has been minimized.
|
I personally really wanted a |
This comment has been minimized.
This comment has been minimized.
|
@steveklabnik I think at that time the main point was that short iterators are probably better off with a Also, the performance benefit of internal iteration via |
This comment has been minimized.
This comment has been minimized.
I must have done a bad job advocating back then, then. Oh well. |
This comment has been minimized.
This comment has been minimized.
|
Ok sounds like there's no reason to not experiment with this at this point, @cuviper if you want to update the example (which I think the comments indicate?) then I'll r+ |
This comment has been minimized.
This comment has been minimized.
|
History time! I would argue that rust-lang/rfcs#1064 (comment) by @nagisa is a decent summary:
The first bullet point has changed. The other ones are still true. |
This comment has been minimized.
This comment has been minimized.
|
Thanks for finding more context! My search-fu is apparently weak... I don't think we have to falsify every point against -- only compare them to the points for:
|
This comment has been minimized.
This comment has been minimized.
|
In general, I feel like there are a lot of people that do want this, and the people against are just "meh". Anyway, I updated the first example like @budziq's suggestion. |
This comment has been minimized.
This comment has been minimized.
|
Obviously I've already been quoted at the top, but I agree with @cuviper that I think the pros outweigh the cons. I think that there is indeed new information since the last time this was discussed:
The two together mean that encouraging the use of The main argument against is basically "TMWTDI", which -- from my POV -- is a good enough reason to stop things without compelling advantages, but not an absolute blocker. I also think one of the points made at the time is at least somewhat false:
I presume that this is referring to |
nikomatsakis
reviewed
Jun 29, 2017
| /// | ||
| /// let (tx, rx) = channel(); | ||
| /// (0..5).map(|x| x * 2 + 1) | ||
| /// .for_each(move |x| tx.send(x).unwrap()); |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
cuviper
Jun 29, 2017
Author
Member
Maybe it's too sneaky, but that lets tx drop automatically, and then rx won't block.
This comment has been minimized.
This comment has been minimized.
|
@bors: r+ |
This comment has been minimized.
This comment has been minimized.
|
|
This comment has been minimized.
This comment has been minimized.
bors
added a commit
that referenced
this pull request
Jun 30, 2017
This comment has been minimized.
This comment has been minimized.
|
|
bors
merged commit e72ee6e
into
rust-lang:master
Jun 30, 2017
This comment has been minimized.
This comment has been minimized.
|
Yay! Now, since I left it unstable with |
This comment has been minimized.
This comment has been minimized.
|
Oh, yes, please do. We probably shouldn't have merged yet actually, but no worries! |
cuviper
referenced this pull request
Jun 30, 2017
Closed
Tracking issue for the `iterator_for_each` library feature #42986
This comment has been minimized.
This comment has been minimized.
|
See #42987 for that update. |
cuviper
referenced this pull request
Jun 30, 2017
Closed
Consider adding a foreach-like iterator adapter #1070
This comment has been minimized.
This comment has been minimized.
phimuemue
commented
Apr 2, 2018
•
|
Hi, I stumbled upon this, but I wondered why This value could be - as it is now - a In code, this could be captured by a trait
I implemented a
Usage could be as follows:
Has this ever been thought about? And if so, why was it apparently rejected? |
This comment has been minimized.
This comment has been minimized.
|
@phimuemue There's a form of that built around the |
This comment has been minimized.
This comment has been minimized.
|
@phimuemue As an example of "break with a certain item", check out how rust/src/libcore/iter/iterator.rs Lines 1738 to 1746 in ab8b961 (That If you wanted to do the same in your code today†, it can be done with self.try_for_each(move |x| {
if predicate(&x) { Err(x) }
else { Ok(()) }
}).err() † Well, once someone makes a stabilization PR for |
cuviper commentedJun 20, 2017
This works like a
forloop in functional style, applying a closure toevery item in the
Iterator. It doesn't allowbreak/continuelikea
forloop, nor any other control flow outside the closure, but it maybe a more legible style for tying up the end of a long iterator chain.
This was tried before in #14911, but nobody made the case for using it
with longer iterators. There was also
Iterator::advanceat that timewhich was more capable than
for_each, but that no longer exists.The
itertoolscrate hasItertools::foreachwith the same behavior,but thankfully the names won't collide. The
rayoncrate also has aParallelIterator::for_eachwhere simpleforloops aren't possible.