Applies a function to each case of a Result.
This API is still experimental, so it may change or be removed in future versions. You should not rely on it for production applications.
forall a, b, c:
(Result a b).((a) => c, (b) => c) => c
Applies a function to each case of a Result.
const Result = require('folktale/result');
const inc = (x) => x + 1;
const dec = (x) => x - 1;
Result.Error(1).fold(inc, dec);
// ==> inc(1)
Result.Ok(1).fold(inc, dec);
// ==> dec(1)
{
/*~*/
Error: function fold(f, g) {
assertFunction('Result.Error#fold', f);
assertFunction('Result.Error#fold', g);
return f(this.value);
},
/*~*/
Ok: function fold(f, g) {
assertFunction('Result.Ok#fold', f);
assertFunction('Result.Ok#fold', g);
return g(this.value);
}
}