Example 1 — Official Sample with Math.abs
Treat a number and its negative as the same key — the sample from the Ramda docs.
const nums = [1, -1, -1, 2, 3, -4, 4, 2, 2];
console.log(R.dropRepeatsBy(Math.abs, nums)); // [1, 2, 3, -4, 2] How It Works
- Keys.
Math.absturns the list into keys1, 1, 1, 2, 3, 4, 4, 2, 2. - Runs.
1, -1, -1share key 1 and-4, 4share key 4, so each run keeps one item. - Original items. The first item of the key-4 run is
-4, so-4is kept, not4. - Neighbours only. The last
2follows a4, so it starts a new run and stays.
