fun_land v0.5.0 FunLandic.Sum

Sum wraps numbers. Each Sum struct is a wrapper around a single number, and basically says: ‘I want to treat this number as something that, when combined with another number, should be summed, using 0 as neutral element’

This is necessary as there are multiple ways to combine numbers. Another, very common way is FunLandic.Product.

You can use Sum either as a Combinable, by just passing in a Reducable with numbers,

Or you can use Sum as Monad, by wrapping individual numbers and combining them using map, apply_with, wrap and chain.

Summary

Functions

Callback implementation for FunLand.Chainable.chain/2

This is called internally whenever a YourMonad.chain() operation fails

Free implementation wrap Mappable.map for Applicative

The only things that make sense to put in a %Sum{} are a number, or a function that will eventually evaluate to a number

Macros

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

Functions

apply_discard_left(a, b)

See FunLand.Applicative.apply_discard_left/2.

apply_discard_right(a, b)

See FunLand.Applicative.apply_discard_right/2.

apply_with(sum1, sum2)

Callback implementation for FunLand.Appliable.apply_with/2.

chain(sum, function)

Callback implementation for FunLand.Chainable.chain/2.

combine(a, b)
fail(var, expr)

This is called internally whenever a YourMonad.chain() operation fails.

For most monads, the default behaviour of crashing is great. For some, you might want to override it.

guard(predicate)
map(a, function)

Free implementation wrap Mappable.map for Applicative

neutral()
wrap(val)

The only things that make sense to put in a %Sum{} are a number, or a function that will eventually evaluate to a number.

Note that unfortunately, Elixir has no way to check the output of a function. So we allow ‘any’ function to be wrapped.

Macros

monadic(list)

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

For more info, see FunLand.Monad.monadic

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.