Convert function from variadic

OSCC · 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

Recommended snippets

  • Convert function to variadic

    JavaScript, Function

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

  • Function arity

    JavaScript, Function

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

  • Juxtapose functions

    JavaScript, Function

    Takes several functions as argument and returns a function that is the juxtaposition of those functions.