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
|
from collections.abc import Callable
from typing import TypeVar, overload
from returns.context import NoDeps
from returns.interfaces.failable import DiverseFailableN, SingleFailableN
from returns.methods.cond import internal_cond
from returns.primitives.hkt import Kinded, KindN
_ValueType = TypeVar('_ValueType')
_ErrorType = TypeVar('_ErrorType')
_DiverseFailableKind = TypeVar('_DiverseFailableKind', bound=DiverseFailableN)
_SingleFailableKind = TypeVar('_SingleFailableKind', bound=SingleFailableN)
@overload
def cond(
container_type: type[_SingleFailableKind],
success_value: _ValueType,
) -> Kinded[
Callable[
[bool],
KindN[_SingleFailableKind, _ValueType, _ErrorType, NoDeps],
]
]: ...
@overload
def cond(
container_type: type[_DiverseFailableKind],
success_value: _ValueType,
error_value: _ErrorType,
) -> Kinded[
Callable[
[bool],
KindN[_DiverseFailableKind, _ValueType, _ErrorType, NoDeps],
]
]: ...
def cond(
container_type: (type[_SingleFailableKind] | type[_DiverseFailableKind]),
success_value: _ValueType,
error_value: _ErrorType | None = None,
):
"""
Reduce the boilerplate when choosing paths.
Works with ``SingleFailableN`` (e.g. ``Maybe``)
and ``DiverseFailableN`` (e.g. ``Result``).
Example using ``cond`` with the ``Result`` container:
.. code:: python
>>> from returns.pointfree import cond
>>> from returns.result import Failure, Result, Success
>>> assert cond(Result, 'success', 'failure')(True) == Success('success')
>>> assert cond(Result, 'success', 'failure')(False) == Failure('failure')
Example using ``cond`` with the ``Maybe`` container:
.. code:: python
>>> from returns.maybe import Maybe, Some, Nothing
>>> assert cond(Maybe, 10.0)(True) == Some(10.0)
>>> assert cond(Maybe, 10.0)(False) == Nothing
"""
def factory(is_success: bool): # noqa: FBT001
return internal_cond(
container_type,
is_success,
success_value,
error_value,
)
return factory
|