Created
May 19, 2014 13:38
-
-
Save elclanrs/bf3dba84a851e25a3354 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require('funcoffee').expose global | |
#- Monadic | |
unit = curry (x, a) -> new x.constructor a | |
mbind = curry (f, ma) -> ma.bind f | |
mjoin = (ma) -> ma.bind id | |
fmap = curry (f, ma) -> ma.bind (a) -> unit ma, f a | |
ap = curry (mf, ma) -> mf.bind (f) -> fmap f, ma | |
liftA = (f, ms...) -> foldr( | |
(vs) -> unit ms[0], f vs... | |
(a, b) -> (vs) -> b.bind (v) -> a vs.concat [v] | |
ms | |
) [] | |
class Maybe | |
bind: (f) -> | |
return f @value if @ instanceof Just | |
new Nothing | |
class Nothing extends Maybe | |
toString: -> 'Nothing' | |
class Just extends Maybe | |
constructor: (@value) -> | |
toString: -> "Just #{@value}" | |
maybe = (x) -> | |
return new Just x if x? | |
new Nothing | |
just = (x) -> new Just x | |
nothing = new Nothing | |
class Either | |
bind: (f) -> | |
return f @right if @ instanceof Right | |
new Left @left | |
class Left extends Either | |
constructor: (@left) -> | |
toString: -> "Left #{@left}" | |
class Right extends Either | |
constructor: (@right) -> | |
toString: -> "Right #{@left}" | |
either = curry (left, right) -> | |
return new Right right if right? | |
new Left left | |
left = (x) -> new Left x | |
right = (x) -> new Right x |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment