How to use the countBy function from ramda

Find comprehensive JavaScript ramda.countBy code examples handpicked from public code repositorys.

128
129
130
131
132
133
134
135
136
137
  }
}

setupB18(bname, version);
let counts = R.compose(
  R.countBy(R.identity),
  R.map(R.prop("color")),
  sortTiles,
  R.uniq,
  R.map(getTile)
fork icon52
star icon51
watch icon0

2
3
4
5
6
7
8
9
10
11
12
const nums = [1, 1, 1, 2, 2, 3];
let k = 2;


const result = R.pipe(
    // countBy is used to count the frequency by it's identity and returns as object
    R.countBy(R.identity),  // count using its identity[x => x] => {1:3, 2:2, 3:1} 
    R.toPairs,  // =>[[1,3], [2,2], [3,1]]
    R.sort((a, b) => b[1] - a[1]),  // sort using value =>[[1,3], [2,2], [3,1]]
    R.take(k),  // slice array => [[1,3], [2,2]]
    R.map(ele => ele[0]), // return all first elements as array => [1,2]
fork icon0
star icon1
watch icon0

+ 2 other calls in file

192
193
194
195
196
197
198
199
200
201
    console.error(`There are unspent output transactions being used more than once: unspent output transaction: '${R.keys(doubleSpendingList).join(', ')}'`);
    throw new BlockAssertionError(`There are unspent output transactions being used more than once: unspent output transaction: '${R.keys(doubleSpendingList).join(', ')}'`);
}

// Check if there is only 1 fee transaction and 1 reward transaction;
let transactionsByType = R.countBy(R.prop('type'), newBlock.transactions);
if (transactionsByType.fee && transactionsByType.fee > 1) {
    console.error(`Invalid fee transaction count: expected '1' got '${transactionsByType.fee}'`);
    throw new BlockAssertionError(`Invalid fee transaction count: expected '1' got '${transactionsByType.fee}'`);
}
fork icon0
star icon1
watch icon0

32
33
34
35
36
37
38
39
40
41

    done(Miner.proveWorkFor(Block.fromJson(input.jsonBlock), input.difficulty));
});

const transactionList = R.pipe(
    R.countBy(R.prop('type')),
    R.toString,
    R.replace('{', ''),
    R.replace('}', ''),
    R.replace(/"/g, '')
fork icon0
star icon1
watch icon0

7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
 * @return {Object} An object mapping keys to number of occurrences in the list.
 * @example
 *
 *      var numbers = [1.0, 1.1, 1.2, 2.0, 3.0, 2.2];
 *      var letters = R.split('', 'abcABCaaaBBc');
 *      R.countBy(Math.floor)(numbers);    //=> {'1': 3, '2': 2, '3': 1}
 *      R.countBy(R.toLower)(letters);   //=> {'a': 5, 'b': 4, 'c': 3}
 */
var countBy = reduceBy(function (acc, elem) {
    return acc + 1;
fork icon0
star icon0
watch icon0

+ 35 other calls in file

2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
 *
 *      const numbers = [1.0, 1.1, 1.2, 2.0, 3.0, 2.2];
 *      R.countBy(Math.floor)(numbers);    //=> {'1': 3, '2': 2, '3': 1}
 *
 *      const letters = ['a', 'b', 'A', 'a', 'B', 'c'];
 *      R.countBy(R.toLower)(letters);   //=> {'a': 3, 'b': 2, 'c': 1}
 */




var countBy =
fork icon0
star icon0
watch icon2

+ 7 other calls in file

14
15
16
17
18
19
20
21
22
23
24
let sum = 0;
lines
	.filter(x => {
		const cs = getChecksum(x);
		const name = getName(x);
		const hist = R.countBy(R.toLower)(name); // make a histogram of usage of each char in the string


		// the histogram needs to be reduced to top 5 (either by value or by alphabet)
		// first; make it into an array
		const arr = Object.keys(hist).map(key => ({ [key]: hist[key] }));
fork icon0
star icon0
watch icon0

Other functions in ramda

Sorted by popularity

function icon

ramda.clone is the most popular function in ramda (30311 examples)