Giter Site home page Giter Site logo

calrissian's Introduction

calrissian

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:

{deps, [
    ...
    {calrissian, ".*", {git, "[email protected]:correl/calrissian.git", "master"}}
  ]}.

And then do the usual:

$ 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.

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

calrissian's People

Contributors

correl avatar oubiwann avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Forkers

longde123 arpunk

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.