Skip to content

Commit 1f5aff0

Browse files
committed
增加ramda的示例代码
1 parent 984bf49 commit 1f5aff0

File tree

2 files changed

+94
-3
lines changed

2 files changed

+94
-3
lines changed

basicSyntaxSample/ramda.js

+93-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @Author: Ping Qixing
33
* @Date: 2017-07-03 08:44:15
44
* @Last Modified by: Ping Qixing
5-
* @Last Modified time: 2017-07-03 13:08:26
5+
* @Last Modified time: 2017-07-04 13:57:31
66
* @Description
77
*/
88
import R from 'ramda';
@@ -34,4 +34,95 @@ let increaseOne = x => x + 1;
3434
// 从左到右执行
3535
let fn = R.pipe(Math.pow, negative, increaseOne);
3636
let r3 = fn(3, 4);
37-
console.log(r3);
37+
38+
// 接受两个参数,第一个参数是函数,第二个参数是函数数组。
39+
// 传入的值先使用第二个参数包含的函数分别处理以后,再用第一个参数处理前一步生成的结果。
40+
let sumOfArr = arr => {
41+
let sum = 0;
42+
arr.forEach(i => sum += i);
43+
return sum;
44+
}
45+
46+
let lenghtOfArr = arr => arr.length;
47+
let average = R.converge(R.divide, [sumOfArr, lenghtOfArr]);
48+
let r4 = average([1, 2, 3, 4, 5, 6, 7]);
49+
// 相当于 28 除以 7
50+
51+
// 柯里化(将多参数的函数,转换成单参数的形式)
52+
let addFourNumbers = (a, b, c, d) => a + b + c + d;
53+
let curriedAddFourNumbers = R.curry(addFourNumbers);
54+
let fn2 = curriedAddFourNumbers(1, 2);
55+
let gn = fn2(3);
56+
gn(4); // 10
57+
58+
// 允许多参数的函数接受一个数组,指定最左边的部分参数。
59+
// partialRight,指定最右边的参数
60+
let greet = (salutation, title, firstName, lastName) => `${salutation}, ${title} ${firstName} ${lastName}!`;
61+
let sayHello = R.partial(greet, ['Hello']);
62+
let sayHelloToMs = R.partial(sayHello, ['Ms.']);
63+
let r5 = sayHelloToMs('Jane', 'Jones');
64+
65+
// useWith:接受一个函数fn和一个函数数组fnList作为参数,返回fn的柯里化版本。
66+
// 该新函数的参数,先分别经过对应的fnList成员处理,再传入fn执行。
67+
let decreaseOne = x => x - 1;
68+
let r6 = R.useWith(Math.pow, [decreaseOne, increaseOne])(3, 4);
69+
70+
// memoize:返回一个函数,会缓存每一次的运行结果。
71+
// complement:返回一个新函数,如果原函数返回true,该函数返回false;如果原函数返回false,该函数返回true。
72+
73+
// 将一个值传入指定函数,并返回该值。
74+
let sayX = x => console.log('x is ' + x);
75+
R.tap(sayX)(100) // 100
76+
R.pipe(
77+
R.assoc('a', 10),
78+
R.tap(console.log),
79+
R.assoc('a', 3)
80+
)({a: 1});
81+
82+
// zipWith:将两个数组对应位置的值,一起作为参数传入某个函数。
83+
// apply:将数组转成参数序列,传入指定函数。
84+
// applySpec:返回一个模板函数,该函数会将参数传入模板内的函数执行,然后将执行结果填充到模板。
85+
let getMetrics = R.applySpec({
86+
sum: R.add,
87+
nested: { mul: R.multiply }
88+
});
89+
90+
let r7 = getMetrics(2, 4);
91+
92+
// ascend:返回一个升序排列的比较函数,主要用于排序。
93+
// descend:返回一个降序排列的比较函数,主要用于排序。
94+
95+
let isEven = n => n % 2 === 0;
96+
let isOdd = (n) => n % 2 === 1;
97+
let r8 = R.reject(isOdd)([1, 2, 3, 4]) // [2, 4]
98+
let r9 = R.filter(isOdd)([1, 2, 3, 4]);
99+
100+
// reduce是三种运算的合成:
101+
// 遍历
102+
// 变形
103+
// 累积
104+
let arr = [1, 2, 3];
105+
let arr2 = arr.reduce((newArr, x) => {
106+
newArr.push(x + 1);
107+
return newArr;
108+
}, []);
109+
110+
console.log(arr2);
111+
112+
// 而transduce就是执行`变形`和`累积`两个运算,让代码具备更高的复用性。
113+
114+
// 变形运算
115+
let plusOne = x => x + 1;
116+
117+
// 累积运算
118+
let append = (newArr, x) => {
119+
newArr.push(x);
120+
return newArr;
121+
};
122+
123+
let r10 = R.transduce(R.map(plusOne), append, [], arr);
124+
console.log(r10);
125+
126+
// or
127+
let r11 = R.into([], R.map(R.add(1)), arr);
128+
console.log(r11);

webpack.config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const webpack = require('webpack');
33
const HtmlWebpackPlugin = require('html-webpack-plugin');
44
const OpenBrowserPlugin = require('open-browser-webpack-plugin');
55

6-
const PORT = 8888;
6+
const PORT = 9999;
77

88
module.exports = {
99
entry: path.resolve(__dirname, './basicSyntaxSample/ramda.js'),

0 commit comments

Comments
 (0)