| |
- pymonad.monad.Monad(typing.Generic)
-
- MonadOperators
class MonadOperators(pymonad.monad.Monad) |
|
MonadOperators(*args, **kwds)
Defines MonadOperators class.
MonadOperators is a MonadAlias which is used to add operators for
map (*), amap (&), and bind (>>) methods to Monad classes. |
|
- Method resolution order:
- MonadOperators
- pymonad.monad.Monad
- typing.Generic
- builtins.object
Methods defined here:
- __and__(self, monad_value)
- __rmul__(self, function)
- __rshift__(self, kleisli_function)
Data and other attributes defined here:
- __parameters__ = ()
Methods inherited from pymonad.monad.Monad:
- __init__(self, value, monoid)
- Initializes the internal values of the monad instance.
All monads can be expressed as a tuple, (a, m). Representing
all monads internally in this canonical form allows for some
interesting effects such as easily aliasing existing monads
instances and, if desired, adding operators. Occasionally it
also makes implementation of the monad methods itself easier.
Args:
value: if we think of monads as storing some data of
interest plus some 'meta data', then 'value' is the data of
interest. Exactly what 'value' is/means will depend on the
specific context of the monad in question.
monoid: this is the 'meta data' part. While implementers may
use an instance of the Monoid class here it is not
required. However, the value passed in here should be a type
that can be treated as a monoid, such as integers; strings;
lists; etc., in order to ensure that the monad laws are
obeyed. This is not enforced but it will result in an
incorrect implementation.
- amap(self: 'Monad[Callable[[S], T]]', monad_value: 'Monad[S]') -> 'Monad[T]'
- Applies the function stored in the functor to the value inside 'functor_value'
returning a new functor value.
- bind(self: 'Monad[S]', kleisli_function: Callable[[~S], ForwardRef('Monad[T]')]) -> 'Monad[T]'
- Applies 'function' to the result of a previous monadic calculation.
- join(self: 'Monad[Monad[T]]') -> 'Monad[T]'
- Unpacks a nested monad instance one level.
- map(self: 'Monad[S]', function: Callable[[~S], ~T]) -> 'Monad[T]'
- Applies 'function' to the contents of the functor and returns a new functor value.
- then(self: 'Monad[S]', function: Union[Callable[[~S], ~T], Callable[[~S], ForwardRef('Monad[T]')]]) -> 'Monad[T]'
- Combines the functionality of bind and fmap.
Instead of worrying about whether to use bind or fmap,
users can just use the then method to chain function
calls together. The then method uses attempts to use
bind first and if that doesn't work, uses fmap
instead.
Args:
function: A python function or lambda expression
which returns either a build-in type (int, string,
etc.) or an appropriate monad type (Maybe, Either,
etc.)
Returns:
A monad value of the same type as 'self'
Class methods inherited from pymonad.monad.Monad:
- apply(function) from builtins.type
- Supplies a cleaner interface for applicative functor/amap usage.
Example:
@curry(2)
def add(a, b): return a + b
x = Just(1)
y = Just(2)
(Maybe.apply(add)
.to_arguments(x, y)
) # results in Just(3)
Args:
function: A regular function which returns non-monadic values.
Returns:
A monad object based on the input class with the wrapped
function and a new method, 'to_arguments' which will apply
the function.
- insert(value: ~T) -> 'Monad[T]' from builtins.type
- Returns an instance of the Functor with 'value' in a minimum context.
Data descriptors inherited from pymonad.monad.Monad:
- __dict__
- dictionary for instance variables (if defined)
- __weakref__
- list of weak references to the object (if defined)
Data and other attributes inherited from pymonad.monad.Monad:
- __orig_bases__ = (typing.Generic[~T],)
Class methods inherited from typing.Generic:
- __class_getitem__(params) from builtins.type
- __init_subclass__(*args, **kwargs) from builtins.type
- This method is called when a class is subclassed.
The default implementation does nothing. It may be
overridden to extend subclasses.
Static methods inherited from typing.Generic:
- __new__(cls, *args, **kwds)
- Create and return a new object. See help(type) for accurate signature.
| |