Module: Combinatorics::Choose::Mixin
Overview
Instance Method Summary collapse
-
#choose(k) {|combo| ... } ⇒ Enumerator
Get combinations with a specified number of elements from an input set.
Instance Method Details
#choose(k) {|combo| ... } ⇒ Enumerator
Get combinations with a specified number of elements from an input set.
39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/combinatorics/choose/mixin.rb', line 39 def choose(k,&block) return enum_for(:choose,k) unless block unless kind_of?(Enumerable) raise(TypeError,"#{inspect} must be Enumerable") end elements = self.to_a elements.uniq! elements.combination(k) do |subset| yield Set.new(subset) end end |