When should I write my functions in curried form? does not match my thought, need to correct myself.
As part of my learning link, this is what I understand from function currying. Below is one example:
def curry2(f):
"""Returns a function g such that g(x)(y) == f(x, y)
>>> from operator import add
>>> add_three = curry2(add)(3)
>>> add_three(4)
"""
def g(x):
def h(y):
return f(x, y)
return h
return g
In any application, if I know that the number of arguments are fixed (say 2 arguments) and
function name is normalise_range
(say), then I will define def normalise_range(x, y):
function and use it in my application directly by calling normalise_range(x, y)
.
In any application, if I know that, the number of arguments are fixed (say 2 arguments),
but the function name is varying (can be normalise_range
/average
/I don't know..),
then I will use def curry2(f):
as shown above, which will accept all functions that take two arguments (fixed).
My question:
- Is my understanding correct?
- If yes, can we think of currying for functions of variable number of arguments?
curry2(f)
, you could callf(x, y)
just fine without currying it.Function Currying
andpartial application