碎片時(shí)間學(xué)編程「362]:從左到右執(zhí)行組合反向撰寫功能

執(zhí)行從左到右的函數(shù)組合。??
使用 Array.prototype.reduce() 執(zhí)行從左到右的函數(shù)組合。
第一個(gè)(最左邊)函數(shù)可以接受一個(gè)或多個(gè)參數(shù);
其余函數(shù)必須是一元函數(shù)。
const composeRight = (...fns) => ?fns.reduce((f, g) => (...args) => g(f(...args)));
示例:
const add = (x, y) => x + y;const square = x => x * x;const addAndSquare = composeRight(add, square);addAndSquare(1, 2); // 9
更多內(nèi)容請(qǐng)?jiān)L問(wèn)我的網(wǎng)站:https://www.icoderoad.com
標(biāo)簽: