Pipe on multiple data in ramda

北战南征 提交于 2019-12-25 08:13:54

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!