Example 1 — Official Count Even Numbers
Count the even numbers in one list, then in each list of a nested list — the samples from the Ramda docs.
const even = x => x % 2 == 0;
console.log(R.count(even, [1, 2, 3, 4, 5])); // 2
console.log(R.map(R.count(even), [[1, 1, 1], [2, 3, 4, 5], [6]])); // [0, 2, 1] How It Works
- Predicate.
evenreturnstruefor numbers with no remainder when divided by 2. - Count. Only
2and4pass, so the first call returns2. - Curried.
R.count(even)has no list yet, so it returns a counter function. - Map.
R.mapruns that counter on each inner list:0,2, and1evens.
