Skip to content

Instantly share code, notes, and snippets.

@antsmartian
Created December 22, 2014 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save antsmartian/f79c0135a1bc8430d86d to your computer and use it in GitHub Desktop.
Save antsmartian/f79c0135a1bc8430d86d to your computer and use it in GitHub Desktop.
Monads in Groovy
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