Applies a function to each variant of the Maybe structure.
forall a, b: (Maybe a).(() => b, (a) => b) => b
Applies a function to each variant of the Maybe structure.
const Maybe = require('folktale/maybe');
Maybe.Just(1).fold(
(() => 'nothing'),
((v) => `got ${v}`)
);
// ==> 'got 1'
Maybe.Nothing().fold(
(() => 'nothing'),
((v) => `got ${v}`)
);
// ==> 'nothing'
{
/*~*/
Nothing: function(transformNothing, transformJust) {
assertFunction('Maybe.Nothing#fold', transformNothing);
assertFunction('Maybe.Nothing#fold', transformJust);
return transformNothing();
},
/*~*/
Just: function(transformNothing, transformJust) {
assertFunction('Maybe.Just#fold', transformNothing);
assertFunction('Maybe.Just#fold', transformJust);
return transformJust(this.value);
}
}