Write a function, f
, that takes in a positive integer and returns a function.
The new function returned should be identical to f
. However, when the "termination call" happens, f
should instead return the sum of all integers passed.
For example, g=f(4)
(if f
is the first function) should set g
to another function. h=g(3)
will do the same. However, when you call h
with no arguments (see below for details), it should output 7, as that is the sum of the previous function arguments. Put another way, f(3)(4)() == 7
.
Do note this is not the same as f(3,4)()
.
"Termination call" is one of the following options (your choice):
- call w/o arguments
- null as argument
- any non-positive value
Arbitrary amount of function calls should be supported, there is no predefined limit.
It's guaranteed that total sum will not be bigger than 1'000.
We can assume that there is at least one call is made prior to "termination call".
Your code should not use static, per-program variables, so it should be possible to run the experiment multiple times in the same runtime and observe exactly the same behavior.
Examples:
f(1)() == 1
f(4)(2)(7)() == 13
f(4)(2)(7)(5)(2)() == 20
f(4)
returns a new function. If that new function is called without arguments, it returns4
, but if it's called with another argument then it will again return a new function with the same semantics but with the new argument added to the4
and so on. \$\endgroup\$q = f(2)(3); b = f(1)(2)(3); q(); b()
? \$\endgroup\$