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

218
Views
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 answers
Answer question

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 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!