-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Simplify flat_map example #25698
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
Simplify flat_map example #25698
Conversation
(rust_highfive has picked a reviewer for you, use r? to override) |
I wanted to come back to this to make it easier to understand. I added that example a long time ago. What do you think of something like this? let letters = ["alpha", "beta", "gamma"];
assert_eq!(letters.iter()
.flat_map(|s| s.chars())
.collect::<String>(),
"alphabetagamma"); Or something else that's easier to concretely understand how it maps to an iterator. |
I'm not sure. I don't think the wording for this is easy to understand at all. Every time I try to modify the examples it fails and I'm not sure why. Apparently I don't understand it: let num = [[1, 2], [3, 4], [5, 6]];
let v = num.into_iter()
.flat_map(|a| a.into_iter() )
.collect::<Vec<i32>>();
println!("{:?}", v); |
with |
I think the example you gave is the best it's gonna get currently. If I slightly reworded it to emphasize that it maps a function to each element. The way it was worded before, it sounds like mapping to an iterator is automatic when actually you must specify the iterator producing function. If you think it's good, I'll ask for review. |
/// Creates an iterator that maps each element to an iterator, | ||
/// and yields the elements of the produced iterators. | ||
/// Creates an iterator that maps an iterator returning function to | ||
/// each element. Yields the elements of the produced iterators. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The function (closure) maps each element to an iterator (actually to anything implementing IntoIterator).
".flat_map()
takes a function that maps each element into a new iterator, and yields all the elements of the produced iterators.”
Let's add something you said about denesting as well? Might as well explain more at least IMO.
“This can be useful do unravel a nested structure.”
@bluss updated |
I do think it sound much better than at the beginning. |
Great! |
@bors: r+ rollup |
📌 Commit 5b443b2 has been approved by |
I'm not sure why `core` is on but it's blocking the playpen. Doesn't seem to be needed but I'm not sure. It's not on the playpen template and playpen works on release and nightly. Seems easier to understand without `take()`.
You can fix by replacing fn main() {
let num = [[1, 2], [3, 4], [5, 6]];
let v = num.into_iter()
.flat_map(|a| a.into_iter() )
.map(|it| it.clone()) // copy each element
.collect::<Vec<_>>(); // note: Vec<_>
println!("{:?}", v);
assert_eq!(vec![1, 2, 3, 4, 5, 6], v);
} Just in case someone's search result lands here same as my one did. |
I'm not sure why
core
is on but it's blocking the playpen. Doesn't seem to be needed but I'm not sure. It's not on the playpen template and playpen works on release and nightly.Seems easier to understand without
take()
.