Rename js snippets

This commit is contained in:
Angelos Chalaris
2023-05-19 20:23:47 +03:00
parent 82a614e42e
commit 9d032ce05e
305 changed files with 70 additions and 70 deletions

View File

@ -0,0 +1,27 @@
---
title: Converge branching functions
type: snippet
language: javascript
tags: [function]
excerpt: Converges a list of branching functions into a single function and returns the result.
cover: golden-gate-bridge
dateModified: 2021-01-08T00:23:44+02:00
---
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.
- Use `Array.prototype.map()` and `Function.prototype.apply()` to apply each function to the given arguments.
- Use the spread operator (`...`) to call `converger` with the results of all other functions.
```js
const converge = (converger, fns) => (...args) =>
converger(...fns.map(fn => fn.apply(null, args)));
```
```js
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
```