Example 1 — Official Samples
Remove trailing numbers that are at most 3, then trailing characters that are not 'd' — from the Ramda docs.
const lteThree = x => x <= 3;
console.log(R.dropLastWhile(lteThree, [1, 2, 3, 4, 3, 2, 1])); // [1, 2, 3, 4]
console.log(R.dropLastWhile(x => x !== 'd', 'Ramda')); // 'Ramd' How It Works
- From the right. The trailing
1,2,3passx <= 3, so they are removed. - Stop.
4fails the test, so the walk stops and[1, 2, 3, 4]is kept. - Strings. In
'Ramda', the last'a'is not'd', so it is removed. - Stop at d.
'd'failsx !== 'd', so the result is'Ramd'.
