This is an interesting programming challenge that reddit user
i7_leaf claims to have received as an interview question.
There is one key difference between the original question and what I will cover in this article. The interview question asked the candidate to write a function that executes as follows:
It is worth noting that this curried function does not end in any sort of delimiter, e.g. a terminating method
.execute() or empty parameter
(). What makes this challenge both difficult and interesting is the lack of signal that “this is the last digit in the sequence.”
I agree with the majority of comments in the discussion thread that the interviewer did not mean to ask this question per se. As postulated, this function cannot exist. It is impossible for
addSubtract(1)(2)(3) to both be a primitive (the number
0 in the first example) and a function (that accepts 4 as a parameter in the second example).
That said, this is conceptually possible with a very slight tweak. While the following two statements cannot both be true, the third statement can.