1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
|
from collections.abc import Callable
from typing import TypeVar
from returns.interfaces.failable import DiverseFailableN
from returns.primitives.hkt import Kinded, KindN, kinded
_FirstType = TypeVar('_FirstType')
_NewFirstType = TypeVar('_NewFirstType')
_SecondType = TypeVar('_SecondType')
_NewSecondType = TypeVar('_NewSecondType')
_ThirdType = TypeVar('_ThirdType')
_NewThirdType = TypeVar('_NewThirdType')
_DiverseFailableKind = TypeVar('_DiverseFailableKind', bound=DiverseFailableN)
def unify( # noqa: WPS234
function: Callable[
[_FirstType],
KindN[
_DiverseFailableKind,
_NewFirstType,
_NewSecondType,
_NewThirdType,
],
],
) -> Kinded[
Callable[
[KindN[_DiverseFailableKind, _FirstType, _SecondType, _ThirdType]],
KindN[
_DiverseFailableKind,
_NewFirstType,
_SecondType | _NewSecondType,
_NewThirdType,
],
]
]:
"""
Composes successful container with a function that returns a container.
Similar to :func:`~returns.pointfree.bind` but has different type.
It returns ``Result[ValueType, Union[OldErrorType, NewErrorType]]``
instead of ``Result[ValueType, OldErrorType]``.
So, it can be more useful in some situations.
Probably with specific exceptions.
.. code:: python
>>> from returns.methods import cond
>>> from returns.pointfree import unify
>>> from returns.result import Result, Success, Failure
>>> def bindable(arg: int) -> Result[int, int]:
... return cond(Result, arg % 2 == 0, arg + 1, arg - 1)
>>> assert unify(bindable)(Success(2)) == Success(3)
>>> assert unify(bindable)(Success(1)) == Failure(0)
>>> assert unify(bindable)(Failure(42)) == Failure(42)
"""
@kinded
def factory(
container: KindN[
_DiverseFailableKind,
_FirstType,
_SecondType,
_ThirdType,
],
) -> KindN[
_DiverseFailableKind,
_NewFirstType,
_SecondType | _NewSecondType,
_NewThirdType,
]:
return container.bind(function) # type: ignore
return factory
|