Monadic API
Monad
- class monadic.Monad
Monad interface.
This is an abstract base class for monads. Monads are wrapper types that allow for chaining operations together.
Subclasses of this class must implement two methods:
unit(a class method)This method is a constructor that wraps a single value
bind(an instance method)This method takes a function that maps a value to a monad and returns a monad of the same type.
The interface is somewhat abstract. See
monadic.option.Optionas an example.- apply(f: Monad[Callable[[T], U]]) Monad[U]
Apply a monadic function to a monad.
This method can be overridden to provide a more efficient or alternative implementation.
Maybe
Maybe monads are a class of monads that represent values that may
or may not be present. Monadic exposes two Maybe monads,
Option and Result. Option represents a value
that may or may not be present, while Result represents the
result of an operation that may or may not have succeeded. The main
difference between the two is that Option carries no information
about why a value may not be present, while Result can carry an
error message explaining why an operation may have failed.
- class monadic.Maybe
Maybe monad interface.
Base class for monads that represent a value that may or may not exist. Subclasses must implement the following methods:
unit(a class method)This method is a constructor that wraps a single valid value
bind(an instance method)How to apply a function to a value that may or may not exist.
unwrap(an instance method)Retrieve the wrapped value. If the value does not exist, raise an
UnwrapError
__bool__(an instance method)Return whether the value exists.
- default(value: U) Maybe[T] | Maybe[U]
Return a default value if the value does not exist.
If the value exists, return the wrapped value; otherwise, return the wrapped default value.
- abstract classmethod unit(value: U) Maybe[U]
Wrap a value in the monad class.
- Parameters:
value (U) – The value to wrap.
- Returns:
The wrapped value.
- Return type:
Monad[U]
- abstract unwrap() T
Retrieve the wrapped value.
- Raises:
UnwrapError – If the value does not exist.
- Returns:
The wrapped value.
- Return type:
T
Option
- class monadic.Option
Bases:
Maybe[T],ABCMonadic type representing a value that may or may not exist.
This class is a subclass of
Maybethat implements theMonadinterface. It is a wrapper type that represents a value that may or may not exist. It has two subclasses: -Some-Nothing- apply(f: Option[Callable[[T], U]]) Option[U]
Apply a function wrapped in an
Option.If
selfandfare bothSomeinstances, apply the function to the wrapped value and wrap in aSome. Otherwise, returnNothing.
- abstract bind(f: Callable[[T], Option[U]]) Option[U]
Chain a function that maps a value to an
Option.If
selfis aSome, apply the function to the wrapped value. Otherwise (selfis aNothing), returnself.
- abstract map(f: Callable[[T], U]) Option[U]
Map a function over the wrapped value.
If
selfis aSome, apply the function to the wrapped value and wrap in aSome. Otherwise (selfis aNothing), returnNothing.- Parameters:
f (Callable[[T], U]) – A function that maps a value to another value.
- Returns:
The result of applying the function to the wrapped value.
- Return type:
Option[U]
Some
Nothing
Result
- class monadic.Result
Bases:
Maybe[T],ABCMonadic type representing the result of a computation that may fail.
This class is a subclass of
Maybethat implements theMonadinterface. It is a wrapper type that represents the result of a computation that may fail. It has two subclasses: -Ok-Error- apply(f: Result[Callable[[T], U]]) Result[U]
Apply a function wrapped in an
Result.If
selfandfare bothOkinstances, apply the function to the wrapped value and wrap in anOk. Otherwise, returnself.If an exception is raised while applying the function, return an
Errorwrapping the exception.- Parameters:
f (Result[Callable[[T], U]]) – The function to apply to the wrapped value
- static attempt(_Result__f: Callable[[...], T], _Result__catch: Type[Exception] | Tuple[Type[Exception], ...], *args, **kwargs) Result[T]
Attempt to call a function that may raise an exception.
If the function raises an exception, return an
Error- Parameters:
__f (Callable[..., T]) – The function to call
__catch (Union[Type[Exception], Tuple[Type[Exception], ...]]) – The types of exceptions to catch
*args – Positional arguments to pass to the function
**kwargs – Keyword arguments to pass to the function
- abstract bind(f: Callable[[T], Result[U]]) Result[U]
Chain a function that maps a value to a
Result.If
selfis anOk, apply the function to the wrapped value. Otherwise (selfis anError), returnself.- Parameters:
f (Callable[[T], Result[U]]) – The function to apply to the wrapped value
- map(f: Callable[[T], U]) Result[U]
Map a function over the wrapped value.
If
selfis anOk, apply the function to the wrapped value and wrap in anOk. Otherwise (selfis anError), returnself.If an exception is raised while applying the function, return an
Errorwrapping the exception.- Parameters:
f (Callable[[T], U]) – The function to apply to the wrapped value
Ok
Error
Iterables
Iterable monads are a class of monads that represent a collection
of values that can be iterated over. Monadic exposes three
Iterable monads, List, Set, and Dict.
These monads each map to their respective Python types, but follow a functional
style and are immutable.
- class monadic.Iterable
Iterable monad interface.
Base class for monads that represent a collection of values that may be iterated over. Subclasses must implement the following methods:
unit(a class method)This method is a constructor that wraps a single valid value
bind(an instance method)How to apply a function to a value that may or may not exist.
from_iterable(a class method)Wrap an iterable in the monad class.
empty(a class method)Create an empty monad.
__eq__(an instance method)Compare two monads for equality.
monadic.Iterableinstances are also Python iterables, so they can be used in for loops and comprehensions.- append(value: U) Iterable[T | U]
Add a value to this iterable.
- Parameters:
value (U) – The value to add.
- Returns:
A new iterable containing the old contents and the value.
- Return type:
Iterable[Union[T, U]]
- concat(other: Iterable[U]) Iterable[T | U]
Add the contents of another iterable to this iterable.
- Parameters:
other (Iterable[U]) – The iterable to add.
- Returns:
A new iterable containing the contents of both iterables.
- Return type:
Iterable[Union[T, U]]
- filter(f: Callable[[T], bool]) Iterable[T]
Filter the values in this iterable.
- Parameters:
f (Callable[[T], bool]) – A function that returns True for values to keep.
- Returns:
A new iterable containing only the values that pass the filter.
- Return type:
Iterable[T]
- fold(f: Callable[[U, T], U], initial: U | None = None) U
Fold the values in this iterable.
- Parameters:
f (Callable[[U, T], U]) – A function that takes the current value and the next value and returns the new value.
initial (Optional[U]) – The initial value to use. If not provided, the first value in the iterable is used.
- Returns:
The result of folding the iterable.
- Return type:
U
- abstract classmethod from_iterable(iterable: Iterable[U]) Iterable[U]
Wrap an iterable in a monad.
- Parameters:
iterable (Iterable) – The iterable to wrap.
- Returns:
A new monad containing the contents of the iterable.
- Return type:
- map(f: Callable[[T], U]) Iterable[U]
Map a function over an iterable.
- Parameters:
f (Callable[[T], U]) – A function to map over the iterable.
- Returns:
A new iterable containing the result of applying the function to each value in the iterable.
- Return type:
Iterable[U]
List
- class monadic.List(inner: Iterable[T])
Bases:
Iterable[T]A monadic list.
This is a wrapper around the built-in list type that implements the
Iterableinterface. Following a functional style, it is immutable. To add items to the end of the list, use theappend()orconcat().- Parameters:
inner (Iterable[T]) – The iterable to wrap as a list.
Examples
>>> from monadic.list import List >>> List([1, 2, 3]).map(lambda x: x + 1) List([2, 3, 4]) >>> List([1, 2, 3]).bind(lambda x: List([x, x + 1])) List([1, 2, 2, 3, 3, 4]) >>> List([1, 2, 3]).apply(List([str, float])) List(['1', 2.0]) >>> List([1, 2, 3]).concat([4, 5, 6]) List([1, 2, 3, 4, 5, 6]) >>> List([1, 2, 3]).append(4) List([1, 2, 3, 4]) >>> list(List([1, 2, 3])) [1, 2, 3]
- apply(f: Iterable[Callable[[T], U]]) List[U]
Apply each function in a list to each element of a list.
- map(f: Callable[[T], U]) List[U]
Map a function over a list.
- Parameters:
f (Callable[[T], U]) – A function that maps a value to a new value.
- Returns:
A new list containing the results of applying the function to each element of the original list.
- Return type:
List[U]
Set
- class monadic.Set(inner: Iterable[T])
Bases:
Iterable[T]A monadic set.
This is a wrapper around the built-in set type that implements the
Iterableinterface. Following a functional style, it is immutable. To add items to the set, useappend()orconcat().- Parameters:
inner (Iterable[T]) – The iterable to wrap as a set.
Examples
>>> from monadic.set import Set >>> Set({1, 2, 3}).map(lambda x: x + 1) Set({2, 3, 4}) >>> Set({1, 2, 3}).bind(lambda x: Set({x, x + 1})) Set({1, 2, 3, 4}) >>> Set({1, 2, 3}).apply(Set({str, float})) Set({1.0, 2.0, 3.0, '2', '1', '3'}) >>> Set({1, 2, 3}).concat({3, 4, 5}) Set({1, 2, 3, 4, 5}) >>> Set({1, 2, 3}).append(4) Set({1, 2, 3, 4}) >>> list(Set({1, 2, 3})) [1, 2, 3]
Dict
- class monadic.Dict(inner: Iterable[Tuple[K, V]] | Dict[K, V])
Bases:
Iterable[Tuple[K,V]]A monadic dictionary.
This is a wrapper around the built-in dict type that implements the
Iterableinterface. Following a functional style, it is immutable. To add an item to the dictionary by key, useset().This class is considered an iterable of tuples, where the first item in the tuple is the key and the second item is the value. This differs from the built-in dict type, which is an iterable of keys. To get the keys of a
Dict, use thekeys()method. To get the values of aDict, use thevalues()method.- Parameters:
inner (Iterable[Tuple[K, V]]) – The iterable of tuples containing the key-value pairs to wrap as a dictionary. Alternatively, a dictionary can be passed in directly.
Examples
>>> from monadic.dict import Dict >>> Dict({"a": 1, "b": 2, "c": 3}).get("a") Some(1) >>> Dict({"a": 1, "b": 2, "c": 3}).get("d") Nothing() >>> Dict({"a": 1, "b": 2, "c": 3}).set("d", 4) Dict({'a': 1, 'b': 2, 'c': 3, 'd': 4})
- apply(f: Dict[K, Callable[[Tuple[K, V]], Tuple[K2, V2]]]) Dict[K | K2, V | V2]
Apply a dictionary of functions to a dictionary.
For each key-value pair in the dictionary, if the key is present in the dictionary of functions, the function is applied to the key-value pair and the result is added to the result dictionary. If the key is not present in the dictionary of functions, the key-value pair is ignored.
- apply_keys(f: Dict[K, Callable[[K], K2]]) Dict[K | K2, V]
Apply a dictionary of functions to the keys of a dictionary.
This method is similar to
apply(), but only applies the functions to the keys of the dictionary, leaving the values unchanged.
- apply_values(f: Dict[K, Callable[[V], V2]]) Dict[K, V | V2]
Apply a dictionary of functions to the values of a dictionary.
This method is similar to
apply(), but only applies the functions to the values of the dictionary, leaving the keys unchanged.
- bind(f: Callable[[Tuple[K, V]], Dict[K2, V2]]) Dict[K2, V2]
Chain a function that maps a value to a dictionary.
- drop(key: K) Dict[K, V]
Remove a key-value pair from a dictionary.
- Parameters:
key (K) – The key to remove.
- Returns:
A new dictionary containing the old contents without the key-value pair.
- Return type:
Dict[K, V]
- filter(f: Callable[[Tuple[K, V]], bool]) Dict[K, V]
Filter a dictionary by a predicate.
The predicate is a function that takes a key-value pair and returns a boolean. If the predicate returns True, the key-value pair is included in the result. If the predicate returns False, the key-value pair is excluded from the result.
- Parameters:
f (Callable[[Tuple[K, V]], bool]) – A predicate function.
- Returns:
A new dictionary containing only the key-value pairs for which the predicate returns True.
- Return type:
Dict[K, V]
- filter_keys(f: Callable[[K], bool]) Dict[K, V]
Filter a dictionary by a predicate on the keys.
This method is similar to
filter(), but only applies the predicate to the keys of the dictionary, ignoring the values.
- filter_values(f: Callable[[V], bool]) Dict[K, V]
Filter a dictionary by a predicate on the values.
This method is similar to
filter(), but only applies the predicate to the values of the dictionary, ignoring the keys.
- map(f: Callable[[Tuple[K, V]], Tuple[K2, V2]]) Dict[K2, V2]
Map a function over a dictionary.
- Parameters:
f (Callable[[Tuple[K, V]], Tuple[K2, V2]]) – A function that maps a key-value pair to a new key-value pair.
- Returns:
A new dictionary containing the results of applying the function to each key-value pair in the original dictionary.
- Return type:
Dict[K2, V2]
- map_keys(f: Callable[[K], K2]) Dict[K2, V]
Map a function over the keys of a dictionary.
This method is similar to
map(), but only applies the function to the keys of the dictionary, leaving the values unchanged.