Given a list of TimeSpan
values, I would like to get the average TimeSpan
. It would be elegant if I could just write it like this:
let durations = [TimeSpan.FromSeconds 4.; TimeSpan.FromSeconds 2.]
let avg = durations |> List.average
However, the compiler complains about this:
The type 'TimeSpan' does not support the operator 'DivideByInt'
which is quite reasonable, since TimeSpan
indeed doesn't define DivideByInt.
To work around this issue, I attempted to extend TimeSpan
like this (before using List.average
):
type TimeSpan with
static member DivideByInt (ts : TimeSpan) (i : int) =
(float ts.Ticks) / (float i) |> int64 |> TimeSpan.FromTicks
While this implementation seems to work correctly when used directly, it unfortunately doesn't seem to make the compiler happy when using List.average
.
While I'm very much aware that I can use List.averageBy, or just write a custom function to calculate the average of a list of TimeSpan
values, I thought it'd be elegant if one could write durations |> List.average
.
Is there any way to enable that syntax?