Entry
Currying
Jul 5th, 2000 09:59
Nathan Wallace, Hans Nowak, Snippet 23, Michael Christopher Vanier
"""
Packages: functional_programming
"""
"""
Here's a couple of nifty tools I came up with today. I'm sure it's been
done before, but what the heck :-) It made me wonder, though: are there
any functions that could be implemented in a functional programming
language that are impossible to do in python?
Mike
"""
#
# Currying functions.
#
class curry:
def __init__(self, func, arg):
self.func = func
self.arg = tuple([arg])
def __call__(self, *args, **kw):
return apply(self.func, self.arg + args, kw)
#
# Transforming binary operators into functions.
# N.B. There's no error checking -- user beware!
#
def op(opstr):
result = "lambda x, y: x %s y" % opstr
func = eval(result)
return func
if __name__ == '__main__':
s = [1, 2, 3, 4, 5]
n = 10
multn = curry(op('*'), n)
print s, "\n", map(multn, s)