0.1.5 • Published 9 years ago

coffee-monad v0.1.5

Weekly downloads
2
License
BSD
Repository
github
Last release
9 years ago

Learn You Monad in Coffeescript

Monad implementations in Coffeescript for learning and practice. The goal is to make it as a guide for Coffeescript programmers to learn Monads.

What is Monad?

  • In general, Monad is a design pattern
  • Specifically, Monad is a data structure which implements Monad type class and satisfies Monad laws

Monad was introduced in Haskell, but any language which supports higher order functions can implement Monads, such as Javascript/Coffeescript

Monad type class

Type class is a concept in Haskell. You can treat it as an interface in Java. Monad type class defines two functions, unit and return.

  • unit is to wrap a value into a monad. In Haskell it's named return.
  • bind is to unwrap the value and apply it to a function which takes the value as input and outputs a monad. In Haskell, it's >>= and in Scala it's flatMap.

We use Monad's name as unit function for conveniences, so you can write Maybe(5) instead of Maybe.unit(5).

In Haskell, bind function is infix operator >>=, so when you bind, it looks like

m >>= \ a -> m

Javascript doesn't support operator overloading, so it would look like

bind m (a)-> m

It's not as convenient as Haskell, so we leverage Javascript's OO nature and make bind as a method of monad. Thus it looks like

m.bind (a)-> m

Monad laws

  • Left identity: unit(a).bind f == f a
  • Right identity: m.bind unit == m
  • Associativity: (m.bind f).bind g == m.bind (x)-> f(x).bind g

How to use monad

Haskell

do
  a <- m(a)
  b <- m(b)
  c <- m(c)
  return (a+b+c)

    ||
    \/

m(a) >>= \a -> m(b) >>= \b -> m(c) >>= \c m(a+b+c)

Coffeescript

m(a).bind (a)->
  m(b).bind (b)->
    m(c).bind (c)->
      m(a+b+c)

Haskell has do-notation, but it's actually syntax sugar for bind(>>=). Coffeescript doesn't support do-notation, but it looks like similar to Haskell binding combinations.

Installation

$ npm install coffee-monad

Unit Test

$ jasmine-node --coffee --verbose spec/

Change Log

  • update README (Dec. 1, 2014)
  • State monad (Nov. 30, 2014)
  • Writer, Reader monad (Nov. 29, 2014)
  • IO monad (Nov. 29, 2014)
  • Maybe, List monad (Nov. 28, 2014)
0.1.5

9 years ago

0.1.4

9 years ago

0.1.3

9 years ago

0.1.2

9 years ago

0.1.1

9 years ago

0.1.0

9 years ago

0.0.9

9 years ago

0.0.8

9 years ago

0.0.7

9 years ago

0.0.6

9 years ago

0.0.5

9 years ago

0.0.4

9 years ago

0.0.3

9 years ago

0.0.2

9 years ago

0.0.1

9 years ago