| |
- pymonad.monad.Monad(typing.Generic)
-
- Maybe(pymonad.monad.Monad, typing.Generic)
-
- Option
- typing.Generic(builtins.object)
-
- Maybe(pymonad.monad.Monad, typing.Generic)
-
- Option
class Maybe(pymonad.monad.Monad, typing.Generic) |
|
Maybe(*args, **kwds)
The Maybe monad class. |
|
- Method resolution order:
- Maybe
- 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: 'Maybe[Callable[[S], T]]', monad_value: 'Maybe[S]') -> 'Maybe[T]'
- See Monad.amap
- bind(self: 'Maybe[S]', kleisli_function: 'Callable[[S], Maybe[T]]') -> 'Maybe[T]'
- See Monad.bind
- is_just(self) -> bool
- Returns True if the monad instance was created with the 'Just' function.
- is_nothing(self) -> bool
- Returns True if the monad instance is the 'Nothing' value.
- map(self: 'Maybe[S]', function: Callable[[~S], ~T]) -> 'Maybe[T]'
- See Monad.map
- maybe(self: 'Maybe[S]', default_value: ~T, extraction_function: Callable[[~S], ~T]) -> ~T
- Extracts a bare value from a Maybe object.
'maybe' takes a default value and a function. If the Maybe
object is Nothing then the default value is returned,
otherwise the result of running the function on the contained
value is returned.
Example 1:
m = (Maybe.insert(1)
.then(add(7))
.then(div(0)) # Returns a Nothing value
.then(mul(5))
.maybe(0, lambda x: x)
) # 'm' takes the value 0
Args:
default_value: a value (of type T) to be returned if the
Maybe object is Nothing.
extraction_function: a function from type S to type T where
S is the type of the value contained in the Maybe object.
Result:
A bare (non-monadic) value of type T.
- option = maybe(self: 'Maybe[S]', default_value: ~T, extraction_function: Callable[[~S], ~T]) -> ~T
Class methods defined here:
- insert(value: ~T) -> 'Maybe[T]' from builtins.type
- See Monad.insert
Data and other attributes defined here:
- __hash__ = None
- __orig_bases__ = (<class 'pymonad.monad.Monad'>, typing.Generic[~T])
- __parameters__ = (~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.
|
class Option(Maybe) |
|
Option(*args, **kwds)
An alias for the Maybe monad class. |
|
- Method resolution order:
- Option
- Maybe
- pymonad.monad.Monad
- typing.Generic
- builtins.object
Methods defined here:
- __repr__(self)
- Return repr(self).
Data and other attributes defined here:
- __orig_bases__ = (pymonad.maybe.Maybe[~T],)
- __parameters__ = (~T,)
Methods inherited from Maybe:
- __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.
- amap(self: 'Maybe[Callable[[S], T]]', monad_value: 'Maybe[S]') -> 'Maybe[T]'
- See Monad.amap
- bind(self: 'Maybe[S]', kleisli_function: 'Callable[[S], Maybe[T]]') -> 'Maybe[T]'
- See Monad.bind
- is_just(self) -> bool
- Returns True if the monad instance was created with the 'Just' function.
- is_nothing(self) -> bool
- Returns True if the monad instance is the 'Nothing' value.
- map(self: 'Maybe[S]', function: Callable[[~S], ~T]) -> 'Maybe[T]'
- See Monad.map
- maybe(self: 'Maybe[S]', default_value: ~T, extraction_function: Callable[[~S], ~T]) -> ~T
- Extracts a bare value from a Maybe object.
'maybe' takes a default value and a function. If the Maybe
object is Nothing then the default value is returned,
otherwise the result of running the function on the contained
value is returned.
Example 1:
m = (Maybe.insert(1)
.then(add(7))
.then(div(0)) # Returns a Nothing value
.then(mul(5))
.maybe(0, lambda x: x)
) # 'm' takes the value 0
Args:
default_value: a value (of type T) to be returned if the
Maybe object is Nothing.
extraction_function: a function from type S to type T where
S is the type of the value contained in the Maybe object.
Result:
A bare (non-monadic) value of type T.
- option = maybe(self: 'Maybe[S]', default_value: ~T, extraction_function: Callable[[~S], ~T]) -> ~T
Class methods inherited from Maybe:
- insert(value: ~T) -> 'Maybe[T]' from builtins.type
- See Monad.insert
Data and other attributes inherited from Maybe:
- __hash__ = None
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.
| |