functional programming | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

functional programming

'''please explain how it works''' #FUNCTIONAL PROGRAMMING def apply_twice(func,arg): return func(func(arg)) def add_five(x): return x+6 print(apply_twice(add_five,10))

5th Jan 2019, 8:59 AM
sayan mondal
sayan mondal - avatar
2 Answers
+ 3
add_five(x) adds 6 (!?) to x and returns the sum. test: print(add_five(4)) # output: 10 apply_twice takes two arguments, func and arg and then returns func(func(arg)). If you call apply_twice with the arguments add_five and 10, this is what happens in apply_twice: return func(func(arg)) => this is the same as return add_five(add_five(10)). => add_five(10) is 16. => add_five(add_five(10)) equals add_five(16). => add_five(16) is 22 and that's the return value of the function.
5th Jan 2019, 9:30 AM
Anna
Anna - avatar
+ 1
Thanks
5th Jan 2019, 9:37 AM
sayan mondal
sayan mondal - avatar