Invoke functions on arguments

OSCC · JavaScript, Function · Oct 21, 2020

Creates a function that invokes each provided function with the arguments it receives and returns the results.

  • Use Array.prototype.map() and Function.prototype.apply() to apply each function to the given arguments.
const over = (...fns) => (...args) => fns.map(fn => fn.apply(null, args));
const minMax = over(Math.min, Math.max);
minMax(1, 2, 3, 4, 5); // [1, 5]

Recommended snippets

  • Transform function arguments

    JavaScript, Function

    Creates a function that invokes the provided function with its arguments transformed.

  • Rearrange function arguments

    JavaScript, Function

    Creates a function that invokes the provided function with its arguments arranged according to the specified indexes.

  • Prepend function arguments

    JavaScript, Function

    Creates a function that invokes fn with partials prepended to the arguments it receives.