Details
-
Type:
Bug
-
Status:
Open
-
Priority:
Major
-
Resolution: Unresolved
-
Affects Version/s: None
-
Fix Version/s: 3.0
-
Component/s: groovy-jdk
-
Labels:None
-
Number of attachments :
Description
Hi.
Groovy's Closure have a name of method 'curry'.
But this is not work for a real currying, it work as a partial function application.
So, I wrote method of 'Real currying'.
Referenced http://en.wikipedia.org/wiki/Currying
Closure add = {a, b, c -> a + b + c } // Closure of adding 3 arguments.
assert add(1, 2, 3) == realCurry(add)(1)(2)(3)
assert 6 == add(1, 2, 3)
def curriedAdd = realCurry(add)
def curriedAdd_1 = curriedAdd(1)
def curriedAdd_1_2 = curriedAdd_1(2)
def addResult = curriedAdd_1_2(3)
assert 6 == addResult
def realCurry(Closure clos) {
if (clos.maximumNumberOfParameters >= 1) {
return { x ->
def cc = clos.curry(x)
if (cc.maximumNumberOfParameters) realCurry(cc)
else cc()
}
} else {
return clos
}
}
(It's also published on gist https://gist.github.com/1193548)
I hope this will be adopted to Groovy core.
Thank you.
the point you are referring to is realCurry(add)(1)(2)(3), instead of our add.curry(1).curry(2).curry(3). Furthermore add.curry(1) results in an add, in which the first argument will be 1 and only second and third can be applied any more, while realCurry(add) returns a function that takes only one argument and returns then a new function, in which add will have 1 set as first argument, but again accepts only one argument. Applying the final argument gives the result then.
Yoshida, do you have some examples in which you can show the advantage of real currying over partial function application?