Infinite Curried Sum

ClosuresCurryingCoercion

Write sum(n) so it can be called any number of times in a chain, each call adding to a running total.

The total appears when the result is converted to a primitive — by +, by String(), or inside a template literal. Two separate chains must not share a total.

Examples

Example 1
Input
+sum(1)(2)(3)
Output
6
Explanation
Each call adds to the closure's total; the unary + asks the returned function for its primitive value.

Constraints

  • Every argument is a number
  • Chains can be arbitrarily long

Notes

  • Unlike a fixed-arity curry, nothing tells the function when the chain has ended — coercion is the only signal.
  • A function is an object, and objects choose how they coerce.

Hints

Read the full write-up for Infinite Curried Sum
</>JavaScript
Loading editor…
Test Result
Run your code, or Submit to test it