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. Here's an example: const R = require('ramda'); const input = [ { data: { n
Solution 1:
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]
)
Solution 2:
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
.
Post a Comment for "Ramda Pipe With Multiple Arguments"