I'm working on a project and I was suprised with current implementation on
any on empty iterators. Their results make sense if you think about it like a monoid fold although it still suprising from the user perspective.
I wonder if anyone else think it worth adding into std. Or probably I should add it to itertool or my custom library first, although in the latter case the feature won't get its auditory even if it's worthy.
The general idea is add new
NonEmptyIterator which is an iterator with at least one element and add mapping
Option<NonEmptyIterator<Item=T>> so every iteratior maybe is non empty.
It helps in cases we want to perform specific operation if iterator produces no elements. For example, if we want
xs are empty. Current implementation returns
false in this case and we don't have any chance telling if collection was empty or predicate returned false for every element.
Why would you want to know that? For example when I have user preferences where one could filter incoming messages by tag. Okay, we write:
let is_message_should_be_displayed = user_preferences.mesage_filter_tags.any(|x| message.tags.contains(x))
But if user has empty
mesage_filter_tags it means he doesn't want to filter anything so
is_message_should_be_displayed should be
true. Unfortunately the current implementation will return
You can hack it in several ways depending if you know the backing collection behind
mesage_filter_tags so you could check its length or if you don't then you could write something like
let is_message_should_be_displayed = user_preferences.mesage_filter_tags.filter(|x| message.tags.contains(x)) .map(|_| true).next().unwrap_or(true)
But it all looks quite hacky and ugly. I find my proposal more general and clean.