Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

219
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda