Created
November 15, 2012 17:48
-
-
Save nkhalasi/4080056 to your computer and use it in GitHub Desktop.
Function currying and scope 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 sum = {a, b -> return a + b } | |
def square = {a -> return a * a } | |
def composition = {f, g, a, b -> f(g(a), g(b))} | |
def sumOfSquares = composition.curry(sum, square) | |
def sumOfSquaresOfTwoLargestNumbers(a, b, c) { | |
if ((a < b) && (a < c)) return sumOfSquares(b, c) | |
else if ((b < a) && (b < c)) return sumOfSquares(a, c) | |
else return sumOfSquares(a, b) | |
} | |
println sumOfSquaresOfTwoLargestNumbers(1, 3, 4) | |
println sumOfSquaresOfTwoLargestNumbers(3, 4, 1) | |
println sumOfSquaresOfTwoLargestNumbers(4, 1, 3) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment