Sha256: 6b96d174598f6a8c979f858c0b0771d81a312d958c1aecf066613b013ff5f57f

Contents?: true

Size: 1.04 KB

Versions: 376

Compression:

Stored size: 1.04 KB

Contents

# frozen_string_literal: true

module Enumerable
  # Produces a array with values's all combinations.
  #
  # Example:
  #   %i[a b].boolean_combinations
  #   => [[], [:a], [:b], [:a, :b]]
  #
  # @return [Array]
  def bool_array_combs
    bool_combs([], method(:bool_array_combs_new_comb))
  end

  # Produces a hash with values's all combinations.
  #
  # Example:
  #   %i[a b].boolean_combinations
  #   => [{a: false, b: false}, {a: false, b: true}, {a: true, b: false}, {a: true, b: true}]
  #
  # @return [Hash]
  def bool_hash_combs
    bool_combs({}, method(:bool_hash_combs_new_comb))
  end

  private

  def bool_combs(empty_value, new_comb_method)
    head = [empty_value]
    r = inject(head) do |a, value|
      new_comb_method.call(value, a)
    end
    r == head ? [] : r
  end

  def bool_array_combs_new_comb(value, combs)
    combs + combs.map { |c| c + [value] }
  end

  def bool_hash_combs_new_comb(value, combs)
    combs.flat_map do |comb|
      [false, true].map { |bool_value| comb.dup.merge(value => bool_value) }
    end
  end
end

Version data entries

376 entries across 376 versions & 4 rubygems

Version Path
eac_tools-0.60.2 sub/eac_ruby_utils/lib/eac_ruby_utils/patches/enumerable/boolean_combinations.rb
eac_tools-0.60.1 sub/eac_ruby_utils/lib/eac_ruby_utils/patches/enumerable/boolean_combinations.rb
eac_tools-0.60.0 sub/eac_ruby_utils/lib/eac_ruby_utils/patches/enumerable/boolean_combinations.rb
eac_tools-0.59.0 sub/eac_ruby_utils/lib/eac_ruby_utils/patches/enumerable/boolean_combinations.rb
eac_tools-0.58.0 sub/eac_ruby_utils/lib/eac_ruby_utils/patches/enumerable/boolean_combinations.rb
eac_tools-0.57.0 sub/eac_ruby_utils/lib/eac_ruby_utils/patches/enumerable/boolean_combinations.rb
eac_tools-0.56.1 sub/eac_ruby_utils/lib/eac_ruby_utils/patches/enumerable/boolean_combinations.rb
eac_tools-0.56.0 sub/eac_ruby_utils/lib/eac_ruby_utils/patches/enumerable/boolean_combinations.rb
eac_tools-0.55.7 sub/eac_ruby_utils/lib/eac_ruby_utils/patches/enumerable/boolean_combinations.rb
eac_tools-0.55.6 sub/eac_ruby_utils/lib/eac_ruby_utils/patches/enumerable/boolean_combinations.rb
eac_tools-0.55.5 sub/eac_ruby_utils/lib/eac_ruby_utils/patches/enumerable/boolean_combinations.rb
eac_tools-0.55.4 sub/eac_ruby_utils/lib/eac_ruby_utils/patches/enumerable/boolean_combinations.rb
eac_tools-0.55.3 sub/eac_ruby_utils/lib/eac_ruby_utils/patches/enumerable/boolean_combinations.rb
eac_tools-0.55.2 sub/eac_ruby_utils/lib/eac_ruby_utils/patches/enumerable/boolean_combinations.rb
eac_ruby_utils-0.112.0 lib/eac_ruby_utils/patches/enumerable/boolean_combinations.rb
eac_tools-0.55.1 sub/eac_ruby_utils/lib/eac_ruby_utils/patches/enumerable/boolean_combinations.rb
eac_ruby_utils-0.111.0 lib/eac_ruby_utils/patches/enumerable/boolean_combinations.rb
eac_tools-0.55.0 sub/eac_ruby_utils/lib/eac_ruby_utils/patches/enumerable/boolean_combinations.rb
eac_tools-0.54.0 sub/eac_ruby_utils/lib/eac_ruby_utils/patches/enumerable/boolean_combinations.rb
eac_tools-0.53.0 sub/eac_ruby_utils/lib/eac_ruby_utils/patches/enumerable/boolean_combinations.rb