Skip to content

Instantly share code, notes, and snippets.

@nkhalasi
Created November 15, 2012 17:48
Show Gist options
  • Save nkhalasi/4080056 to your computer and use it in GitHub Desktop.
Save nkhalasi/4080056 to your computer and use it in GitHub Desktop.
Function currying and scope in groovy
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