```// Les briques de base que nous allons composer
const double = x => x + x;
const triple = x => 3 * x;
const quadruple = x => 4 * x;

// Une fonction qui permet d'appliquer une composition
const pipe = (...functions) => input => functions.reduce(
(acc, fn) => fn(acc),
input
);

// On crée des fonctions pour multiplier par un facteur donné
const multiply6 = pipe(double, triple);
const multiply9 = pipe(triple, triple);
const multiply24 = pipe(double, triple, quadruple);

// Utilisation
multiply6(6); // 36
multiply9(9); // 81
multiply16(16); // 256
multiply24(10); // 240
```
```Use functools.reduce() to perform right-to-left function composition. The last (rightmost) function can accept one or more arguments; the remaining functions must be unary.
from functools import reduce

1.def compose(*fns):
2.return reduce(lambda f, g: lambda *args: f(g(*args)), fns)
EXAMPLES
add5 = lambda x: x + 5
multiply = lambda x, y: x * y

```
star

Sat Feb 12 2022 14:41:52 GMT+0000 (Coordinated Universal Time) https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce

#function #composition
star

Mon Apr 20 2020 13:38:08 GMT+0000 (Coordinated Universal Time) https://www.30secondsofcode.org/python/s/compose/

#python #python #function #composition

#### Save snippets that work with our extensions   