Module: Immutable::Foldable
Instance Method Summary (collapse)
-
- (Object) foldl(e, &block)
Reduces self using block from left to right.
-
- (Integer) length
(also: #size)
Returns the number of elements in self.
-
- (#*) product
Computes the product of the numbers in self.
-
- (#+) sum
Computes the sum of the numbers in self.
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.
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.
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 |