Given the following expression to sum an IEnumerable of numbers:
let sum l = l |> Seq.reduce(+) //version a
is it possible to eliminate
A point-free function is a value.
As other answers say, F# does not allow generic values. However, it perfectly allows generic functions. Let's convert sum into a function by adding a fake unit parameter:
let sum_attempt1() = Seq.reduce (+)
let v1 = [1.0; 2.0] |> sum() // float
// inferred by first usage:
// val sum_attempt1: unit -> (seq -> float)
This works, although it is not yet generic. Marking the function inline does the trick:
let inline sum() = Seq.reduce (+)
// val sum: unit -> (seq<'a> -> 'a)
// Use
let v1 = [1; 2] |> sum() // int
let v2 = [1.0; 2.0] |> sum() // float
let v3 = ["foo"; "bar"] |> sum() // string