Skip to content

Add a FilterMap operator #355

Open
Open

Description

@Kixiron

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

Activity

  1. frankmcsherry commented on Jan 25, 2021

    @frankmcsherry
    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.

  2. ryzhyk commented on Jan 25, 2021

    @ryzhyk
    Contributor

    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.

  3. Kixiron commented on Jan 26, 2021

    @Kixiron
    ContributorAuthor

    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions