Skip to content

Instantly share code, notes, and snippets.

@shovon
Last active March 25, 2018 16:10
Show Gist options
  • Save shovon/5685073d03829f3b63d0 to your computer and use it in GitHub Desktop.
Save shovon/5685073d03829f3b63d0 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