flat_map

flat_map

flat_map(f, r)[source]

Enables the chaining computations that can error

Example

>>> from math import sqrt
>>> @wrap(ValueError)
... def parse(s: str) -> float:
...    return float(s)
>>> @wrap(ValueError)
... def my_sqrt(f: float) -> float:
...    return sqrt(f)
>>> flat_map(my_sqrt, parse("2"))
1.414...
>>> flat_map(my_sqrt, parse("-1"))
Err1(msg='ValueError thrown: math domain error', contexts=[])
>>> flat_map(my_sqrt, parse("invalid"))
Err1(msg="ValueError thrown: could not convert string to float: 'invalid'", contexts=[])
Parameters
  • f (Callable[[TypeVar(_Value_co, covariant=True)], Union[TypeVar(_ReturnType), Err]]) – Function that takes a result parameterized by type _Value and returns a result parameterized by type _ReturnType

  • r (Union[TypeVar(_Value_co, covariant=True), Err]) – Result parameterized by type _Value

Return type

Union[TypeVar(_ReturnType), Err]

Returns

Result parameterized by type _ReturnType

See also

map()