Last active
May 25, 2016 16:00
-
-
Save crazy4groovy/cfff535dcdbb249e3775 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
Closure bind | |
List.metaClass.rightShiftUnsigned = { return bind(delegate, it) } | |
List.metaClass.call = { return [delegate, ''] } | |
Closure squrt = { Double x -> | |
if (x < 0.0d) return [] | |
if (x == 0.0d) return [0.0] | |
return [Math.pow(x, 0.5), -Math.pow(x, 0.5)] | |
} | |
bind = { List x, Closure f -> | |
return (x.collect{ Double d -> f(d) }).flatten() | |
} | |
assert [5.0, 0.0, 3.0] >>> squrt >>> squrt == | |
[1.4953487812212205, -1.4953487812212205, 0.0, 1.3160740129524924, -1.3160740129524924] | |
////// | |
Closure verbose_u = { Integer x -> | |
return [x + 4, "[verbose_u was called on $x]"] | |
} | |
Closure verbose_v = { Integer x -> | |
return [x * 2, "[verbose_v was called on $x]"] | |
} | |
bind = { List x, Closure f -> | |
Integer result | |
String output | |
(result, output) = f(x[0]) | |
return [result, x[1] + output] | |
} | |
assert [4]() >>> verbose_v >>> verbose_u == | |
[12, '[verbose_v was called on 4][verbose_u was called on 8]'] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Examples taken from haskell article "Monads, part 1: a design pattern":
https://www.stephanboyer.com/post/9/monads-part-1-a-design-pattern