Ramda常用函数


  1. 专为函数式编程风格设计的Javascript库;

  2. _: 柯里化函数占位符;

f(R.__,2,3)(1);
f(R.__,2)(1)(3);
f(R_,R.__,3)(1)(2);
  1. R.add: 两数相加;

  2. R.map

const double = x => x * 2;
?
R.map(double, [1, 2, 3]); //=> [2, 4, 6]
?
R.map(double, {x: 1, y: 2, z: 3}); //=> {x: 2, y: 4, z: 6}
  1. R.addIndex:获取函子的索引值

const mapIndexed = R.addIndex(R.map);
mapIndexed((val, idx) => idx + '-' + val, ['f', 'o', 'o', 'b', 'a', 'r']);
//=> ['0-f', '1-o', '2-o', '3-b', '4-a', '5-r']
  1. R.adjust:对函子指定索引处的应用函数

R.adjust(1, R.toUpper, ['a', 'b', 'c', 'd']);      //=> ['a', 'B', 'c', 'd']
R.adjust(-1, R.toUpper, ['a', 'b', 'c', 'd']);     //=> ['a', 'b', 'c', 'D']
  1. R.all:函子所有值应用函数后返回的值都为true,适用列表类型的数据

const equals3 = R.equals(3);
R.all(equals3)([3, 3, 3, 3]); //=> true
R.all(equals3)([3, 3, 1, 3]); //=> false
  1. R.allPass:函子所有值应用对应的函数后返回的值都为true,适用对象类型的数据

const isQueen = R.propEq('rank', 'Q');
const isSpade = R.propEq('suit', '??');
const isQueenOfSpades = R.allPass([isQueen, isSpade]);
?
isQueenOfSpades({rank: 'Q', suit: '??'}); //=> false
isQueenOfSpades({rank: 'Q', suit: '??'}); //=> true
  1. R.always:返回常量

const t = R.always('Tee');
t(); //=> 'Tee'
  1. R.any:与运算

  2. R.andThen:用于处理函数组合内的Promise,即接收一个函数(f)和一个Promise(p)对象为参数,如果p没发生错误,则执行 p.then(f);

  3. R.any:或运算;

  4. R.anyPass:类似于R.allPass;

  5. R.ap:用于列表类型的数据时,第一个参数为方法列表,第二个参数为数据;

    用于字符串类型的数据时,前两个参数为方法,第三个参数为数据;

R.ap([R.multiply(2), R.add(3)], [1,2,3]); //=> [2, 4, 6, 4, 5, 6]
R.ap([R.concat('tasty '), R.toUpper], ['pizza', 'salad']); //=> ["tasty pizza", "tasty salad", "PIZZA", "SALAD"]
?
// R.ap 可以用作 S组合子
// when only two functions are passed
R.ap(R.concat, R.toUpper)('Ramda') //=> 'RamdaRAMDA'
  1. R.aperture:用于列表类型数据,返回一个列表,列表元素为由原列表相邻元素组成的n元组;如果n>列表长度,返回空列表;

R.aperture(2, [1, 2, 3, 4, 5]); //=> [[1, 2], [2, 3], [3, 4], [4, 5]]
R.aperture(3, [1, 2, 3, 4, 5]); //=> [[1, 2, 3], [2, 3, 4], [3, 4, 5]]
R.aperture(7, [1, 2, 3, 4, 5]); //=> []
  1. 常用:

R.type([]);
// "Array"
?
R.uniq([1, 1, 2, 2]);
// [1,2]
?
R.equals([1,2,3], [1,2,3]);
// true
?
R.clone(obj);
// 返回克隆的新对象
?
R.both(f1, f2, data);
// f1(data) 和 f2(data) 都为真时返回true,否则返回false
?
const f = R.pipe(f1, f2, f3);
f(parameter1, parameter2);
// f1参数个数不限,其余函数必须为一元函数,返回的结果不会自动柯里化;
// 从左往右顺序执行;
?
f = (a, b, c) => a + b + c;
kf = R.curry(f);
// 函数的柯里化
?
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
// 对象是否包含指定属性
?
var double = x => x * 2;
R.map(double)({x: 1, y: 2, z: 3});
// {x: 2, y: 4, z: 6}
// map函数
?
var mySubtract = function (a, b) {
 return a - b;
};
R.reduce(mySubtract, 0)([1, 2, 3, 4]);
// -10
// reduce函数
?
const sortBy = R.sortBy(d => d[key]);
// 排序函数
?