Funktionen komponieren in JavaScript
Level 4256 Tastenanschläge
Funktionskomposition kombiniert mehrere Funktionen zu einer und wendet sie von rechts nach links an.
JavaScript-Code
// compose-Funktion mit reduceRight// Nimmt beliebig viele Funktionen und gibt eine neue Funktion zurück1const compose = (...fns) => (x) => fns.reduceRight((acc, fn) => fn(acc), x);// Einfache Transformationsfunktionen definieren2const add5 = x => x + 5;3const multiply2 = x => x * 2;4const subtract3 = x => x - 3;// Komponieren: erst add5, dann multiply2, dann subtract35const calc = compose(subtract3, multiply2, add5);// calc(10) = subtract3(multiply2(add5(10))) = (10+5)*2-3 = 276console.log(calc(10));// calc(0) = subtract3(multiply2(add5(0))) = (0+5)*2-3 = 77console.log(calc(0));Ausgabe
27 7
Übe, diesen Code zu tippen. Ergebnisse werden nicht in der Rangliste gespeichert.
Symbole in diesem Code
Die Symbole, die du in diesem JavaScript-Code tippst, und wie oft sie vorkommen.
=× 11(× 10)× 10;× 7.× 6>× 6,× 4-× 1*× 1+× 1