I am trying to run this snippet from http://www.ibm.com/developerworks/linux/library/l-prog3.html on a python 2.6 runtime.
from functional import *
taxcalc = lambda income,rate,deduct: (income-(deduct))*rate
taxCurry = curry(taxcalc)
taxCurry = taxCurry(50000)
taxCurry = taxCurry(0.30)
taxCurry = taxCurry(10000)
print "Curried taxes due =",taxCurry
print "Curried expression taxes due =", \
curry(taxcalc)(50000)(0.30)(10000)
Ok, so I understand from http://www.python.org/dev/peps/pep-0309/ that functional is renamed to functools and curry to partial but just doing the renames doesn’t help. I get the error:
taxCurry = taxCurry(50000)
TypeError: <lambda>() takes exactly 3 arguments (1 given)
The following does work but do I really have to change it so much?
from functools import partial
taxcalc = lambda income,rate,deduct: (income-(deduct))*rate
taxCurry = partial(taxcalc)
taxCurry = partial(taxCurry, 50000)
taxCurry = partial(taxCurry, 0.30)
taxCurry = partial(taxCurry, 10000)
print "Curried taxes due =", taxCurry()
print "Curried expression taxes due =", \
taxcalc(50000, 0.30, 10000)
Is there a better way of preserving the mechanics of the original example? Lastly was the original example truly currying or just partial application? (as per http://www.uncarved.com/blog/not_currying.mrk)
Thanks for your time
I guess the reason why they changed it is because Python is dynamically typed. This means that it would be really hard to debug the original
currycode if anything goes wrong – much harder than in a language like Haskell where you would directly get a nice type error. So I think it was a reasonable decision to replace it with the more explicitpartialversion (looks more pythonic to me).Your example is also a bit strange, since you just reassigning the partially applied functions to the same name. Normally the partially applied function would be given to another function. At least that is the only reasonable use case in Python I can think of.