如何按名称寻址 f# 管道参数?
我正在尝试做类似的事情,
seq { 1..100 }
|> Seq.sum
|> pown 2
它甚至无法编译,因为 pown 期望 'T^' 参数作为第一个参数,而我将其作为第二个参数,因为这是管道的默认行为。通过谷歌搜索,我没有找到让“pown”使用管道携带的参数作为第一个参数的方法。也许它有一些默认名称?
I'm trying to do something like
seq { 1..100 }
|> Seq.sum
|> pown 2
It doesn't even compile cause pown expects 'T^' argument as first argument and i'm giving it as a second one, as this is default behavior of pipeline. By googling i didnt find the way to make "pown" use the param carried by pipeline as it's first arg. Maybe it has some default name?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你可以使用辅助函数:
在标准库中有 flip 会很整洁:)
you can use auxiliary function:
It will be neat to have flip in standard library :)
您可以使用 lambda 为传入值命名:
上面,我将其命名为
x
。You can use a lambda to give the incoming value a name:
Above, I named it
x
.如果参数不匹配,我通常不会使用管道,因为我觉得这会损害可读性。 Brian 的选项非常清楚,但它与将结果分配给一个值(使用
let
而不是使用 pipeline 和fun
)没有太大区别。所以,我会这样写(没有任何好的功能魔法):
这本质上与 Brian 的版本相同 - 您还将结果分配给一个值 - 但它更简单。我可能会使用更具描述性的名称,例如
sumResult
而不是x
。使用
flip
的版本在 Haskell 中是相当标准的事情 - 尽管我也认为它可能会变得难以阅读(不是在这个简单的情况下,但如果你组合多个函数,它很容易变得可怕)。我认为这也是 F# 库中缺少flip
(和其他)的原因。I don't usually use pipeline if the arguments do not match, because I feel that it can hurt the readability. The option by Brian is quite clear, but it is not much different from assigning the result to a value (using
let
instead of using pipeline andfun
).So, I would just write this (without any nice functional magic):
This is essentially the same as Brian's version - you're also assigning the result to a value - but it is more straightforward. I would probably use more descriptive name e.g.
sumResult
instead ofx
.The version using
flip
is pretty standard thing to do in Haskell - though I also thing that it may become difficult to read (not in this simple case, but if you combine multiple functions, it can easily become horrible). That's I think also a reason whyflip
(and others) are missing in the F# libraries.