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

220
Visualizações
Ramda point-free problems with transform functions (map, etc)

I have this function, which is basically mapping request parameters and query parameters to a SQL statement:

function selectSingleResult(params, { order, limit, offset, fields, runset_id, txOffset, txFactor }) {
      const transformer = R.when(R.equals('values'), R.compose(knex.raw.bind(knex), applyConversion({ txOffset, txFactor })))
      const newFields = R.map(transformer , fields);

      knex('myTable').select(...newFields) // etc...

Ideally, I would like to be able to define transformer outside the function so the function can just become:

const mapFields = R.map(transformer);
function selectSingleResult(params, { order, limit, offset, fields, runset_id, txOffset, txFactor }) {
      knex('myTable').select(...mapFields(fields)) // etc...

The issue being that the applyConversion function needs arguments given to selectSingleResult.

This is a common issue I have with transform functions to map. They often require parameters other than the values being mapped over. In such cases, how can I write in point-free, or at least more testable style and not end up nesting functions so much?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

It feels as though you're trying to go point-free in code where it doesn't make much sense. But there's a contrasting notion of destructuring a large set of fields from the second parameter that doesn't seem necessary, so this seems mostly confused. (It might actually be necessary: perhaps you're using order, limit, offset, and runset_id in the remainder of your main function.)

I think you can accomplish almost what you're asking by just introducing one more layer of calls, with something like this:

const transformer = (query) => 
  R .when (R .equals ('values'), field => knex .raw (applyConversion (query) (field)))

const mapFields = R.pipe (transformer, map)

const selectSingleResult = (params, query) => {
  knex ('myTable') .select (... mapFields (query) (query .fields)) 
  // etc...
}

I name the second parameter to your main function query; it's a guess, and if that's confusing, replace all the instances of query with foo or with something meaningful to you.

Note that mapFields could also be written as const mapFields = (query) => R.map (transformer (query)), but the version above seems simpler. Also note that I simplified transformer a bit. I simply don't see any reason to try to go point-free, when you can't get all the way there. And trying to mix point-free code with OO constructs such as knex .raw just seems to confuse things.

If I read the code correctly, we also might rewrite transformer like this:

const transformer = (query) => (field) =>
  field == 'values' ? knex .raw (applyConversion (query) ('values')) : field

but I can't decide if that is an improvement or not.

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