如何用长参数列表实现无点风格 [英] How to do pointfree style with long parameter list

查看:153
本文介绍了如何用长参数列表实现无点风格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个创建Async工作流的函数,并且这个函数需要10个以咖喱风格为参数的参数。例如

  let createSequenceCore abcdefghij = 
异步{
...
}

我想创建另一个函数来启动这个工作流程,所以我得到了

  let startSequenceCore abcdefghij = 
Async.StartImmediate(createSequenceCore abcdefghij)

有什么办法可以摆脱那些冗余参数?我尝试了<< 运算符,但这只允许我删除一个。

  let startSequenceCore abcdefghi = 
Async.StartImmediate<< (createSequenceCore abcdefghi)

(我将Haskell和Scala加入了这个问题,即使代码本身是F#因为我真正想要的是如何做这种currying,这将适用于任何;我认为Haskell或Scala答案可以轻松地移植到F#,并且很可能被标记为正确的答案)。 >

注意合理地表明没有简单的解决方案可以获得赏金。






更新 geesh我不会给出100分的答案,而是回答问题而不是回答问题,即使它是最高的投票,所以在这里:

我有一个创建Async工作流的函数,以及以咖喱风格为参数的 4 参数的函数。例如

  let createSequenceCore abcd = 
异步{
...
}

我想创建另一个函数来启动这个工作流程,所以我得到了

  let startSequenceCore abcd = 
Async.StartImmediate(createSequenceCore abcd)

有什么办法可以摆脱那些冗余参数?我尝试了<< 运算符,但这只允许我删除一个。

  let startSequenceCore abc = 
Async.StartImmediate<< (createSequenceCore abc)


解决方案

10个参数听起来太多了。你如何创建一个包含10个属性的记录,或者可能是一个你不需要全部10个属性的DU?无论哪种方式,你最终会得到一个单一的参数,正常的功能组合如预期的那样工作。



编辑:当你居然需要它,您可以创建一个功能更强大的版本:<< >>< / code>

  let(<。<)f =(<<<)(<<<)(<< (< ...<< ...<<)<< ...< ;)f =(<<)(<)(<<)<)f 

let flip fab = fba
let(>。>) (>)> f = flip(<。<)f
let(> ...>) f = flip(< ...<)f

然后你可以写:

  let startSequenceCore = 
Async.StartImmediate< ...< createSequenceCore

  let startSequenceCore = 
createSequenceCore> ...> Async.StartImmediate

PS:参数 f 是否存在,以便类型推断推断泛型参数而不是 obj


I've got a function that creates an Async workflow, and the function that takes 10 arguments in curry style. e.g.

let createSequenceCore a b c d e f g h i j = 
  async { 
    ... 
  }

I want to create another function to start that workflow, so I've got

let startSequenceCore a b c d e f g h i j =
  Async.StartImmediate (createSequenceCore a b c d e f g h i j)

Is there any way I can get rid of those redundant parameters? I tried the << operator, but that only lets me remove one.

let startSequenceCore a b c d e f g h i =
  Async.StartImmediate << (createSequenceCore a b c d e f g h i)

(I added Haskell and Scala to this question even though the code itself is F#, as really what I want is just how to do this kind of currying, which would apply to any; I'd think a Haskell or Scala answer would be easily portable to F# and could well be marked as the correct answer).

NOTE Reasonably well showing that there is not an easy solution to this could also get the bounty.


UPDATE geesh I'm not going to give 100 points to an answer that argues with the question rather than answering it, even if it's the highest voted, so here:

I've got a function that creates an Async workflow, and the function that takes 4 arguments in curry style. e.g.

let createSequenceCore a b c d = 
  async { 
    ... 
  }

I want to create another function to start that workflow, so I've got

let startSequenceCore a b c d =
  Async.StartImmediate (createSequenceCore a b c d)

Is there any way I can get rid of those redundant parameters? I tried the << operator, but that only lets me remove one.

let startSequenceCore a b c =
  Async.StartImmediate << (createSequenceCore a b c)

解决方案

10 arguments sounds like too many... How about you'd create a record with 10 properties instead, or maybe a DU where you don't need all 10 in every case? Either way, you'd end up with a single argument that way and normal function composition works as expected again.

EDIT: When you actually need it, you can create a more powerful version of the << and >> operators thusly:

let (<.<) f = (<<) (<<) (<<) f
let (<..<) f = (<<) (<<) (<.<) f
let (<...<) f = (<<) (<<) (<..<) f

let flip f a b = f b a
let (>.>) f = flip (<.<) f
let (>..>) f = flip (<..<) f
let (>...>) f = flip (<...<) f

and then you can just write:

let startSequenceCore =
    Async.StartImmediate <...< createSequenceCore

or

let startSequenceCore =
    createSequenceCore >...> Async.StartImmediate

P.S.: The argument f is there, so that the type inference infers generic args as opposed to obj.

这篇关于如何用长参数列表实现无点风格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆