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))  

Popular posts from this blog

Decision Tree in R, with Graphs: Predicting State Politics from Big Five Traits

Chi-Square in R on by State Politics (Red/Blue) and Income (Higher/Lower)

Logistic Regression in R on State Voting in National Elections and Income