Example 1 — Official Samples
Take numbers until a 4 appears, and letters until a 'd' appears — both samples from the Ramda docs.
const isNotFour = x => x !== 4;
console.log(R.takeWhile(isNotFour, [1, 2, 3, 4, 3, 2, 1])); // [1, 2, 3]
console.log(R.takeWhile(x => x !== 'd', 'Ramda')); // 'Ram' How It Works
- Numbers.
1,2, and3pass;4fails, so the walk stops there. - Left out. The
4and the3, 2, 1after it are not in the result. - String.
'R','a','m'pass;'d'fails, so the result is'Ram'. - Same type. A list gives back a list and a string gives back a string.
