Applies the function contained in one Result to the value in another Result.
Application only occurs if both Results are Ok
, otherwise keeps the first
Error
.
forall a, b, c:
(Result a ((b) => c)).(Result a b) => Result a c
Applies the function contained in one Result to the value in another Result.
Application only occurs if both Results are Ok
, otherwise keeps the first
Error
.
Note that Result.Ok(x => x + 1).apply(Result.Ok(1))
is equivalent to
Result.Ok(1).map(x => x + 1)
.
const Result = require('folktale/result');
function increment(value) {
return value + 1;
}
Result.Ok(increment).apply(Result.Ok(1));
// ==> Result.Ok(2)
Result.Ok(increment).apply(Result.Error(1));
// ==> Result.Error(1)
Result.Error(increment).apply(Result.Ok(1));
// ==> Result.Error(increment)
Result.Error(increment).apply(Result.Error(1));
// ==> Result.Error(increment)
{
/*~*/
Error: function apply(anResult) {
assertResult('Result.Error#apply', anResult);
return this;
},
/*~*/
Ok: function apply(anResult) {
assertResult('Result.Ok#apply', anResult);
return anResult.map(this.value);
}
}