Module: Immutable::Foldable

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

Instance Method Summary (collapse)

Instance Method Details

- (Object) foldl(e, &block)

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

Parameters:

  • e (Object)

    the start value.

Returns:

  • (Object)

    the reduced value.

Raises:

  • (NotImplementedError)


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

def foldl(e, &block)
  raise NotImplementedError
end

- (Integer) length Also known as: size

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

Returns:

  • (Integer)

    the number of elements in self.



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.

Returns:

  • (#*)

    the product of the numbers.



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

def product
  foldl(1, &:*)
end

- (#+) sum

Computes the sum of the numbers in self.

Returns:

  • (#+)

    the sum of the numbers.



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

def sum
  foldl(0, &:+)
end