Partitioning a Collection
Sometimes in the middle of processing a pile of data, we may run into a business requirement that needs us to split a collection of data into two groups: elements that satisfy a criteria and those that don’t. For example, full-time employees vs. part-time, minors vs. adults, electric- vs. gas-operated machines, honest vs. dishonest politicians, and so on. Well, on second thought, the last one may not need any computational effort. Nevertheless, in general, splitting a collection is a task we want to carry out efficiently.
As we’ve seen before, the filter function can be used to pick elements that satisfy a condition and discard the rest from a collection. But if we use this method we’ll have to iterate over the collection ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access