Example #1
0
def internal_bind_io(
    container: KindN[_IOBasedKind, _FirstType, _SecondType, _ThirdType],
    function: Callable[[_FirstType], 'IO[_UpdatedType]'],
) -> KindN[_IOBasedKind, _UpdatedType, _SecondType, _ThirdType]:
    """
    Bind a ``IO`` returning function over a container.

    It is not marked as ``@kinded``, because this function is intended
    to be used inside a kinded context:
    like in :func:`returns.pointfree.bind.bind`.
    It returns ``KindN[]`` instance, not a real type.

    If you wish to use the user-facing ``bind_io``
    that infers the return type correctly,
    use :func:`~bind_io` function instead.

    .. code:: python

      >>> from returns.methods.bind_io import bind_io
      >>> from returns.io import IO

      >>> def example(argument: int) -> IO[int]:
      ...     return IO(argument + 1)

      >>> assert bind_io(IO(1), example) == IO(2)

    Note, that this function works
    for all containers with ``.bind_io`` method.
    See :class:`returns.primitives.interfaces.specific.io.IOBasedN`
    for more info.

    """
    new_instance, rebound = debound(container)
    return rebound(new_instance.bind_io(function))
Example #2
0
def internal_rescue(
    container: KindN[_RescuableKind, _FirstType, _SecondType, _ThirdType],
    function: Callable[[_SecondType], KindN[_RescuableKind, _FirstType,
                                            _UpdatedType, _ThirdType], ],
) -> KindN[_RescuableKind, _FirstType, _UpdatedType, _ThirdType]:
    """
    Bind a function over a container. Works for the second type argument.

    It is not marked as ``@kinded``, because this function is intended
    to be used inside a kinded context:
    like in :func:`returns.pointfree.bind.bind`.
    It returns ``KindN[]`` instance, not a real type.

    If you wish to use the user-facing ``rescue``
    that infers the return type correctly,
    use :func:`~rescue` function instead.

    .. code:: python

      >>> from returns.methods.rescue import rescue
      >>> from returns.result import Result, Success, Failure

      >>> def example(argument: int) -> Result[str, int]:
      ...     return Failure(argument + 1)

      >>> assert rescue(Failure(1), example) == Failure(2)
      >>> assert rescue(Success('a'), example) == Success('a')

    Note, that this function works for all containers with ``.rescue`` method.
    See :class:`returns.primitives.interfaces.rescuable.RescuableN`
    for more info.

    """
    new_instance, rebound = debound(container)
    return rebound(new_instance.rescue(function))
Example #3
0
def internal_bind_ioresult(
    container: KindN[_IOResultBasedKind, _FirstType, _SecondType, _ThirdType],
    function: Callable[[_FirstType], 'IOResult[_UpdatedType, _SecondType]'],
) -> KindN[_IOResultBasedKind, _UpdatedType, _SecondType, _ThirdType]:
    """
    Bind a ``IOResult`` returning function over a container.

    It is not marked as ``@kinded``, because this function is intended
    to be used inside a kinded context:
    like in :func:`returns.pointfree.bind.bind`.
    It returns ``KindN[]`` instance, not a real type.

    If you wish to use the user-facing ``bind_ioresult``
    that infers the return type correctly,
    use :func:`~bind_ioresult` function instead.

    .. code:: python

      >>> import anyio
      >>> from returns.methods.bind_ioresult import bind_ioresult
      >>> from returns.io import IOResult, IOSuccess, IOFailure
      >>> from returns.future import FutureResult

      >>> def example(argument: int) -> IOResult[int, str]:
      ...     return IOSuccess(argument + 1)

      >>> assert bind_ioresult(IOSuccess(1), example) == IOSuccess(2)
      >>> assert bind_ioresult(IOFailure('a'), example) == IOFailure('a')

      >>> assert anyio.run(
      ...     bind_ioresult(FutureResult.from_value(1), example).awaitable,
      ... ) == IOSuccess(2)
      >>> assert anyio.run(
      ...     bind_ioresult(FutureResult.from_failure(1), example).awaitable,
      ... ) == IOFailure(1)

    Note, that this function works
    for all containers with ``.bind_ioresult`` method.
    See :class:`returns.primitives.interfaces.specific.ioresult.IOResultBasedN`
    for more info.

    """
    new_instance, rebound = debound(container)
    return rebound(new_instance.bind_ioresult(function))
Example #4
0
def internal_apply(
    container: KindN[_ApplicativeKind, _FirstType, _SecondType, _ThirdType],
    other: KindN[
        _ApplicativeKind,
        Callable[[_FirstType], _UpdatedType],
        _SecondType,
        _ThirdType,
    ],
) -> KindN[_ApplicativeKind, _UpdatedType, _SecondType, _ThirdType]:
    """
    Calls a wrapped function in a container on the first container.

    It is not marked as ``@kinded``, because this function is intended
    to be used inside a kinded context:
    like in :func:`returns.pointfree.apply.apply`.
    It returns ``KindN[]`` instance, not a real type.

    If you wish to use the user-facing ``apply``
    that infers the return type correctly,
    use :func:`~bind` function instead.

    .. code:: python

      >>> from returns.methods.apply import internal_apply, apply
      >>> from returns.maybe import Some, Nothing

      >>> def example(argument: int) -> int:
      ...     return argument + 1

      >>> applied = apply(Some(1), Some(example))
      >>> assert applied == internal_apply(Some(1), Some(example)) == Some(2)

      >>> applied = apply(Nothing, Some(example))
      >>> assert applied == internal_apply(Nothing, Some(example)) == Nothing

    Note, that this function works for all containers with ``.apply`` method.
    See :class:`returns.primitives.interfaces.Applicative.ApplicativeN`
    for more info.

    """
    new_instance, rebound = debound(container)
    return rebound(new_instance.apply(other))
Example #5
0
def internal_bind(
    container: KindN[_BindableKind, _FirstType, _SecondType, _ThirdType],
    function: Callable[[_FirstType], KindN[_BindableKind, _UpdatedType,
                                           _SecondType, _ThirdType], ],
) -> KindN[_BindableKind, _UpdatedType, _SecondType, _ThirdType]:
    """
    Bind a function over a container.

    It is not marked as ``@kinded``, because this function is intended
    to be used inside a kinded context:
    like in :func:`returns.pointfree.bind.bind`.
    It returns ``KindN[]`` instance, not a real type.

    If you wish to use the user-facing ``bind``
    that infers the return type correctly,
    use :func:`~bind` function instead.

    .. code:: python

      >>> from returns.methods.bind import internal_bind, bind
      >>> from returns.maybe import Maybe, Some, Nothing

      >>> def example(argument: int) -> Maybe[int]:
      ...     return Some(argument + 1)

      >>> bound = bind(Some(1), example)
      >>> assert bound == internal_bind(Some(1), example) == Some(2)

      >>> bound = bind(Nothing, example)
      >>> assert bound == internal_bind(Nothing, example) == Nothing

    Note, that this function works for all containers with ``.bind`` method.
    See :class:`returns.primitives.interfaces.bindable.BindableN` for more info.

    """
    new_instance, rebound = debound(container)
    return rebound(new_instance.bind(function))