I just wanted to share that now it is possible (since f#8) to avoid the fun
and lambdas altogether at least in a very specific situation: when your lambda is nothing more than an atomic expression (that means, when the expression in you lambda does not has any whitespace outside of a parentheses, like when you pass multiple curried arguments to a function.)
For example:
let exp1 = [{| X = 0; Y = 1; |}] |> List.distinctBy (fun x -> x.X)
let exp2 = ["A"; "B"; "C"] |> List.map (fun x -> x.ToLower())
let exp3 = List.map (fun x -> x.CompareTo(5)) [1..10]
Can now be written as:
let exp1 = [{| X = 0; Y = 1; |}] |> List.distinctBy _.X
let exp2 = ["A"; "B"; "C"] |> List.map _.ToLower()
let exp3 = [1..10] |> List.map _.CompareTo(5)
Notice that, in the case of exp3
, I had to pass the data argument to the front and pipe the function, so the type inference knows that _
is an int
in that context.
fun
in place oflet rec
. The equivalent to OCaml and F#'sfun
isfn
. – Schenk