Monads in LFE
Go to file
Correl Roush e488fb2225 Merge pull request #4 from lfex/formatting
Formatting changes
2015-05-21 22:03:37 -04:00
include Updated to new call syntax. 2015-05-21 20:57:48 -05:00
resources/make Removed old targets. 2015-05-21 20:45:42 -05:00
src Updated formatting to use data forms. 2015-05-21 18:20:06 -05:00
test Updated deps and unit tests to latest. 2015-05-21 17:31:11 -05:00
.gitignore Maybe 2014-04-24 00:47:45 -04:00
.travis.yml Add OTP version 17.0 to the travis build config 2014-07-09 23:25:37 -04:00
Makefile Updated make file and include to latest. 2015-05-21 17:42:05 -05:00
README.rst Using pwd in ERL_LIBS allows standard usage of include. 2015-05-19 23:12:13 -05:00
lfe.config Fixed bad copy/paste. 2015-05-21 20:44:39 -05:00
rebar.config Replaced obselete EXPM data with lfe.config. 2015-05-21 17:39:27 -05:00

README.rst

###########
calrissian
###########
.. image:: https://travis-ci.org/correl/calrissian.svg?branch=master   :target: https://travis-ci.org/correl/calrissian

Introduction
============

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

Dependencies
------------

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``:

* `LFE`_ (Lisp Flavored Erlang; needed only to compile)
* `lfeunit`_ (needed only to run the unit tests)


Installation
============

Just add it to your ``rebar.config`` deps:

.. code:: erlang

    {deps, [
        ...
        {calrissian, ".*", {git, "git@github.com:correl/calrissian.git", "master"}}
      ]}.


And then do the usual:

.. code:: bash

    $ rebar get-deps
    $ rebar compile


Examples
========

The following examples demonstrate some of the possible uses of monads
in real-world code.

Error Monad
-----------

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.

.. code:: scheme

    (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:

.. code:: scheme

    (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))))))

.. Links
.. -----
.. _erlando: https://github.com/rabbitmq/erlando
.. _rebar: https://github.com/rebar/rebar
.. _LFE: https://github.com/rvirding/lfe
.. _lfeunit: https://github.com/lfe/lfeunit