ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
[TOC] ## compose 将多个函数合并成一个函数,**从右到左执行**。 ``` R.compose(Math.abs, R.add(1), R.multiply(2))(-4) // 7 ``` ## pipe 将多个函数合并成一个函数,从左到右执行 ``` var negative = x => -1 * x; var increaseOne = x => x + 1; var f = R.pipe(Math.pow, negative, increaseOne); f(3, 4) // -80 => -(3^4) + 1 ``` ## curry 将多参数的函数,转换成单参数的形式 ``` var addFourNumbers = (a, b, c, d) => a + b + c + d; var curriedAddFourNumbers = R.curry(addFourNumbers); var f = curriedAddFourNumbers(1, 2); var g = f(3); g(4) // 10 ``` ## memoize 返回一个函数,会缓存每一次的运行结果 ``` var productOfArr = arr => { var product = 1; arr.forEach(i => product *= i); return product; }; var count = 0; var factorial = R.memoize(n => { count += 1; return productOfArr(R.range(1, n + 1)); }); factorial(5) // 120 factorial(5) // 120 factorial(5) // 120 count // 1 ``` ## zipWith ``` var f = (x, y) => { // ... }; R.zipWith(f, [1, 2, 3])(['a', 'b', 'c']) // [f(1, 'a'), f(2, 'b'), f(3, 'c')] ```