Skip to content
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

Add a FilterMap operator #355

Open
Kixiron opened this issue Jan 25, 2021 · 3 comments
Open

Add a FilterMap operator #355

Kixiron opened this issue Jan 25, 2021 · 3 comments

Comments

@Kixiron
Copy link
Contributor

Kixiron commented Jan 25, 2021

This'd be a nice ergonomic tweak, an operator in the style of Iterator::filter_map() would be really great. Currently there's a somewhat strange workaround using .flat_map(), e.g.

stream.flat_map(|foo| {
    if foo.bar() {
        Some(foo.baz)
    } else {
        None
    }
})

and while this would functionally be nearly identical, the FilterMap operator could likely specialize somewhat since it only has to handle a single output value

@frankmcsherry
Copy link
Member

I think filter_map is just flat_map, isn't it? The latter takes something that implements IntoIterator<Item=T> and Option<T> implements that trait.

@ryzhyk
Copy link
Contributor

ryzhyk commented Jan 25, 2021

Heh, sorry about my (deleted) posts, I'm used to seeing your messages in the DDlog repo :)

But yeah, I use flat_map to express filter_map in DD.

@Kixiron
Copy link
Contributor Author

Kixiron commented Jan 26, 2021

Implementation-wise yes, filter_map and flat_map are equivalent. However, they do have the semantic difference of "flattening an expanding collection" vs. "removing some items from a collection". It's a very slight thing that tripped me up until I discovered the flat_map pattern, it works but it feels like a slight misuse of impl Iterator for Option, almost like writing for x in Some(thing) {}

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

No branches or pull requests

3 participants