Skip to content

Instantly share code, notes, and snippets.

@StasKoval
Forked from shovon/gist:5685073d03829f3b63d0
Created March 29, 2016 15:09
Show Gist options
  • Save StasKoval/7af0c5af3e619da45702 to your computer and use it in GitHub Desktop.
Save StasKoval/7af0c5af3e619da45702 to your computer and use it in GitHub Desktop.
Haskell-style monads in JavaScript (ES6).
'use strict';
class Monad {
constructor(a) { this._value = a; }
'>>='(f) { return f(this.value()); }
static create(a) { return new Monad(a); }
value() { return this._value; }
}
var val = Monad.create(10) ['>>=']
(x => Monad.create(x*x) ['>>=']
(x => Monad.create(x - 42) ['>>=']
(x => Monad.create(x + 3)) ))
.value();
console.log(val);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment