Reputation: 153
I have to following code:
R.map(
object => ({
...object,
sth: R.compose(
R.filter(R.propEq('key', val)),
R.prop('sth')
)(object)
}),
array
)
Is there a way to write it cleaner, without passing literal function to map. We want to R.assoc the result of compose to object. But we need to pass the same arg to assoc as 3rd arg, because we get the correct value, but we need to use same object as source to make a copy from in assoc:
R.map(
R.compose(
R.assoc('sth'),
R.filter(R.propEq('key', val)),
R.prop('sth')
),
array
)
Upvotes: 0
Views: 165
Reputation: 191936
You can use R.evolve
to transform some of an object's properties without effecting the others:
const { map, evolve, filter, propEq } = R
const matchSth = val => map(evolve({
sth: filter(propEq('key', val))
}))
const arr = [{"foo":1,"sth":[{"key":2,"bar":"a"},{"key":1,"bar":"b"},{"key":2,"bar":"c"}],"baz":3},{"foo":2,"sth":[{"key":1,"bar":"c"},{"key":1,"bar":"d"},{"key":2,"bar":"e"}],"baz":5}]
console.log(matchSth(2)(arr))
.as-console-wrapper {max-height: 100% !important; top: 0}
<script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.27.1/ramda.min.js"></script>
Upvotes: 2
Reputation: 50787
Without some sample data, I'm mostly making a guess here. But I think this might do what you want:
const matchSth = (val) => map (
over (lensProp ('sth')) (filter (propEq ('key') (val)))
)
const arr = [
{foo: 1, sth: [{key: 2, bar: 'a'}, {key: 1, bar: 'b'}, {key: 2, bar: 'c'}, ], baz: 3},
{foo: 2, sth: [{key: 1, bar: 'c'}, {key: 1, bar: 'd'}, {key: 2, bar: 'e'}, ], baz: 5}
]
console .log (matchSth (2) (arr))
.as-console-wrapper {max-height: 100% !important; top: 0}
<script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.27.1/ramda.min.js"></script>
<script>const {map, over, lensProp, filter, propEq} = R </script>
Lenses are very useful abstractions, and here lensProp ('sth')
focuses our attention on the sth
property of its target. over
clones an object, except that it applies the function supplied to the value at that focus. Here we pass a function which filters those whose key
properties match the supplied value.
While we could certainly find a way to make this point-free, I find this readable and understandable as it is, and probably wouldn't bother pursuing it.
Upvotes: 1