Skip to content

True Myth / result / mapOr

Function: mapOr()

Call Signature

mapOr<T, U, E>(orU, mapFn, result): U

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.

Examples

ts
import { ok, err, mapOr } from 'true-myth/result';

const length = (s: string) => s.length;

const anOkString = ok('a string');
const theStringLength = mapOr(0, length, anOkString);
console.log(theStringLength);  // 8

const anErr = err('uh oh');
const anErrMapped = mapOr(0, length, anErr);
console.log(anErrMapped);  // 0

Type Parameters

T

T

U

U

E

E

Parameters

orU

U

The default value to use if result is an Err.

mapFn

(t) => U

The function to apply the value to if result is an Ok.

result

Result<T, E>

The Result instance to map over.

Returns

U

Call Signature

mapOr<T, U, E>(orU, mapFn): (result) => U

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.

Examples

ts
import { ok, err, mapOr } from 'true-myth/result';

const length = (s: string) => s.length;

const anOkString = ok('a string');
const theStringLength = mapOr(0, length, anOkString);
console.log(theStringLength);  // 8

const anErr = err('uh oh');
const anErrMapped = mapOr(0, length, anErr);
console.log(anErrMapped);  // 0

Type Parameters

T

T

U

U

E

E

Parameters

orU

U

The default value to use if result is an Err.

mapFn

(t) => U

The function to apply the value to if result is an Ok.

Returns

(result): U

Parameters

result

Result<T, E>

Returns

U

Call Signature

mapOr<T, U, E>(orU): (mapFn) => (result) => U

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.

Examples

ts
import { ok, err, mapOr } from 'true-myth/result';

const length = (s: string) => s.length;

const anOkString = ok('a string');
const theStringLength = mapOr(0, length, anOkString);
console.log(theStringLength);  // 8

const anErr = err('uh oh');
const anErrMapped = mapOr(0, length, anErr);
console.log(anErrMapped);  // 0

Type Parameters

T

T

U

U

E

E

Parameters

orU

U

The default value to use if result is an Err.

Returns

(mapFn): (result) => U

Parameters

mapFn

(t) => U

Returns

(result): U

Parameters
result

Result<T, E>

Returns

U