I just ran into a situation where I needed to segregate a list into two lists, corresponding to those items which pass and don’t pass a validation test. Something like:
accepted_items = Enum.filter(items, &item_is_valid?/1)
rejected_items = Enum.reject(items, &item_is_valid?/1)
In this particular case, the size of items
is small enough that it’s not a big deal, but it still feels wasteful to run the same iteration and same validation function twice just to get those results. Perhaps someday we could add a single function that would do both at once? Something like:
{accepted_items, rejected_items} = Enum.filter_and_reject(items, &item_is_valid?/1)
Or maybe there’s already some technique here that I’m unfamiliar with?