I assume that you have at least two pipes. Otherwise, fn 1 2
does the job; why should we make it more complicated?
For commutative functions (which satisfy f x y = f y x
) such as (+)
, (*)
, etc. you can just use 1 |> fn 2
.
For other functions, I can see a few alternatives:
Use backward pipes
arg1 |> fn1 |> fn2 <| arg2
I tend to use this with care since operators' precedence might cause some subtle bugs.
Use
flip
functionlet inline flip f x y = f y x Array.map2 (fun y d -> (y - d) ** 2.0) y d |> Array.sum |> flip (/) 2.0
It's not pretty but it's clear that order of arguments in
(/)
is treated differently.Use anonymous functions with
fun
andfunction
keywords
This is handy if you need pattern matching in place.input |> createOption |> function None -> 0 | Some _ -> 1
IMO, you don't have to pipe all the way. Create one more let
binding, then your intention is clear and you can avoid bugs due to unusual styles.
let sum =
Array.map2 (fun y d -> (y - d) ** 2.0) y d
|> Array.sum
sum / 2.0