EasyPythonJavaScriptTypeScript

Once Wrapper

FunctionsPatternsClosures

Implement solve(callArgs) that demonstrates the once higher-order
function: a wrapper that ensures the underlying function runs at most once
and returns the same result for every subsequent call.

callArgs is an array of argument lists. The first call invokes the
underlying function; all subsequent calls return the cached first result.

The underlying function simply returns the sum of its arguments.

Example
solve([[1,2],[3,4],[5]])[3, 3, 3]
(first call returns 1+2=3; subsequent calls also return 3)

Sample tests

Test #1Single call
Input: [[[10]]]
Output: {"results":[10],"invocations":1}
Test #2First result is 0 (sum of zeros), returned for all calls
Input: [[[0,0],[1,2],[3,4]]]
Output: {"results":[0,0,0],"invocations":1}
Test #3No calls → no invocations
Input: [[]]
Output: {"results":[],"invocations":0}
Test #4Second and third calls ignored → all return first result
Input: [[[5,5],[99],[1,2,3]]]
Output: {"results":[10,10,10],"invocations":1}
Test #5First call returns 3; subsequent calls return same 3
Input: [[[1,2],[3,4],[5]]]
Output: {"results":[3,3,3],"invocations":1}