Higher order functions => take function as arguments, or return them as results
"Function as object"
>>>
def apply_twice(func arg):
return func(func(arg))
def add_five(x):
return x +5
print (apply_twice(add_five, 10))
>>>
20
Pure Functions => have no inside effects and return a value that depends only on their arguments.
eg : Pure function
>>>
def pure_function(x,y)
temp = z +2*y
return temp / (2*x + y)
eg : Impure function
>>>
def impure(arg):
some_list.append(arg)
Pure Functions
Pros
- easier to reason about the test
- more efficient
- easier to run in parallel
memoization =>Once the function has been evaluated for an input , the result can be stored and referred to the next time and function of that input is needed , reducing the number of times the function is called.
Cons
- inherently require side effects
- more difficult to write in some situations
No comments:
Post a Comment