| |
- pymonad.monad.Monad(typing.Generic)
-
- Either(pymonad.monad.Monad, typing.Generic)
- typing.Generic(builtins.object)
-
- Either(pymonad.monad.Monad, typing.Generic)
class Either(pymonad.monad.Monad, typing.Generic) |
|
Either(*args, **kwds)
The Either monad class. |
|
- Method resolution order:
- Either
- pymonad.monad.Monad
- typing.Generic
- builtins.object
Methods defined here:
- __eq__(self, other)
- Checks equality of Maybe objects.
Maybe objects are equal iff:
1. They are both Nothing, or
2. They are both Just and
2a. They both contain the same value.
- __repr__(self)
- Return repr(self).
- amap(self: 'Either[M, Callable[[S], T]]', monad_value: 'Either[M, S]') -> 'Either[M, T]'
- Applies the function stored in the functor to the value inside 'functor_value'
returning a new functor value.
- bind(self: 'Either[M, S]', kleisli_function: Callable[[~S], ForwardRef('Either[M, T]')]) -> 'Either[M, T]'
- See Monad.bind
- either(self: 'Either[M, S]', left_function: Callable[[~M], ~T], right_function: Callable[[~S], ~T]) -> ~T
- Extracts a bare value from an Either object.
'either' takes two functions. If the Either object is a 'Left'
then the first (left) function is called with the Left value
as its argument. Otherwise the second (right) function is
called with the Right value as its argument.
Example:
e = (Either.insert(1)
.then(add(7))
.then(div(0)) # Returns a Left with a ZeroDivisionError
.then(mul(5))
.either(lambda e: 0, lambda x: x) # ignore the error and return 0
) # 'e' takes the value 0
Args:
left_function: a function from M to T where M is the type
contained by Left values.
right_function: a function from S to T where S is the type
contained in by Right values.
Result:
A bare (non-monadic) value of type T.
- is_left(self) -> bool
- Returns True if this Either instance was created with the 'Left' function.
- is_right(self) -> bool
- Returns True if this Either instance was created with the 'Right' function.
- map(self: 'Either[M, S]', function: Callable[[~S], ~T]) -> 'Either[M, T]'
- See Monad.map
Class methods defined here:
- insert(value: ~T) -> 'Either[Any, T]' from builtins.type
- See Monad.insert
Data and other attributes defined here:
- __hash__ = None
- __orig_bases__ = (<class 'pymonad.monad.Monad'>, typing.Generic[~M, ~T])
- __parameters__ = (~M, ~T)
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.
- join(self: 'Monad[Monad[T]]') -> 'Monad[T]'
- Unpacks a nested monad instance one level.
- 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.
Data descriptors inherited from pymonad.monad.Monad:
- __dict__
- dictionary for instance variables (if defined)
- __weakref__
- list of weak references to the object (if defined)
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.
| |