Seq.average - F#
Lets examine Seq.average.
Seq.average: returns the average of the elements in the sequence. ~ FSharp.Core
[<EntryPoint>]
let main argv =
let seq = seq { 1.0 .. 4.0 }
let average = seq |> Seq.average
printfn "%f" average
0
We have a sequence consisting of [1.0; 2.0; 3.0. 4.0], running this prints out 2.5000, which is the correct average.
Pretty self explanatory, the only gotcha is it doesn’t support int as a data type.
Seq.average supports any type that:
- has
(+)operator - has static property
Zero - has static member DivideByInt.
int fulfills everything but the last requirement.
int is not supported because some numbers divided by an int come out as a float instead of an int. ie: 5 / 2 = 2.5.
Since the input type int is different than the output type float, it doesn’t pass the third case, therefore, can’t be used in Seq.average.