Methods
(static) fold(a) → {Object}
Combine the elements of a structure using the monoid. For example, fold a list of lists into a
single list.
Haskell> fold :: Monoid m => t m -> m
Parameters:
Name | Type | Description |
---|---|---|
a |
Object | The monoid to fold |
- Source:
Returns:
The folded monoid
- Type
- Object
Example
const mb = just(1);
const mmb = just(mb);
const lst = list(1,2,3); // => [1:2:3:[]]
const llst = list(lst); // => [[1:2:3:[]]:[]]
fold(llst); // => [1:2:3:[]]
fold(mmb); // => Just 1
(static) Foldable() → {boolean}
A Foldable
is a data structure that can be folded into a summary value. Lists are a common form
of foldable. Instances of Foldable must define a foldr
method.
Parameters:
Type | Description |
---|---|
* | Any object |
- Source:
Returns:
true
if an object is an instance of Foldable
and false
otherwise
- Type
- boolean
(static) foldMap(f, a) → {Object}
Map each element of the structure to a monoid, and combine the results.
Haskell> foldMap :: Monoid m => (a -> m) -> t a -> m
Parameters:
Name | Type | Description |
---|---|---|
f |
function | The function to map |
a |
Object | The monoid to map over |
- Source:
Returns:
A new monoid of the same type, the result of the mapping
- Type
- Object
Example
const mb = just(1);
const lst = list(1,2,3);
const f1 = x => just(x * 3);
const f2 = x => list(x * 3);
foldMap(f1, mb); // => Just 3
foldMap(f2, lst); // => [3:6:9:[]]
(static) foldr(f, z, t) → {*}
Right-associative fold of a structure. This is the work horse function of Foldable
. See also
the list reducing function foldl
for the left-associative version.
Haskell> foldr :: (a -> b -> b) -> b -> t a -> b
Parameters:
Name | Type | Description |
---|---|---|
f |
function | A binary function |
z |
* | A base accumulator value |
t |
Object | A |
- Source:
Returns:
The result of applying the function to the foldable and the accumulator
- Type
- *
Example
const mb = just(1);
const tup = tuple(1,2);
const lst = list(1,2,3);
const f = (x, y) => x + y;
foldr(f, 0, mb); // => 1
foldr(f, 0, tup); // => 2
foldr(f, 0, lst); // => 6