Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module Enumerable
- def partition_many(*conditions)
- conditions.reduce([[], self]) { |(previously_selected, previously_rejected), condition|
- newly_selected, newly_rejected = previously_rejected.partition(&condition)
- [previously_selected + [newly_selected], newly_rejected]
- }
- end
- end
- [1, 2, 3, 4, 5, 6, 7, 8, 9].partition_many(
- -> (el) { el % 3 == 0 },
- -> (el) { el == 2 || el == 6 },
- -> (el) { el % 2 == 0 }
- ) # => [[[3, 6, 9], [2], [4, 8]], [1, 5, 7]]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement