I am trying to understand how currying works in functional programming. I have gone through wiki and a couple of questions about the same on SO.
Need help understanding lambda (currying)
What is 'Currying'?
I understand that currying is all about splitting a function that takes n arguments into n or less functions with one argument each. I theoretically comprehend it but I am not able to connect the dots while coding for the same. Perhaps it is my lack of knowledge in functional programming languages or C# (as many answers in the above questions deal with).
Anyway, I understand groovy & java. So I tried to get an outline for the standard add(a,b)
function in groovy but I couldn't finish it.
def closure = { arg ->
// ??
}
def add(anotherClosure , a){
return closure // ??
}
Can someone help me understand currying using groovy closures?
A closure in Groovy is an open, anonymous, block of code that can take arguments, return a value and be assigned to a variable. A closure may reference variables declared in its surrounding scope.
currying is a concept / idea, closures are implementation technique / detail.
Curried functions are also an important application of closure. They split a function with many parameters into functions with only one parameter each.
Currying is the transformation of a function with multiple arguments into a sequence of single-argument functions. That means converting a function like this f(a, b, c, ...) into a function like this f(a)(b)(c)... . If you know Python's functools. partial , then this is very similar to it.
You can roll your own currying functionality by writing a closure that takes another closure and a curried parameter to set, and returns a closure that uses this value.
// Our closure that takes 2 parameters and returns a String
def greet = { greeting, person -> "$greeting $person" }
// This takes a closure and a default parameter
// And returns another closure that only requires the
// missing parameter
def currier = { fn, param ->
{ person -> fn( param, person ) }
}
// We can then call our currying closure
def hi = currier( greet, 'Hi' )
// And test it out
hi( 'Vamsi' )
But you're better sticking with the inbuilt Groovy curry
method as shown by jalopaba. (there is also rcurry and ncurry which curry from the right, and at a given position respectively)
It should be said, that the Groovy curry method is a misnomer as it is more a case of partial application as you do not need to get down to a closure requiring only a single parameter, ie:
def addAndTimes = { a, b, c -> ( a + b ) * c }
println addAndTimes( 1, 2, 3 ) // 9
def partial = addAndTimes.curry( 1 )
println partial( 2, 3 ) // 9
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With