R.length([]);
R.length([1, 2, 3]);
adjust(对指定索引的元素执行操作)
(a → a) → Number → [a] → [a]
R.adjust(R.add(10), 1, [1, 2, 3]);
R.adjust(R.add(10))(1)([1, 2, 3]);
all(所有成员是否满足判定规则)
(a → Boolean) → [a] → Boolean
var equals3 = R.equals(3);
R.all(equals3)([3, 3, 3, 3]);
R.all(equals3)([3, 3, 1, 3]);
any(是否有成员满足判定规则)
(a → Boolean) → [a] → Boolean
var lessThan0 = R.flip(R.lt)(0);
var lessThan2 = R.flip(R.lt)(2);
R.any(lessThan0)([1, 2]);
R.any(lessThan2)([1, 2]);
none(是否所有成员都不满足判定规则)
(a → Boolean) → [a] → Boolean
var isEven = n => n % 2 === 0;
var isOdd = n => n % 2 === 1;
R.none(isEven, [1, 3, 5, 7, 9, 11]);
R.none(isOdd, [1, 3, 5, 7, 8, 11]);
aperture(指定数量的数组分割重组)
Number → [a] → [[a]]
R.aperture(2, [1, 2, 3, 4, 5]);
R.aperture(3, [1, 2, 3, 4, 5]);
R.aperture(7, [1, 2, 3, 4, 5]);
append(数组后置添加Array.prototype.push)
a → [a] → [a]
R.append('tests', ['write', 'more']);
R.append('tests', []);
R.append(['tests'], ['write', 'more']);
prepend(数组前置添加Array.prototype.unshift)
a → [a] → [a]
R.prepend('fee', ['fi', 'fo', 'fum']);
chain(每个成员执行指定链式操作)
Chain m => (a → m b) → m a → m b
var duplicate = n => [n, n];
R.chain(duplicate, [1, 2, 3]);
R.chain(R.append, R.head)([1, 2, 3]);
concat(合并)
[a] → [a] → [a]
String → String → String
R.concat('ABC', 'DEF');
R.concat([4, 5, 6], [1, 2, 3]);
R.concat([], []);
contains(列表是否包含指定元素)
a → [a] → Boolean
R.contains(3, [1, 2, 3]);
R.contains(4, [1, 2, 3]);
R.contains({ name: 'Fred' }, [{ name: 'Fred' }]);
R.contains([42], [[42]]);
drop(删除前n个元素)
Number → [a] → [a]
Number → String → String
R.drop(1, ['foo', 'bar', 'baz']);
R.drop(2, ['foo', 'bar', 'baz']);
R.drop(3, ['foo', 'bar', 'baz']);
R.drop(4, ['foo', 'bar', 'baz']);
R.drop(3, 'ramda');
dropWhile(删除符合规则的前n个元素)
(a → Boolean) → [a] → [a]
(a → Boolean) → String → String
var lteTwo = x => x <= 2;
R.dropWhile(lteTwo, [1, 2, 3, 4, 3, 2, 1]);
R.dropWhile(x => x !== 'd' , 'Ramda');
dropLast(删除后n个元素)
Number → [a] → [a]
Number → String → String
R.dropLast(1, ['foo', 'bar', 'baz']);
R.dropLast(2, ['foo', 'bar', 'baz']);
R.dropLast(3, ['foo', 'bar', 'baz']);
R.dropLast(4, ['foo', 'bar', 'baz']);
R.dropLast(3, 'ramda');
dropLastWhile(删除符合规则的后n个元素)
(a → Boolean) → [a] → [a]
(a → Boolean) → String → String
var lteThree = x => x <= 3;
R.dropLastWhile(lteThree, [1, 2, 3, 4, 3, 2, 1]);
R.dropLastWhile(x => x !== 'd' , 'Ramda');
dropRepeats(去除连续重复元素)
[a] → [a]
R.dropRepeats([1, 1, 1, 2, 3, 4, 4, 2, 2]);
dropRepeatsWith(指定规则去除连续重复元素)
((a, a) → Boolean) → [a] → [a]
var l = [1, -1, 1, 3, 4, -4, -4, -5, 5, 3, 3];
R.dropRepeatsWith(R.eqBy(Math.abs), l);
endsWith(判断是否以指定元素结尾)
[a] → Boolean
String → Boolean
R.endsWith('c', 'abc')
R.endsWith('b', 'abc')
R.endsWith(['c'], ['a', 'b', 'c'])
R.endsWith(['b'], ['a', 'b', 'c'])
startsWith(判断是否以指定元素开始)
[a] → Boolean
String → Boolean
R.startsWith('a', 'abc')
R.startsWith('b', 'abc')
R.startsWith(['a'], ['a', 'b', 'c'])
R.startsWith(['b'], ['a', 'b', 'c'])
filter(符合规则的成员集合)
Filterable f => (a → Boolean) → f a → f a
var isEven = n => n % 2 === 0;
R.filter(isEven, [1, 2, 3, 4]);
R.filter(isEven, {a: 1, b: 2, c: 3, d: 4});
reject(不符合规则的成员集合)
Filterable f => (a → Boolean) → f a → f a
var isOdd = (n) => n % 2 === 1;
R.reject(isOdd, [1, 2, 3, 4]);
R.reject(isOdd, {a: 1, b: 2, c: 3, d: 4});
find(正序查找第一个符合规则的元素)
(a → Boolean) → [a] → a | undefined
var xs = [{a: 1}, {a: 2}, {a: 3}, {a: 2}];
R.find(R.propEq('a', 2))(xs);
R.find(R.propEq('a', 4))(xs);
findIndex(正序查找第一个符合规则的元素索引)
(a → Boolean) → [a] → Number
var xs = [{a: 1}, {a: 2}, {a: 3}, {a: 2}];
R.findIndex(R.propEq('a', 2))(xs);
R.findIndex(R.propEq('a', 4))(xs);
findLast(倒序查找第一个符合规则的元素)
(a → Boolean) → [a] → a | undefined
var xs = [{a: 1, b: 0}, {a:1, b: 1}];
R.findLast(R.propEq('a', 1))(xs);
R.findLast(R.propEq('a', 4))(xs);
findLastIndex(倒序查找第一个符合规则的元素索引)
(a → Boolean) → [a] → Number
var xs = [{a: 1, b: 0}, {a:1, b: 1}];
R.findLastIndex(R.propEq('a', 1))(xs);
R.findLastIndex(R.propEq('a', 4))(xs);
flatten(多维转一维)
[a] → [b]
R.flatten([1, 2, [3, 4], 5, [6, [7, 8, [9, [10, 11], 12]]]]);
forEach(遍历数组)
(a → *) → [a] → [a]
var printXPlusFive = x => console.log(x + 5);
R.forEach(printXPlusFive, [1, 2, 3]);
fromPairs(数组转对象)
[[k,v]] → {k: v}
R.fromPairs([['a', 1], ['b', 2], ['c', 3]]);
groupBy(所有成员指定规则分组)
(a → String) → [a] → {String: [a]}
var byGrade = R.groupBy(function(student) {
var score = student.score;
return score < 65 ? 'F' :
score < 70 ? 'D' :
score < 80 ? 'C' :
score < 90 ? 'B' : 'A';
var students = [{name: 'Abby', score: 84},
{name: 'Eddy', score: 58},
{name: 'Jack', score: 69}];
byGrade(students);
groupWith(相邻元素指定规则分组)
((a, a) → Boolean) → [a] → [[a]]
R.groupWith(R.equals, [0, 1, 1, 2, 3, 5, 8, 13, 21])
R.groupWith((a, b) => a + 1 === b, [0, 1, 1, 2, 3, 5, 8, 13, 21])
R.groupWith((a, b) => a % 2 === b % 2, [0, 1, 1, 2, 3, 5, 8, 13, 21])
head(第一个元素)
[a] → a | Undefined
String → String
R.head(['fi', 'fo', 'fum']);
R.head([]);
R.head('abc');
R.head('');
last(最后一个元素)
[a] → a | Undefined
String → String
R.last(['fi', 'fo', 'fum']);
R.last([]);
R.last('abc');
R.last('');
indexBy(指定属性生成新对象)
(a → String) → [{k: v}] → {k: {k: v}}
var list = [{id: 'xyz', title: 'A'}, {id: 'abc', title: 'B'}];
R.indexBy(R.prop('id'), list);
indexOf(正序查找元素在数组中第一次出现的位置)
a → [a] → Number
R.indexOf(3, [1,2,3,4]);
R.indexOf(10, [1,2,3,4]);
lastIndexOf(倒序查找元素在数组中第一次出现的位置)
a → [a] → Number
R.lastIndexOf(3, [-1,3,3,0,1,2,3,4]);
R.lastIndexOf(10, [1,2,3,4]);
init(去尾)
[a] → [a]
String → String
R.init([1, 2, 3]);
R.init([1, 2]);
R.init([1]);
R.init([]);
R.init('abc');
R.init('ab');
R.init('a');
R.init('');
tail(去头)
[a] → [a]
String → String
R.tail([1, 2, 3]);
R.tail([1, 2]);
R.tail([1]);
R.tail([]);
R.tail('abc');
R.tail('ab');
R.tail('a');
R.tail('');
insert(指定索引处插入一个值)
Number → a → [a] → [a]
R.insert(2, 'x', [1,2,3,4]);
insertAll(指定索引处插入一个数组的所有值)
Number → [a] → [a] → [a]
R.insertAll(2, ['x','y','z'], [1,2,3,4]);
intersperse(成员间插入间隔值)
a → [a] → [a]
R.intersperse('n', ['ba', 'a', 'a']);
into(原数组处理后结果与目标对象合并)
a → (b → b) → [c] → a
var numbers = [1, 2, 3, 4];
var transducer = R.compose(R.map(R.add(1)), R.take(2));
R.into([], transducer, numbers);
var intoArray = R.into("jsfkd");
intoArray(transducer, numbers);
join(数组转字符串)
String → [a] → String
var spacer = R.join(' ');
spacer(['a', 2, 3.4]);
R.join('|', [1, 2, 3]);
map(每个数组成员执行指定操作)
Functor f => (a → b) → f a → f b
var double = x => x * 2;
R.map(double, [1, 2, 3]);
R.map(double, {x: 1, y: 2, z: 3});
mapAccum(元素左侧累加)
((acc, x) → (acc, y)) → acc → [x] → (acc, [y])
var digits = ['1', '2', '3', '4'];
var appender = (a, b) => [a + b, a + b];
R.mapAccum(appender, 0, digits);
var digits = ['1', '2', '3', '4'];
var appender = (a, b) => [a+1, a + b];
R.mapAccum(appender, 0, digits);
mapAccumRight(元素右侧累加)
((x, acc) → (acc, y)) → acc → [x] → ([y], acc)
var digits = ['1', '2', '3', '4'];
var append = (a, b) => [a + b, a + b];
R.mapAccumRight(append, 5, digits);
var digits = ['1', '2', '3', '4'];
var append = (a, b) => [b+1, a + b];
R.mapAccumRight(append, 5, digits);
mergeAll(对象数组成员合并为一个对象)
[{k: v}] → {k: v}
R.mergeAll([{foo:1},{bar:2},{baz:3}]);
R.mergeAll([{foo:1},{foo:2},{bar:2}]);
nth(指定索引处的元素)
Number → [a] → a | Undefined
Number → String → String
var list = ['foo', 'bar', 'baz', 'quux'];
R.nth(1, list);
R.nth(-1, list);
R.nth(-99, list);
R.nth(2, 'abc');
R.nth(3, 'abc');
pair(生成数组)
a → b → [a,b]
R.pair('foo', 'bar');
partition(规则过滤)
Filterable f => (a → Boolean) → f a → [f a, f a]
R.partition(R.contains('s'), ['sss', 'ttt', 'foo', 'bars']);
R.partition(R.contains('s'), { a: 'sss', b: 'ttt', foo: 'bars' });
pluck(获取指定属性名的属性值集合)
Functor f => k → f {k: v} → f v
R.pluck('a')([{a: 1}, {a: 2}]);
R.pluck(0)([[1, 2], [3, 4]]);
R.pluck('val', {a: {val: 3}, b: {val: 5}});
range(范围内数值集合)
Number → Number → [Number]
R.range(1, 5);
R.range(50, 53);
scan(递归操作返回数组)
((a, b) → a) → a → [b] → [a]
var numbers = [1, 2, 3, 4];
var factorials = R.scan(R.multiply, 1, numbers);
reduce(递归操作返回最终值)
((a, b) → a) → a → [b] → a
R.reduce(R.subtract, 0, [1, 2, 3, 4])
reduceBy(分组递归操作)
((a, b) → a) → a → (b → String) → [b] → {String: a}
var reduceToNamesBy = R.reduceBy((acc, student) => acc.concat(student.name), []);
var namesByGrade = reduceToNamesBy(function(student) {
var score = student.score;
return score < 65 ? 'F' :
score < 70 ? 'D' :
score < 80 ? 'C' :
score < 90 ? 'B' : 'A';
var students = [{name: 'Lucy', score: 92},
{name: 'Drew', score: 85},
{name: 'Bart', score: 62}];
namesByGrade(students);
reduced(递归终止)
a → *
R.reduce(
(acc, item) => item > 3 ? R.reduced(acc) : acc.concat(item),
[1, 2, 3, 4, 5])
reduceRight(倒序递归操作)
((a, b) → b) → b → [a] → b
R.reduceRight(R.subtract, 0, [1, 2, 3, 4])
reduceWhile(不符合条件终止递归操作)
((a, b) → Boolean) → ((a, b) → a) → a → [b] → a
var isOdd = (acc, x) => x % 2 === 1;
var xs = [1, 3, 5, 60, 777, 800];
R.reduceWhile(isOdd, R.add, 0, xs);
var ys = [2, 4, 6]
R.reduceWhile(isOdd, R.add, 111, ys);
remove(指定索引开始删除n个元素)
Number → Number → [a] → [a]
R.remove(2, 3, [1,2,3,4,5,6,7,8]);
repeat(重复n次值)
a → n → [a]
R.repeat('hi', 5);
var obj = {};
var repeatedObjs = R.repeat(obj, 5);
repeatedObjs[0] === repeatedObjs[1];
times(重复n次函数)
(Number → a) → Number → [a]
R.times(R.identity, 5);
reverse(翻转操作)
[a] → [a]
String → String
R.reverse([1, 2, 3]);
R.reverse([1, 2]);
R.reverse([1]);
R.reverse([]);
R.reverse('abc');
R.reverse('ab');
R.reverse('a');
R.reverse('');
slice(截取操作)
Number → Number → [a] → [a]
Number → Number → String → String
R.slice(1, 3, ['a', 'b', 'c', 'd']);
R.slice(1, Infinity, ['a', 'b', 'c', 'd']);
R.slice(0, -1, ['a', 'b', 'c', 'd']);
R.slice(-3, -1, ['a', 'b', 'c', 'd']);
R.slice(0, 3, 'ramda');
sort(数组排序)
((a, a) → Number) → [a] → [a]
var diff = function(a, b) { return a - b; };
R.sort(diff, [4,2,7,5]);
splitAt(指定索引处分割数组)
Number → [a] → [[a], [a]]
Number → String → [String, String]
R.splitAt(1, [1, 2, 3]);
R.splitAt(5, 'hello world');
R.splitAt(-1, 'foobar');
splitEvery(指定数量分割数组)
Number → [a] → [[a]]
Number → String → [String]
R.splitEvery(3, [1, 2, 3, 4, 5, 6, 7]);
R.splitEvery(3, 'foobarbaz');
splitWhen(规则第一次通过处分割数组)
(a → Boolean) → [a] → [[a], [a]]
R.splitWhen(R.equals(2), [1, 2, 3, 1, 2, 3]);
take(正序取n个值)
Number → [a] → [a]
Number → String → String
R.take(1, ['foo', 'bar', 'baz']);
R.take(2, ['foo', 'bar', 'baz']);
R.take(3, ['foo', 'bar', 'baz']);
R.take(4, ['foo', 'bar', 'baz']);
R.take(3, 'ramda');
takeWhile(正序取符合规则的值集合)
(a → Boolean) → [a] → [a]
(a → Boolean) → String → String
var isNotFour = x => x !== 4;
R.takeWhile(isNotFour, [1, 2, 3, 4, 3, 2, 1]);
R.takeWhile(x => x !== 'd' , 'Ramda');
takeLast(倒序取n个值)
Number → [a] → [a]
Number → String → String
R.takeLast(1, ['foo', 'bar', 'baz']);
R.takeLast(2, ['foo', 'bar', 'baz']);
R.takeLast(3, ['foo', 'bar', 'baz']);
R.takeLast(4, ['foo', 'bar', 'baz']);
R.takeLast(3, 'ramda');
takeLastWhile(倒序取符合规则的值集合)
(a → Boolean) → [a] → [a]
(a → Boolean) → String → String
var isNotOne = x => x !== 1;
R.takeLastWhile(isNotOne, [1, 2, 1, 3, 4]);
R.takeLastWhile(x => x !== 'R' , 'Ramda');
transduce(数组迭代转换)
(c → c) → ((a, b) → a) → a → [b] → a
var numbers = [1, 2, 3, 4];
var transducer = R.compose(R.map(R.add(1)), R.take(2));
R.transduce(transducer, R.flip(R.append), [], numbers);
var isOdd = (x) => x % 2 === 1;
var firstOddTransducer = R.compose(R.filter(isOdd), R.take(5));
R.transduce(firstOddTransducer, R.flip(R.append), [], R.range(0, 100));
transpose(相同索引的值集合)
[[a]] → [[a]]
R.transpose([[1, 'a'], [2, 'b'], [3, 'c']])
R.transpose([[1, 2, 3], ['a', 'b', 'c']])
R.transpose([[10, 11], [20], [], [30, 31, 32]])
unfold(指定规则的迭代操作)
(a → [b]) → * → [b]
var f = n => n > 50 ? false : [-n, n + 10];
R.unfold(f, 10);
uniq(数组去重)
[a] → [a]
R.uniq([1, 1, 2, 1]);
R.uniq([1, '1']);
R.uniq([[42], [42]]);
uniqBy(指定操作去重)
(a → b) → [a] → [a]
R.uniqBy(Math.abs, [-1, -5, 2, 10, 1, 2]);
uniqWith(指定规则去重)
((a, a) → Boolean) → [a] → [a]
var strEq = R.eqBy(String);
R.uniqWith(strEq)([1, '1', 2, 1]);
R.uniqWith(strEq)([{}, {}]);
R.uniqWith(strEq)([1, '1', 1]);
R.uniqWith(strEq)(['1', 1, 1]);
unnest(降维操作)
Chain c => c (c a) → c a
R.unnest([1, [2], [[3]]]);
R.unnest([[1, 2], [3, 4], [5, 6]]);
update(修改指定索引处的值)
Number → a → [a] → [a]
R.update(1, 11, [0, 1, 2]);
R.update(1)(11)([0, 1, 2]);
without(去除与指定数组元素相同的所有元素)
[a] → [a] → [a]
R.without([1, 2], [1, 2, 1, 3, 4]);
xprod(数组混搭集合)
[a] → [b] → [[a,b]]
R.xprod([1, 2], ['a', 'b']);
zip(两个数组相同索引处的值匹配成新数组)
[a] → [b] → [[a,b]]
R.zip([1, 2, 3], ['a', 'b', 'c']);
R.zip([1, 2, 3, 4], ['a', 'b', 'c']);
zipObj(两个数组匹配成一个对象)
[String] → [*] → {String: *}
R.zipObj(['a', 'b', 'c'], [1, 2, 3]);
zipWith(两个数组相同索引处的值传入指定函数匹配成函数数组)
((a, b) → c) → [a] → [b] → [c]
var f = (x, y) => {
R.zipWith(f, [1, 2, 3], ['a', 'b', 'c']);
2018-11-27 11:49:10