Converge branching functions

OSCC · JavaScript, Function · Jan 7, 2021

Accepts a converging function and a list of branching functions and returns a function that applies each branching function to the arguments and the results of the branching functions are passed as arguments to the converging function.

const converge = (converger, fns) => (...args) =>
  converger(...fns.map(fn => fn.apply(null, args)));
const average = converge((a, b) => a / b, [
  arr => arr.reduce((a, v) => a + v, 0),
  arr => arr.length
]);
average([1, 2, 3, 4, 5, 6, 7]); // 4

Recommended snippets

  • Invoke functions on arguments

    JavaScript, Function

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

  • Attempt invoking a function

    JavaScript, Function

    Attempts to invoke a function with the provided arguments, returning either the result or the caught error object.

  • Pipe async functions

    JavaScript, Function

    Performs left-to-right function composition for asynchronous functions.