Convert function from variadic

JavaScript, Function · Jun 13, 2021

Takes a variadic function and returns a function that accepts an array of arguments.

  • Use a closure and the spread operator (...) to map the array of arguments to the inputs of the function.
const spreadOver = fn => argsArr => fn(...argsArr);
const arrayMax = spreadOver(Math.max);
arrayMax([1, 2, 3]); // 3

More like this

  • Convert function to variadic

    Changes a function that accepts an array into a variadic function.

    JavaScript, Function · Jun 13, 2021

  • Flip function arguments

    Takes a function as an argument, then makes the first argument the last.

    JavaScript, Function · Jun 13, 2021

  • Function arity

    Creates a function that accepts up to n arguments, ignoring any additional arguments.

    JavaScript, Function · Oct 18, 2020