[英]Ramda pipe with multiple arguments
I have a method which requires multiple arguments, and I am trying to set up a ramda pipe to handle it. 我有一个需要多个参数的方法,并且我正在尝试设置一个ramda管道来处理它。
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? 有没有更干净的方法可以做到这一点,特别是
x => x.number === number
filter
x => x.number === number
部分,必须在管道末端调用(array)
?
Here's a link to the code above loaded into the ramda repl. 这是到ramda repl中的上述代码的链接。
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. 在这里,我们把它换成使用
R.pluck
和匿名函数给R.find
与R.pathEq
给出谓词R.find
代替。 Once found, the value can be retrieved by walking down the properties of the object with R.path
. 一旦找到,就可以通过使用
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. 可以使用
R.useWith
以无点方式重写它,尽管我觉得过程中会失去可读性。
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
. 我认为使用
pluck
和prop
代替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
. 像
getAttachedValueByNumber
一样。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.