Functional programming. Don't understand | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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?

14th May 2020, 4:17 PM
Sigitas S
Sigitas S - avatar
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.
14th May 2020, 4:26 PM
Mustafa K.
Mustafa K. - avatar
+ 1
Thanks
1st Aug 2020, 8:06 AM
riya charles
riya charles - avatar