Add a FilterMap operator
Kixiron opened this issue · comments
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
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.
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.
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) {}