Advertisement
Guest User

Untitled

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