Pointfree

This module provides a bunch of primitives to work with containers.

It is centered around the composition idea. Sometimes using methods on containers is not very helpful. Instead we can use functions that has the reverse semantics, but the same result.

bind

Without bind() function it would be very hard to declaratively compose two entities:

  1. Existings container

  2. Existing functions that accepts a regular value and returns a container

We can compose these entities with .bind when calling it directly, but how can we do it inversevely?

>>> from returns.pointfree import bind
>>> from returns.maybe import Maybe, Some

>>> def bindable(arg: str) -> Maybe[int]:
...     return Some(1)

>>> container: Maybe[str] = Some('a')
>>> # We now have two way of composining these entities.
>>> # 1. Via ``.bind``:
>>> assert container.bind(bindable) == Some(1)
>>> # 2. Or via ``bind`` function, the same but in the inverse way:
>>> assert bind(bindable)(container) == Some(1)

That’s it.

rescue

The same applies for rescue() function. It is also required for better declarative programming.

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

>>> def function(arg: str) -> Result[int, str]:
...     return Success(1)

>>> container: Result[int, str] = Failure('a')
>>> # We now have two way of composining these entities.
>>> # 1. Via ``.rescue``:
>>> assert container.rescue(function) == Success(1)
>>> # 2. Or via ``rescue`` function, the same but in the inverse way:
>>> assert rescue(function)(container) == Success(1)

API Reference

bind(function)

Boxes function’s input parameter from a regular value to a container.

In other words, it modifies the function signature from: a -> Container[b] to: Container[a] -> Container[b]

Similar to returns.pointfree.rescue(), but works for successful containers.

This is how it should be used:

>>> from returns.pointfree import bind
>>> from returns.maybe import Maybe, Some, Nothing

>>> def example(argument: int) -> Maybe[int]:
...     return Some(argument + 1)
...
>>> assert bind(example)(Some(1)) == Some(2)
>>> assert bind(example)(Nothing) == Nothing

Note, that this function works for all containers with .bind method. See returns.primitives.interfaces.Bindable for more info.

rescue(function)

Boxes function’s input parameter from a regular value to a container.

In other words, it modifies the function signature from: a -> Container[b] to: Container[a] -> Container[b]

Similar to returns.pointfree.bind(), but works for failed containers.

This is how it should be used:

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

>>> def example(argument: int) -> Result[str, int]:
...     return Success(argument + 1)
...
>>> assert rescue(example)(Success('a')) == Success('a')
>>> assert rescue(example)(Failure(1)) == Success(2)

Note, that this function works for all containers with .rescue method. See returns.primitives.interfaces.Rescueable for more info.