Skip to content

Instantly share code, notes, and snippets.

@dexterous
Forked from nkhalasi/currying_and_scope.groovy
Created November 15, 2012 18:06
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 dexterous/4080178 to your computer and use it in GitHub Desktop.
Save dexterous/4080178 to your computer and use it in GitHub Desktop.
Function currying and scope in groovy
def sum = { it.sum() }
def square = { it * it }
def ofEach = { Object[] l -> l.&collect }
sumOfSquares = sum << { it(square) } << ofEach
def sumOfSquaresOfTwoLargestNumbers(a, b, c) {
def l = [a, b, c]
l -= l.min()
sumOfSquares(*l)
}
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