Transforms a Maybe value using a function contained in another Maybe. As with
.map()
, the Maybe values are expected to be Just
, and no operation is
performed if any of them is a Nothing
.
forall a, b: (Maybe (a) => b).(Maybe a) => Maybe b
Transforms a Maybe value using a function contained in another Maybe. As with
.map()
, the Maybe values are expected to be Just
, and no operation is
performed if any of them is a Nothing
.
const Maybe = require('folktale/maybe');
function increment(value) {
return value + 1;
}
Maybe.Just(increment).apply(Maybe.Just(1));
// ==> Maybe.Just(2)
Maybe.Just(increment).apply(Maybe.Nothing());
// ==> Maybe.Nothing()
Maybe.Nothing().apply(Maybe.Just(1));
// ==> Maybe.Nothing()
{
/*~*/
Nothing: function apply(aMaybe) {
assertMaybe('Maybe.Nothing#apply', aMaybe);
return this;
},
/*~*/
Just: function apply(aMaybe) {
assertMaybe('Maybe.Just#apply', aMaybe);
return aMaybe.map(this.value);
}
}