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
|
from collections.abc import Callable
from typing import TypeVar
from returns.interfaces.altable import AltableN
from returns.primitives.hkt import Kinded, KindN, kinded
_FirstType = TypeVar('_FirstType')
_SecondType = TypeVar('_SecondType')
_ThirdType = TypeVar('_ThirdType')
_UpdatedType = TypeVar('_UpdatedType')
_AltableKind = TypeVar('_AltableKind', bound=AltableN)
def alt(
function: Callable[[_SecondType], _UpdatedType],
) -> Kinded[
Callable[
[KindN[_AltableKind, _FirstType, _SecondType, _ThirdType]],
KindN[_AltableKind, _FirstType, _UpdatedType, _ThirdType],
]
]:
"""
Lifts function to be wrapped in a container for better composition.
In other words, it modifies the function's
signature from:
``a -> b``
to:
``Container[a] -> Container[b]``
This is how it should be used:
.. code:: python
>>> from returns.io import IOFailure, IOSuccess
>>> from returns.pointfree import alt
>>> def example(argument: int) -> float:
... return argument / 2
>>> assert alt(example)(IOSuccess(1)) == IOSuccess(1)
>>> assert alt(example)(IOFailure(4)) == IOFailure(2.0)
Note, that this function works for all containers with ``.alt`` method.
See :class:`returns.primitives.interfaces.altable.AltableN` for more info.
"""
@kinded
def factory(
container: KindN[_AltableKind, _FirstType, _SecondType, _ThirdType],
) -> KindN[_AltableKind, _FirstType, _UpdatedType, _ThirdType]:
return container.alt(function)
return factory
|