Module: Enumerable

Included in:
NoSE::KeyPath, NoSE::Plans::QueryPlan, NoSE::Plans::QueryPlanTree
Defined in:
lib/nose/util.rb

Overview

Reopen to add utility methods

Instance Method Summary collapse

Instance Method Details

#partitions(max_length = nil) ⇒ Enumerator

Enumerate all partitionings of an enumerable

Returns:

  • (Enumerator)


25
26
27
28
29
30
31
32
# 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

#prefixesEnumerator

Enumerate all non-empty prefixes of the enumerable

Returns:

  • (Enumerator)


13
14
15
16
17
18
19
20
21
# 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) ⇒ Object

Take the product of the result of calling the block on each item

Returns:



42
43
44
# File 'lib/nose/util.rb', line 42

def product_by(initial = 1)
  reduce(initial) { |product, item| product * yield(item) }
end

#sum_by(initial = 0) ⇒ Object

Take the sum of the result of calling the block on each item

Returns:



36
37
38
# File 'lib/nose/util.rb', line 36

def sum_by(initial = 0)
  reduce(initial) { |sum, item| sum + yield(item) }
end