AniUI Academy
hard+350 XPPractice

Curry a variadic function

Fixed-arity currying can watch fn.length to know when it has enough. A variadic function has fn.length of 0, so there is nothing to count towards, and the curried form needs an explicit end signal instead. The convention is a call with no arguments, which is also the trick behind the add(1)(2)(3) puzzle.

Implement variadicCurry(fn). It returns a collector function. Calling the collector with one or more arguments returns a new collector holding everything gathered so far, so arguments can be supplied one at a time or several at a time, over any number of calls. Calling a collector with no arguments invokes fn with all of the arguments gathered along that chain and returns its result — so variadicCurry(fn)() invokes fn with no arguments at all. Every collector is independent: branching off the same partially applied collector twice must give two separate results, with no arguments leaking between the branches.

What it has to do

  • Collect arguments across any number of calls, one or several at a time.
  • Invoke fn with everything collected when a collector is called with no arguments.
  • Invoke fn with no arguments when the first call is empty.
  • Keep branches independent: reusing a partially applied collector must not accumulate arguments across uses.

Your workspace

Try it yourself
Loading playground...

Ready to check it?

5 tests run against your code, right here in your browser. Sign in to claim the XP when you pass.

AI Crack & Solution Assist

Stuck? Get instant AI hints or break down the optimal solution.

Stuck? The javascript course covers everything this challenge needs.