How a function can be applied on top of another function like a(b)(x,y) where a and b are functions and x and y are arguments? Can somebody provide me with the term we call this concept along with some examples.
CodePudding user response:
For a(b)(x, y)
to make sense, a
needs to be a function that takes b
as an argument and produces a new function that takes two arguments. Here's an example:
>>> def a(func):
... def wrapped(x, y):
... return 2 * func(x, y)
... return wrapped
...
>>> def b(x, y):
... return x y
...
>>> a(b)(1, 2)
6
In the above example a
is a function that wraps a two-argument function and doubles its result. b(1, 2)
is 3
, and a(b)(1, 2)
is therefore 6.
CodePudding user response:
a function can be applied to another function like this
a(b)(x,y)
if the returned value from a(b) is a function that takes 2 arguments in that case the function that is returned from a(b) will be applied on the parameters x,y
for example :
def mul(x,y):
return x*y
def add5_to_func(func):
return lambda x,y : func(x,y) 5
print(add5_to_func(mul)(3,3)) # will print 14