开发者

How to address f# pipeline parameter by name?

I'm trying to do something like

开发者_如何学JAVA
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?


you can use auxiliary function:

let flip f x y = f y x

seq { 1L..100L }
    |> Seq.sum
    |> flip pown 2

It will be neat to have flip in standard library :)


You can use a lambda to give the incoming value a name:

seq { 1..100 } 
|> Seq.sum 
|> (fun x -> pown x 2)

Above, I named it x.


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 and fun).

So, I would just write this (without any nice functional magic):

let x = seq { 1 .. 100 } |> Seq.sum
pown x 2

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 of x.

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 why flip (and others) are missing in the F# libraries.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜