Module: Immutable::Foldable

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

Instance Method Summary collapse

Instance Method Details

#lengthInteger Also known as: size

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

Returns:

  • (Integer)

    the number of elements in self.



6
7
8
# File 'lib/immutable/foldable.rb', line 6

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

#product#*

Computes the product of the numbers in self.

Returns:

  • (#*)

    the product of the numbers.



25
26
27
# File 'lib/immutable/foldable.rb', line 25

def product
  foldl(1, &:*)
end

#sum#+

Computes the sum of the numbers in self.

Returns:

  • (#+)

    the sum of the numbers.



18
19
20
# File 'lib/immutable/foldable.rb', line 18

def sum
  foldl(0, &:+)
end