module Enumerable

Reopen to add utility methods

Public Instance Methods

partitions(max_length = nil) click to toggle source

Enumerate all partitionings of an enumerable @return [Enumerator]

# File lib/nose/util.rb, line 25
def partitions(max_length = nil)
  max_length = length if max_length.nil?
  Enumerator.new do |enum|
    1.upto(max_length).map do |length|
      enum.yield partition.with_index { |_, i| i < length }
    end
  end
end
prefixes() click to toggle source

Enumerate all non-empty prefixes of the enumerable @return [Enumerator]

# File lib/nose/util.rb, line 13
def prefixes
  Enumerator.new do |enum|
    prefix = []
    each do |elem|
      prefix = prefix.dup << elem
      enum.yield prefix
    end
  end
end
product_by(initial = 1) { |item| ... } click to toggle source

Take the product of the result of calling the block on each item @return [Object]

# File lib/nose/util.rb, line 42
def product_by(initial = 1)
  reduce(initial) { |product, item| product * yield(item) }
end
sum_by(initial = 0) { |item| ... } click to toggle source

Take the sum of the result of calling the block on each item @return [Object]

# File lib/nose/util.rb, line 36
def sum_by(initial = 0)
  reduce(initial) { |sum, item| sum + yield(item) }
end