Created
July 19, 2015 16:44
-
-
Save kachick/4c53ed4eed9a4b590f4d to your computer and use it in GitHub Desktop.
Ruby 全ての組み合わせ
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding: us-ascii | |
# Copyright (c) 2015 Kenichi Kamiya | |
class Array | |
def all_combinations(&block) | |
return to_enum __callee__ unless block_given? | |
size.downto(1).map { |n| combination(n, &block) } | |
end | |
end | |
p [1, 2, 3].all_combinations.to_a #=> [[1, 2, 3], [1, 2], [1, 3], [2, 3], [1], [2], [3]] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment