I am looking into functional programming where I have the following code so far:
const R = require('ramda')
const original = {
words: [
{ duration: '0.360000', name: 'Arthur', position: '0.660000' },
{ duration: '0.150000', name: 'the', position: '1.020000' },
{ duration: '0.380000', name: 'rat', position: '1.170000' },
{ duration: '0.770000', name: '.', position: '1.550000' }
]
}
// 1. convert position and duration to int and multiply by 100
const makeInteger = a => parseFloat(a) * 100
const words = R.lensPath(['words'])
log('position', R.over(words, R.map(R.over(position, makeInteger)), original).words)
returns:
position: [
{
duration: '0.360000',
name: 'Arthur',
position: 66
},
{
duration: '0.150000',
name: 'the',
position: 102
},
{
duration: '0.380000',
name: 'rat',
position: 117
},
{
duration: '0.770000',
name: '.',
position: 155
}
]
How do I modify both duration and position in the same function in order to make them integers?
Following that, I would have this function where I pass an index and update all the positions following that point.
Basically I like to shift the 'position' offset depending on the object where the 'duration' was modified?
const pos = R.over(words, R.map(R.over(position, makeInteger)), original)
const y = (i) => R.slice(i, Infinity, pos.words)
const foo = R.adjust(R.add(-2), 0, y(1))
log(foo)
And I got
[
NaN,
{
duration: '0.150000',
name: 'the',
position: 102
},
{
duration: '0.380000',
name: 'rat',
position: 117
},
{
duration: '0.770000',
name: '.',
position: 155
}
]
So I am kind of stuck on how to offset the position.
Any advice is much appreciated.