Example 1 — Official Runs of Equal Numbers
Group neighbours that are equal — the first sample from the Ramda docs.
const fib = [0, 1, 1, 2, 3, 5, 8, 13, 21];
console.log(JSON.stringify(R.groupWith(R.equals, fib)));
// [[0],[1,1],[2],[3],[5],[8],[13],[21]] How It Works
- Pairwise test.
R.equalsalready takes two values, so it is the test itself. - Only one match. The pair
1, 1is the only equal neighbours, so it forms[1, 1]. - Everything else splits. Every other pair differs, so each number gets its own group.
- Nothing lost. Joining the groups gives back the original
fiblist.
