0
Functional programming. Don't understand
def apply_twice(func, arg): return func(func(arg)) def add_five(x): return x + 5 print(apply_twice(add_five, 10)) Maybe someone can explain step by step how its work? I read comments, but anyway can't understand proper how it si work? What are doing main function?
2 Answers
+ 3
print (apply_twice (add_five, 10) )
Here we see that we will print the number returned by apply_twice().
apply_twice's first argument is a function and other is a value.
in this func, func( func(arg) ) is returned.
since the function that we pass to apply_twice is add_five, it will be called.
So, apply_twice(add_five, 10) is equal to
add_five (add_five (10) ) which is equal to
add_five ( 15 ) which is equal to
20
So in print func we actually have the value of 20.
+ 1
Thanks