How can I write code using FP
Alejandro Dubrovsky
dubrovsky at physics.uq.edu.au
Tue Jan 16 23:10:31 EST 2007
kernel1983 wrote:
> In Function Program?Language can use like this:
>
> define a function:
> f = lambda x,y:x,y
>
> then we use f to define another function:
> f2 = f(1)
> the f2 should equal to:
> f2=lambda y:1,y
>
> we should be able call f2 with one parameter:f2(2)
> just return 1,2
>
> but how can I implement the characteristic in Python??I think it could
> do like this but HOW-TO?
If you are using python 2.5, then you can use functools.partial, otherwise
lookup one of the many curry implementations, like the ones found in
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52549
One found in the comments of that page by Scott David Daniels:
def curry(*args, **kwargs):
function, args = args[0], args[1:]
def result(*rest, **kwrest):
combined = kwargs.copy()
combined.update(kwrest)
return function(*args + rest, **combined)
return result
so, in your case:
f = lambda x,y: (x,y)
f2 = functools.partial(f,1)
>>> f2(3)
1,3
More information about the Python-list
mailing list