tags:

views:

103

answers:

3

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?

+3  A: 

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 :)

desco
+3  A: 

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.

Brian
So i can't address that variable by some name like _$, _@, whatever? That's strange.
fspirit
If by "strange" you mean "unlike insane perl", then yes, it is strange. :)
Brian
Yes, perl was on my mind, and, yes, it's kinda insane :)
fspirit
+1  A: 

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.

Tomas Petricek