Functions are first class objects: Higher Order Functions
I am currently working through Computational Statistics in Python's module Functions are first class objects, and for this - I have worked with delegates and higher order functions in other languages - I put together this example to better grasp higher order functions in Python:
def power_function(num, power):
"""power of num."""
return num**power
def add_function(num, more):
"""sum of num."""
return num+more
def runFunction(fx, x, y):
return fx(x,y)
num = 2
powers = range(0,64)
arrPower = list(map(lambda x: runFunction(power_function, num, x), powers))
arrAdd = list(map(lambda x: runFunction(add_function, num, x), powers))
arrPower
arrAdd
Comments
Post a Comment