Converge branching functions

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

More like this

  • Invoke functions on arguments

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

    JavaScript, Function · Oct 21, 2020

  • Pipe async functions

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

    JavaScript, Function · Oct 22, 2020

  • Pipe functions

    Performs left-to-right function composition.

    JavaScript, Function · Oct 22, 2020