Ramda 常用函数(快速上手)
标签:
JavaScript
Ramda 常用函数
Ramda 一款实用的 JavaScript 函数式编程库。
Ramda 库信息非常大,只对常用函数进行整理总结,方便快速上手使用
R.type([]);
// "Array"
R.uniq([1, 1, 2, 1]);
// [1, 2]
R.equals([1, 2, 3])([1, 2, 3]);
// true
var user = {name: 'Lucy', contact: {email:'lucy@gmail.com'}};
var newUser = R.clone(user);
newUser.name = 'Tom';
newUser.contact.email = 'tom@yahoo.com';
user
// {"contact": {"email": "lucy@gmail.com"}, "name": "Lucy"}
var gt10 = x => x > 10;
var even = x => x % 2 === 0;
var f = R.both(gt10, even);
f(15); // false
f(30); // true
var negative = x => -1 * x;
var increase = x => x + 1;
var f = R.pipe(Math.pow, negative, increase);
f(3, 4); // -80 => -(3^4) + 1
var add3nums = (a, b, c) => a + b + c;
var curriedAdd3n = R.curry(add3nums);
var f = curriedAdd3nums(1, 2);
f(3);
// 6
R.contains({name: 'Lucy'})([{name: 'Fred'}, {name: 'Lucy'}]);
// true
R.without([1, 2])([1, 2, 2, 3, 4]);
// [3, 4]
R.flatten([1, 2, [3, 4], 5, [6, [7]]]);
// [1, 2, 3, 4, 5, 6, 7]
R.intersection([1, 2, 3])([4, 3, 2]);
// [2, 3]
R.difference([{a: 1}, {b: 2}])([{a: 1}, {c: 3}]);
// [{b: 2}]
R.symmetricDifference([1, 2, 3])([5, 4, 3, 2]);
// [1, 4, 5]
R.union([1, 2, 3], [2, 3, 4]);
// [1, 2, 3, 4]
R.pluck('uname')([{id: 1, uname: 'Lucy'}, {id: 2, uname: 'Tom'}]);
// ['Lucy', 'Tom']
var isEven = n => n % 2 === 0;
R.filter(isEven)({a: 1, b: 2, c: 3, d: 4});
// {b: 2, d: 4}
var isEven = n => n % 2 === 0;
R.reject(isEven)([1, 2, 3, 4]);
// [1, 3]
R.pick(['a', 'd'])({a: 1, b: 2, c: 3, d: 4});
// {a: 1, d: 4}
var isUpperCase = (val, key) => key.toUpperCase() === key;
R.pickBy(isUpperCase)({a: 1, b: 2, A: 3, B: 4});
// {A: 3, B: 4}
var users = {first: 'Lucy', second: 'Tom', third: 'Lucy'};
R.invertObj(users);
// {"Lucy": "third", "Tom": "second"}
R.mergeDeepRight(
{name: 'Lucy', contact: {email:'lucy@gmail.com'}},
{contact: {email: 'lucy@yahoo.com'}, age: 23}
);
// {"age": 23, "contact": {"email": "lucy@yahoo.com"}, "name": "Lucy"}
var tomato = {
firstName: ' Tomato ',
data: {elapsed: 100, remaining: 1400}
};
var transformations = {
firstName: R.trim,
data: {elapsed: R.add(1), remaining: R.add(-1)}
};
R.evolve(transformations)(tomato)
// {
// firstName: 'Tomato',
// data: {elapsed: 101, remaining: 1399},
// }
R.prop('x')({x: 100});
// 100
let count = 0;
const factorial = R.memoize(n => {
count += 1;
return R.product(R.range(1, n + 1));
});
factorial(5); // 120
factorial(5); // 120
factorial(5); // 120
count; // 1, 不用 R.memoize 时 count 值为 3
var hasName = R.has('name');
hasName({name: 'Lucy'});
// true
hasName({id: 1});
// false
var double = x => x * 2;
R.map(double)({x: 1, y: 2, z: 3});
// {x: 2, y: 4, z: 6}
var mySubtract = function (a, b) {
return a - b;
};
R.reduce(mySubtract, 0)([1, 2, 3, 4]);
// -10
var sortByName = R.sortBy(
R.compose(R.toLower, R.prop('name'))
);
var alice = {name: 'ALICE', age: 101};
var bob = {name: 'Bob', age: -10};
var clara = {name: 'clara', age: 314.159};
sortByName([clara, bob, alice]);
// [alice, bob, clara]
点击查看更多内容
3人点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦