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 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
|
from __future__ import annotations
from collections.abc import Callable
from typing import TYPE_CHECKING, TypeVar
from returns.interfaces.specific.reader import ReaderLike2, ReaderLike3
from returns.primitives.hkt import Kind2, Kind3, Kinded, kinded
if TYPE_CHECKING:
from returns.context import RequiresContext # noqa: WPS433
_FirstType = TypeVar('_FirstType')
_SecondType = TypeVar('_SecondType')
_ThirdType = TypeVar('_ThirdType')
_UpdatedType = TypeVar('_UpdatedType')
_Reader2Kind = TypeVar('_Reader2Kind', bound=ReaderLike2)
_Reader3Kind = TypeVar('_Reader3Kind', bound=ReaderLike3)
def bind_context2(
function: Callable[
[_FirstType],
RequiresContext[_UpdatedType, _SecondType],
],
) -> Kinded[
Callable[
[Kind2[_Reader2Kind, _FirstType, _SecondType]],
Kind2[_Reader2Kind, _UpdatedType, _SecondType],
]
]:
"""
Composes successful container with a function that returns a container.
In other words, it modifies the function's
signature from:
``a -> RequresContext[b, c]``
to:
``Container[a, c] -> Container[b, c]``
.. code:: python
>>> from returns.pointfree import bind_context2
>>> from returns.context import Reader
>>> def example(argument: int) -> Reader[int, int]:
... return Reader(lambda deps: argument + deps)
>>> assert bind_context2(example)(Reader.from_value(2))(3) == 5
Note, that this function works with only ``Kind2`` containers
with ``.bind_context`` method.
See :class:`returns.primitives.interfaces.specific.reader.ReaderLike2`
for more info.
"""
@kinded
def factory(
container: Kind2[_Reader2Kind, _FirstType, _SecondType],
) -> Kind2[_Reader2Kind, _UpdatedType, _SecondType]:
return container.bind_context(function)
return factory
def bind_context3(
function: Callable[
[_FirstType],
RequiresContext[_UpdatedType, _ThirdType],
],
) -> Kinded[
Callable[
[Kind3[_Reader3Kind, _FirstType, _SecondType, _ThirdType]],
Kind3[_Reader3Kind, _UpdatedType, _SecondType, _ThirdType],
]
]:
"""
Composes successful container with a function that returns a container.
In other words, it modifies the function's
signature from: ``a -> RequresContext[b, c]``
to: ``Container[a, c] -> Container[b, c]``
.. code:: python
>>> from returns.context import RequiresContext, RequiresContextResult
>>> from returns.result import Success, Failure
>>> from returns.pointfree import bind_context
>>> def function(arg: int) -> RequiresContext[str, int]:
... return RequiresContext(lambda deps: len(deps) + arg)
>>> assert bind_context(function)(
... RequiresContextResult.from_value(2),
... )('abc') == Success(5)
>>> assert bind_context(function)(
... RequiresContextResult.from_failure(0),
... )('abc') == Failure(0)
Note, that this function works with only ``Kind3`` containers
with ``.bind_context`` method.
See :class:`returns.primitives.interfaces.specific.reader.ReaderLike3`
for more info.
"""
@kinded
def factory(
container: Kind3[_Reader3Kind, _FirstType, _SecondType, _ThirdType],
) -> Kind3[_Reader3Kind, _UpdatedType, _SecondType, _ThirdType]:
return container.bind_context(function)
return factory
#: Useful alias for :func:`~bind_context3`.
bind_context = bind_context3
|