The Monad Class¶
-
class
hymn.types.monad.
Monad
(value)¶ Bases:
object
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()
andjoin()
are mutual recursive, subclasses should at least either overridebind()
, orfmap()
andjoin()
, or all of them for better performance.-
bind
(f)¶ the bind operation
f
is a function that maps from the underlying value to a monadic type, something like signaturef :: a -> M a
in haskell’s term.The default implementation defines
bind()
in terms offmap()
andjoin()
.
-
fmap
(f)¶ the fmap operation
The default implementation defines
fmap()
in terms ofbind()
andunit()
.
-
join
()¶ the join operation
The default implementation defines
join()
in terms ofbind()
andidentity
function.
-
classmethod
monadic
(f)¶ decorator that turn
f
into monadic function of the monad
-
classmethod
unit
(value)¶ the unit of monad
-