What are the Ruby equivalent of Python itertools, esp. combinations/permutations/groupby?
Array#permutation
, Array#combination
and Enumerable#group_by
are defined in ruby since 1.8.7. If you're using 1.8.6 you can get equivalent methods from facets or active_support or backports.
Example Usage:
[0,1,2].permutation.to_a#=> [[0, 1, 2], [0, 2, 1], [1, 0, 2], [1, 2, 0], [2, 0, 1], [2, 1, 0]][0,1,2,3].combination(2).to_a#=> [[0, 1], [0, 2], [0, 3], [1, 2], [1, 3], [2, 3]][0,0,0,1,1,2].group_by {|x| x}.map {|k,v| v}#=> [[0, 0, 0], [1, 1], [2]][0,1,2,3].group_by {|x| x%2}#=> {0=>[0, 2], 1=>[1, 3]}