// Lambdas sind so eine schöne Sache aber diese ständige Verkürzung
// soweit wie geht ist einfach nervig und birgt Fehler.
// Zwei Klammern für die Parameterliste und zwei für den Body kann man
// doch schreiben oder?
// Sie sind anderer Meinung? - is ok.
// Ich nehme an Sie wissen, dass Pfeilfunktionen rechts assoziativ sind.
// Trotzdem glaube ich nicht, dass Sie das hier lesen wollen:
curry = f => a => b => f(a, b)
uncurry = f => (a, b) => f(a)(b)
papply = (f, a) => b => f(a, b)
Quelle:
https://blog.benestudio.co/currying-in-javascript-es6-540d2ad09400
// Anderes und hoffentlich besseres Beispiel
// Nur zur Vorbereitung
const woerter = ['Hallo', 'heute', 'gestern'];
const processArray = function( items, fn){
const result = [];
items.forEach( (item) => result.push(fn(item)));
return result;
}
// vollständige Schreibweise mit Selbstausführung am Ende
const berechneWortlaenge = function(){
const map = processArray(woerter, function( wort ){
return { wort: wort, laenge: wort.length};
});
console.log('1. Ausgabe: ' +JSON.stringify(map));
}();
// Umgewandelt in lambda Ausdruck
const berechneWortlaenge = function(){
const map = processArray(woerter, ( wort ) =>{
return { wort: wort, laenge: wort.length};
});
console.log('2. Ausgabe: ' + JSON.stringify(map));
}();
// obsolete Klammern entfernt: Parameterliste, Body und
// return Schlüsselwort entfernt, da an letzter Stelle
// => compile Fehler weil das Ergebnisobjekt als Body
// interpretiert wird !!!
//const berechneWortlaenge = function(){
// const map = processArray(woerter, wort =>
// { wort: wort, laenge: wort.length}
// );
// console.log('3. Ausgabe: ' + JSON.stringify(map));
//}();
// Obsolete Klammern zur Umwandlung in Expression hinzugefügt
const berechneWortlaenge = function(){
const map = processArray(woerter, wort =>
({ wort: wort, laenge: wort.length})
);
console.log('4. Ausgabe: ' + JSON.stringify(map));
}();
Cheatographer
https://stackoverflow.com/users/story/373498
Metadata
Comments
No comments yet. Add yours below!
Add a Comment
Related Cheat Sheets
More Cheat Sheets by FunThomas424242