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.list module¶
Adds operators to 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.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.state module¶
Adds operators to 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