Calrissian is an implementation of monads in LFE, inspired by erlando, mostly as a learning exercise. The following monads are currently supported:
- Identity
- Maybe
- Error
- State
- State Transformer
This project assumes that you have rebar installed somwhere in your
$PATH
.
This project depends upon the following, which are installed to the deps
directory of this project when you run make deps
:
Just add it to your rebar.config
deps:
{deps, [
...
{calrissian, ".*", {git, "[email protected]:correl/calrissian.git", "master"}}
]}.
And then do the usual:
$ rebar get-deps
$ rebar compile
The following examples demonstrate some of the possible uses of monads in real-world code.
The following is an example of using the error monad and do-notation to simplify flow control through a series of sequential operations that, if any step should fail, should halt execution and return an error.
The error monad will inspect the result of the previous operation. If
it was successful (represented as 'ok
or (tuple 'ok result)
),
the result will be passed on to the next operation. If it failed
(represented as (tuple 'error reason)
, the error will be returned
and execution will cease.
(include-lib "calrissian/include/monads.lfe")
(defun dostuff ()
(do-m (monad 'error)
(input <- (fetch-input)) ;; fetch-input -> (tuple 'ok result) | (tuple 'error reason)
(parsed <- (parse-input input)) ;; parse-input -> (tuple 'ok result) | (tuple 'error reason)
(store-data parsed))) ;; store-data -> 'ok | (tuple 'error reason)
Without the error monad, the code might have looked like this:
(defun dostuff ()
(case (fetch-input)
((tuple 'error reason)
(tuple 'error reason))
((tuple 'ok input)
(case (parse-input input)
((tuple 'error reason)
(tuple 'error reason))
((tuple 'ok parsed)
(store-data parsed))))))