fun_land v0.6.1 FunLand.Monad
A Monad is something that is a Monad.
To be a Monad, something has to be Applicative, as well as Chainable.
Something that is a Monad has four operations:
wrap
, which allows us to put anything inside a new structure of this kind.map
, which allows us to take a normal function transforming one element, and change it into a function that transforms all of the contents of the structure.chain
, which allows us to take a function that usually returns a new structure of this kind, and instead apply it on an already-existing structure, the result being a single new structure instead of multiple layers of it.apply_with
, which allows us to take a partially-applied function inside a structure of this kind to be applied with another structure of this kind.
This allows us to:
- Take any normal value and put it into our new structure.
- Use any normal functions as well as any functions returning a structure of this kind to be used in a sequence of operations.
- determine what should happen between subsequent operations. (when/how/if the next step should be executed)
Summary
Macros
Allows you to write multiple consecutive operations using this monad on new lines. This is called ‘monadic do-notation’
Functions
Macros
Allows you to write multiple consecutive operations using this monad on new lines. This is called ‘monadic do-notation’.
Rules:
- Every normal line returns a new instance of the monad.
- You can write
x <- some_expr_returning_a_monad_instance
to bindx
to whatever is inside the monad. You can then usex
on any subsequent lines. - If you want to use one or multiple statements, use
let something = some_statement
orlet something = do ...
The final line is of course expected to also return an instance of the monad.
Use wrap
at any time to wrap a value back into a monad if you need.
Inside the monadic context, the module of the monad that was defined is automatically imported.
Any local calls to e.g. wrap
, apply
, chain
or functions you’ve defined yourself in your monad module will thus be called on your module.