hymn.types.monad
the monad class
Implements bind operator >>
and inverted bind operator <<
as syntactic sugar. It is equivalent to (>>=)
and (=<<)
in haskell, not to be confused with (>>)
and (<<)
in haskell.
As python treats assignments as statements, there is no way we can overload >>=
as a chainable bind, be it directly overloaded through __irshift__
, or derived by python itself through __rshift__
.
The default implementations of bind
, fmap
and join
are mutual recursive, subclasses should at least either override bind
, or fmap
and join
, or all of them for better performance.
bind(self, f)
the bind operation
f
is a function that maps from the underlying value to a monadic type, something like signature f :: a -> M a
in haskell's term.
The default implementation defines bind
in terms of fmap
and join
.
fmap(self, f)
the fmap operation
The default implementation defines fmap
in terms of bind
and .unit
.
join(self)
the join operation
The default implementation defines join
in terms of bind
and identity
function.
monadic(cls, f)
decorator that turn f
into monadic function of the monad
unit(cls, value)
the unit of monad