pymonad.operators package

Submodules

pymonad.operators.either module

Adds operators to the Either monad.

class pymonad.operators.either.Either(value, monoid)[source]

Bases: pymonad.operators.operators.MonadOperators, pymonad.either.Either

See pymonad.operators.operators and pymonad.either.

pymonad.operators.either.Error(value: S) → pymonad.operators.either._Error[S, Any][source]

Creates an error value as the result of a calculation.

pymonad.operators.either.Left(value: S) → pymonad.operators.either.Either[S, Any][source]

Creates a value of the first possible type in the Either monad.

pymonad.operators.either.Result(value: T) → pymonad.operators.either._Error[Any, T][source]

Creates a value representing the successful result of a calculation.

pymonad.operators.either.Right(value: T) → pymonad.operators.either.Either[Any, T][source]

Creates a value of the second possible type in the Either monad.

pymonad.operators.list module

Adds operators to the List monad.

pymonad.operators.list.ListMonad(*elements) → pymonad.operators.list._List[T][source]

Creates an instance of the List monad.

Args:
elements: any number of elements to be inserted into the list
Returns:
An instance of the List monad.

pymonad.operators.maybe module

Adds operators to the Maybe monad.

pymonad.operators.maybe.Just(value: T) → pymonad.operators.maybe.Maybe[T][source]

A Maybe object representing the presence of an optional value.

class pymonad.operators.maybe.Maybe(value, monoid)[source]

Bases: pymonad.operators.operators.MonadOperators, pymonad.maybe.Maybe

See pymonad.operators.operators and pymonad.maybe.

class pymonad.operators.maybe.Option(value, monoid)[source]

Bases: pymonad.operators.maybe.Maybe

An alias for the Maybe monad class.

pymonad.operators.maybe.Some(value: T) → pymonad.operators.maybe.Option[T][source]

An Option object representing the presence of an optional value.

pymonad.operators.operators module

Defines the monad operators.

The MonadOperators class allows users to create a monad alias for any monad class with the operators __and__, __rmul__, and __rshift__ defined.

Example:
>>> class MaybeOp(MonadOperators, Maybe):
>>>     pass

Nothing extra needs to be implemented to get the correct behaviour unless the monad has multiple ‘constructors’ such as Just and Nothing. In this case, we need to override those constructors as well:

Example:
>>> def Just(value):
>>>     return MaybeOp(value, True)
>>> Nothing = MaybeOp(None, False)
class pymonad.operators.operators.MonadOperators(value, monoid)[source]

Bases: pymonad.monad.MonadAlias

Defines MonadOperators class.

MonadOperators is a MonadAlias which is used to add operators for map (<<), amap (&), bind (>>), and then (*) methods to Monad classes.

pymonad.operators.reader module

Adds operators to the Reader monad.

pymonad.operators.reader.Reader(function: Callable[R, T]) → pymonad.operators.reader._Reader[R, T][source]

Creates an instance of the Reader monad.

Args:
function: a function which takes the read-only data as input and
returns any appropriate type.
Result:
An instance of the Reader monad.

pymonad.operators.state module

Adds operators to the State monad.

pymonad.operators.state.State(state_function: Callable[S, Tuple[T, S]]) → pymonad.operators.state._State[S, T][source]

The State monad constructor function.

Args:
state_function: a function with type State -> (Any, State)
Returns:
An instance of the State monad.

pymonad.operators.writer module

Adds operators to the Writer monad.

class pymonad.operators.writer.Writer(value, monoid)[source]

Bases: pymonad.operators.operators.MonadOperators, pymonad.writer.Writer

See pymonad.operators.operators and pymonad.writer.

Module contents

Provides monad classes with operators.

The operators module overrides all base monad types and their aliases with versions which support operators.

The defined operators are:
  • & - amap
  • >> - bind
  • << - map
  • * - then
Example:
>>> from pymonad.operators import Maybe, Just, Nothing
>>> from pymonad.tools import curry
>>> @curry(2)
>>> def add(x, y): return x + y
>>> @curry(2)
>>> def div(y, x):
>>>     if y == 0:
>>>         return Nothing
>>>     else:
>>>         return Just(x / y)
>>> # Equivalent to Maybe.apply(add).to_arguments(Just(1), Just(2))
>>> print(add << Just(1) & Just(2)) # Just 3
>>>
>>> # Equivalent to Maybe.insert(5).bind(div(2)).bind(div(0))
>>> print(Just(5) >> div(2) >> div(0)) # Nothing
>>>
>>> # Alternatively, each could be written with *
>>> print(add * Just(1) & Just(2)) # Just 3
>>> print(Just(5) * div(2) * div(0)) # Nothing