Module: Immutable::Foldable

Included in:
Headable, Map
Defined in:
lib/immutable/foldable.rb

Instance Method Summary collapse

Instance Method Details

#foldl(e, &block) ⇒ Object

Reduces self using block from left to right. e is used as the starting value. A class including Immutable::Foldable must implement this method.

Raises:

  • (NotImplementedError)


9
10
11
# File 'lib/immutable/foldable.rb', line 9

def foldl(e, &block)
  raise NotImplementedError
end

#lengthInteger Also known as: size

Returns the number of elements in self. May be zero.



16
17
18
# File 'lib/immutable/foldable.rb', line 16

def length
  foldl(0) { |x, y| x + 1 }
end

#product#*

Computes the product of the numbers in self.



35
36
37
# File 'lib/immutable/foldable.rb', line 35

def product
  foldl(1, &:*)
end

#sum#+

Computes the sum of the numbers in self.



28
29
30
# File 'lib/immutable/foldable.rb', line 28

def sum
  foldl(0, &:+)
end