#Is there a clean way of doing optional filter to iterators?

1 messages · Page 1 of 1 (latest)

crystal geode
#

To clarify: The crate can make filters optional. Ex: If a value you want to filter on is None. The filter does nothing.

sleek monolith
#

If you have a bool b that determines if the filter is enabled, you can just do

.filter(|i| !b || condition(i))
```The only issue with this is that it may fail to optimize out redundant checks on `b` if the loop is complex. You can get around that by making a custom type like that crate does, but the crate only goes halfway and doesn't override `try_fold`, so it's not any better than the code above in most circumstances.
crystal geode
#

hmmm...
The loop will often often contain None values that i dont want to filter on. I guess optimizing for speed isn't critical.
But am I wrong in thinking that this should just be a feature?
I want to suggest it for either itertools or just rust.
This is used a lot and can be heavily optimized

sleek monolith
#

There's no need for this to be in the standard library since it's fully implementable in user code. Itertools might add it, but you should just make it yourself.