Reputation: 845
So I have the following array
[true,true,false,false,false,true,false,false,true,true].
How can I use ramda to make the array look as follows
[true,false,true,false,true]
Also if the data was instead
[{t: '2018-10/09', v:true},{t: '2018-10/08', v:true}, {t: '2018-10/07', v:false},{t: '2018-10/06', v:false},{t: '2018-10/05', v:false},{t: '2018-10/04', v:true},{t: '2018-10/03', v:false},{t: '2018-10/02', v:false},{t: '2018-10/01', v:true},{t: '2018-09/09', v:true}]
and I wanted to dropRepeats using v only, then I would use R.dropRepeats(bool.v) ? Also as a side note, I am using R.pipe to get the input and do the transformations on the data.
Upvotes: 0
Views: 136
Reputation: 50797
Ramda happens to have a built-in function for that, dropRepeats
const bools = [true,true,false,false,false,true,false,false,true,true]
console.log(R.dropRepeats(bools))
<script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.js"></script>
A comment asked how I would do this on a slightly more complex structure. It would be much the same, using dropRepeatsWith
and eqProps
:
const bools = [{t: '2018-10/09', v:true},{t: '2018-10/08', v:true}, {t: '2018-10/07', v:false},{t: '2018-10/06', v:false},{t: '2018-10/05', v:false},{t: '2018-10/04', v:true},{t: '2018-10/03', v:false},{t: '2018-10/02', v:false},{t: '2018-10/01', v:true},{t: '2018-09/09', v:true}]
console.log(R.dropRepeatsWith(R.eqProps('v'), bools))
<script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.js"></script>
Upvotes: 4
Reputation: 18901
I would suggest a solution using groupWith
:
You use groupWith
to group consecutive identical elements into sub arrays, then you use head
in map
to take the first element of each sub array.
const prune = R.pipe(R.groupWith(R.equals), R.map(R.head));
console.log(
prune([true,true,false,false,false,true,false,false,true,true]));
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.js"></script>
Upvotes: 1