Module: Immutable::Foldable
Instance Method Summary collapse
-
#foldl(e, &block) ⇒ Object
Reduces
self
usingblock
from left to right. -
#length ⇒ Integer
(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
#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.
9 10 11 |
# File 'lib/immutable/foldable.rb', line 9 def foldl(e, &block) raise NotImplementedError end |
#length ⇒ Integer 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) { |x, y| x * y } end |
#sum ⇒ #+
Computes the sum of the numbers in self
.
28 29 30 |
# File 'lib/immutable/foldable.rb', line 28 def sum foldl(0) { |x, y| x + y } end |