Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

173
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda