问题
How can I pipe on multiple data arrays? Ultimately I want to achieve something like this:
const data = [{id: 1, data:100}, {id: 2, data: 200}, {id: 3, data: 3000}, ... ]
I tried this, but didn't work:
pipe(
map(assoc('data', __, {})),
map(assoc('id', multiply(100, prop('data', __))))
)(range(1, 1000))
If the approach is to use two pipes, then there has to be some way to pipe over two different arrays simultaneously. How can this be implemented?
回答1:
I suggest this:
R.map(n => ({id: n, data: 100 * n}), R.range(1, 1000))
A point-free solution is available, but it's not elegant:
R.map(R.converge(R.merge,
[R.objOf('id'),
R.compose(R.objOf('data'), R.multiply(100))]),
R.range(1, 10))
回答2:
This is the best I could come up with in pointfree style, a bit hacky in that the number given to repeat should match the amount of keys given to zipObj. Extra keys could be added with an assoc after the evolve
map(
pipe(
repeat(__, 2),
zipObj(['data', 'id']),
evolve({
data: multiply(100)
})
)
)(range(1, 1000))
来源:https://stackoverflow.com/questions/42041473/pipe-on-multiple-data-in-ramda