Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

160
Views
Ramda.js: How to pass same argument when we compose

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
)
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

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>

about 4 years ago · Juan Pablo Isaza Report

0

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.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!