简体   繁体   English

变换函数(地图等)的 Ramda 无点问题

[英]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,它基本上是将请求参数和查询参数映射到 SQL 语句:

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:理想情况下,我希望能够在 function 之外定义transformer ,因此 function 可以变成:

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 .问题是applyConversion function 需要 arguments 给selectSingleResult

This is a common issue I have with transform functions to map .这是我在将函数转换为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?在这种情况下,我怎样才能以无点或至少更可测试的风格编写,而不至于过多地嵌套函数呢?

It feels as though you're trying to go point-free in code where it doesn't make much sense.感觉好像您正在尝试 go 在没有多大意义的代码中无点。 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.) (这实际上可能是必要的:也许您在主 function 的其余部分中使用了orderlimitoffsetrunset_id 。)

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 ;我将第二个参数命名为您的主要 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.这是一个猜测,如果这令人困惑,请将所有query实例替换为foo或对您有意义的内容。

Note that mapFields could also be written as const mapFields = (query) => R.map (transformer (query)) , but the version above seems simpler.请注意, mapFields也可以写为const mapFields = (query) => R.map (transformer (query)) ,但上面的版本似乎更简单。 Also note that I simplified transformer a bit.另请注意,我稍微简化了transformer I simply don't see any reason to try to go point-free, when you can't get all the way there.当您无法一直到达那里时,我根本看不出有任何理由尝试 go 无积分。 And trying to mix point-free code with OO constructs such as knex.raw just seems to confuse things.并且试图将无点代码与诸如knex.raw类的 OO 结构混合在一起似乎会使事情变得混乱。

If I read the code correctly, we also might rewrite transformer like this:如果我正确阅读了代码,我们还可以像这样重写transformer器:

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

but I can't decide if that is an improvement or not.但我无法决定这是否是一种改进。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM