Created
December 22, 2014 14:08
-
-
Save antsmartian/f79c0135a1bc8430d86d to your computer and use it in GitHub Desktop.
Monads in Groovy
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
def sin = { x -> | |
return [Math.sin(x),"Called"] | |
} | |
def cube = { x -> | |
return [x * x * x,"Hello "] | |
} | |
def compose = { f , g -> | |
def cls = { x -> | |
return f(g(x)) | |
} | |
return cls | |
} | |
def composableDebuggable = { f, g -> | |
def cls = { x -> | |
def gx = g(x) | |
def y = gx[0] | |
def s = gx[1] | |
def fy = f(y) | |
def z = fy[0] | |
def t = fy[1] | |
return [z,s + t] | |
} | |
return cls | |
} | |
def bind = { f -> | |
def cls = { tuple -> | |
def x = tuple[0], s = tuple[1],fx = f(x), y = fx[0] , t = fx[1] | |
return [y,s + t ] | |
} | |
return cls | |
} | |
def unit = { x -> | |
return [x, ' '] | |
} | |
def lift = { f -> | |
def cls = { x -> | |
return unit(f(x)) | |
} | |
return cls | |
} | |
def round = { x -> return Math.round(x) }; | |
def roundDebug = lift(round) | |
def f = compose(bind(roundDebug),bind(sin)) | |
f(unit(27)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment