Skip to content

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

Merged
merged 1 commit into from
May 25, 2015
Merged

Simplify flat_map example #25698

merged 1 commit into from
May 25, 2015

Conversation

mdinger
Copy link
Contributor

@mdinger mdinger commented May 22, 2015

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().

@rust-highfive
Copy link
Contributor

r? @nikomatsakis

(rust_highfive has picked a reviewer for you, use r? to override)

@bluss
Copy link
Member

bluss commented May 22, 2015

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.

@mdinger
Copy link
Contributor Author

mdinger commented May 22, 2015

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);

playpen

@bluss
Copy link
Member

bluss commented May 22, 2015

with .collect::<Vec<&i32>>(); it's fine. arrays don't have any by-value iterator in libstd, so that outer num.into_iter() is the same as .iter().

@mdinger
Copy link
Contributor Author

mdinger commented May 23, 2015

I think the example you gave is the best it's gonna get currently. If into_iter() worked on arrays, that example I gave might be better as is emphasizes the ability to denest where it's less obvious with your example. Using vec! makes it messy.

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.
Copy link
Member

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

@mdinger
Copy link
Contributor Author

mdinger commented May 23, 2015

@bluss updated

@mdinger
Copy link
Contributor Author

mdinger commented May 23, 2015

I do think it sound much better than at the beginning.

@mdinger
Copy link
Contributor Author

mdinger commented May 23, 2015

r? @steveklabnik

@bluss
Copy link
Member

bluss commented May 23, 2015

Great!

@steveklabnik
Copy link
Member

@bors: r+ rollup

@bors
Copy link
Collaborator

bors commented May 25, 2015

📌 Commit 5b443b2 has been approved by steveklabnik

bors added a commit that referenced this pull request May 25, 2015
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()`.
@bors
Copy link
Collaborator

bors commented May 25, 2015

⌛ Testing commit 5b443b2 with merge 1d00028...

@bors bors merged commit 5b443b2 into rust-lang:master May 25, 2015
@My-
Copy link

My- commented Apr 28, 2019

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);

playpen

You can fix by replacing Vec<i32> to Vec<_> like:

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.

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.

7 participants