javascript - Ramda pipe with multiple arguments - Stack Overflow

I have a method which requires multiple arguments, and I am trying to set up a ramda pipe to handle it.

I have a method which requires multiple arguments, and I am trying to set up a ramda pipe to handle it.

Here's an example:

const R = require('ramda');
const input = [
  { data: { number: 'v01', attached: [ 't01' ] } },
  { data: { number: 'v02', attached: [ 't02' ] } },
  { data: { number: 'v03', attached: [ 't03' ] } },
]

const method = R.curry((number, array) => {
  return R.pipe(
    R.pluck('data'),
    R.find(x => x.number === number),
    R.prop('attached'),
    R.head
  )(array)
})

method('v02', input)

Is there a cleaner way of doing this, especially the x => x.number === number part of filter and having to call (array) at the end of the pipe?

Here's a link to the code above loaded into the ramda repl.

I have a method which requires multiple arguments, and I am trying to set up a ramda pipe to handle it.

Here's an example:

const R = require('ramda');
const input = [
  { data: { number: 'v01', attached: [ 't01' ] } },
  { data: { number: 'v02', attached: [ 't02' ] } },
  { data: { number: 'v03', attached: [ 't03' ] } },
]

const method = R.curry((number, array) => {
  return R.pipe(
    R.pluck('data'),
    R.find(x => x.number === number),
    R.prop('attached'),
    R.head
  )(array)
})

method('v02', input)

Is there a cleaner way of doing this, especially the x => x.number === number part of filter and having to call (array) at the end of the pipe?

Here's a link to the code above loaded into the ramda repl.

Share Improve this question edited Dec 16, 2016 at 23:41 dvlsg asked Dec 16, 2016 at 23:23 dvlsgdvlsg 5,5382 gold badges31 silver badges34 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

One way this could possibly be rewritten:

const method = R.curry((number, array) => R.pipe(
  R.find(R.pathEq(['data', 'number'], number)),
  R.path(['data', 'attached', 0])
)(array))

Here we've replaced the use of R.pluck and the anonymous function given to R.find with R.pathEq given as the predicate to R.find instead. Once found, the value can be retrieved by walking down the properties of the object with R.path.

It is possible to rewrite this in a point-free manner using R.useWith, though I feel readability gets lost in the process.

const method = R.useWith(
  R.pipe(R.find, R.path(['data', 'attached', 0])),
  [R.pathEq(['data', 'number']), R.identity]
)

I think readability could be improved using pluck and prop instead of path. Like this:

const method = R.useWith(
  R.pipe(R.find, R.prop('attached')),
  [R.propEq('number'), R.pluck('data')]
);

And of course, it's better to use a good name for the function. Like getAttachedValueByNumber.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742337587a4424961.html

相关推荐

  • javascript - Ramda pipe with multiple arguments - Stack Overflow

    I have a method which requires multiple arguments, and I am trying to set up a ramda pipe to handle it.

    7小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信