Map over a Result
instance as in map
and get out the
value if result
is an Ok
, or return a default value if result
is an Err
.
import { ok, err, mapOr } from 'true-myth/result';
const length = (s: string) => s.length;
const anOkString = ok('a string');
const theStringLength = mapOr(0, anOkString);
console.log(theStringLength); // 8
const anErr = err('uh oh');
const anErrMapped = mapOr(0, anErr);
console.log(anErrMapped); // 0
Map over a Result
instance as in map
and get out the
value if result
is an Ok
, or return a default value if result
is an Err
.
import { ok, err, mapOr } from 'true-myth/result';
const length = (s: string) => s.length;
const anOkString = ok('a string');
const theStringLength = mapOr(0, anOkString);
console.log(theStringLength); // 8
const anErr = err('uh oh');
const anErrMapped = mapOr(0, anErr);
console.log(anErrMapped); // 0
The default value to use if result
is an Err
.
Map over a
Result
instance as inmap
and get out the value ifresult
is anOk
, or return a default value ifresult
is anErr
.Examples