How do you keep the every kth element of a list in Ramda?
How do you filter out every kth element of a list with Ramda?
input = [1, 2, 3, 4, 5, 6, 7, 8, 9]
output = keepKth(input, 3)
output = [1, 4, 7]
Solution 1:
This seemed to work:
let k = 3;
let Kth = (value, index) => (index % k == 0)
let filterKth = R.addIndex(R.filter)(Kth);
let input = [1, 2, 3, 4, 5, 6, 7, 8, 9];
let output = filterKth(input);
Solution 2:
A variation based on Scott's comment:
const keepEvery = k => compose(pluck(0), splitEvery(k));
keepEvery(3)([1, 2, 3, 4, 5, 6, 7, 8, 9]);
//=> [1, 4, 7]
- https://ramdajs.com/docs/#pluck
- https://ramdajs.com/docs/#splitEvery