The input UnknownOptional type.
The value type of the Optional returned by the function.
The result of applying the function, or None.
const parseNumber = (input: string): Optional<number> => {
const num = Number.parseInt(input, 10);
return Number.isNaN(num) ? Optional.none : Optional.some(num);
};
const parsed = Optional.flatMap(Optional.some('10'), parseNumber);
assert.deepStrictEqual(parsed, Optional.some(10));
const flatMapParse = Optional.flatMap(parseNumber);
assert.deepStrictEqual(flatMapParse(Optional.some('5')), Optional.some(5));
assert.deepStrictEqual(flatMapParse(Optional.some('invalid')), Optional.none);
Applies a function that returns an Optional to the value in an
Some. If the input is None, returns None.
This is the monadic bind operation for Optional.
The value type of the Optional returned by the function.
The result of applying the function, or None.
const parseNumber = (input: string): Optional<number> => {
const num = Number.parseInt(input, 10);
return Number.isNaN(num) ? Optional.none : Optional.some(num);
};
const parsed = Optional.flatMap(Optional.some('10'), parseNumber);
assert.deepStrictEqual(parsed, Optional.some(10));
const flatMapParse = Optional.flatMap(parseNumber);
assert.deepStrictEqual(flatMapParse(Optional.some('5')), Optional.some(5));
assert.deepStrictEqual(flatMapParse(Optional.some('invalid')), Optional.none);
Applies a function that returns an
Optionalto the value in anSome. If the input isNone, returnsNone. This is the monadic bind operation forOptional.