r/csinterviewproblems • u/parlezmoose • Dec 17 '15
[Recursion] Write a function to perform currying addition
Write a function that adds by currying. The function accepts either one numerical argument, or no arguments. If called with a number, the function returns another function. If called without an argument, the function returns the sum of all previous arguments.
Example:
a()
returns 0
a(1)
returns function
a(1)()
returns 1
var add = a(1)(2)(3)
add is a function
add()
returns 6
3
Upvotes
2
u/parlezmoose Dec 17 '15
1
u/BlckKnght Dec 22 '15
Python:
You could use
functools.partialinstead of a closure, but this seems just as good.