fun_land v0.7.3 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

apply_with(a, b)

See FunLand.Appliable.apply_with/2.

chain(a, fun)

See FunLand.Chainable.chain/2.

map(a, fun)

See FunLand.Mappable.map/2.

wrap(module, a)

See FunLand.Applicative.wrap/2.

Macros

monadic(monad, list)

Allows you to write multiple consecutive operations using this monad on new lines. This is called ‘monadic do-notation’.

Rules:

  1. Every normal line returns a new instance of the monad.
  2. You can write x <- some_expr_returning_a_monad_instance to bind x to whatever is inside the monad. You can then use x on any subsequent lines.
  3. If you want to use one or multiple statements, use let something = some_statement or let 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.