Join GitHub today
GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.Sign up
Average of collection functions should support more numeric types #812
Average of collection functions should support more numeric types
I propose we allow Seq.average, Array.average and friends to work on a wider variety of numeric data types (e.g int, int64, etc.). Currently only data types with a static member of "DivideByInt" are supported.
Pros and Cons
The advantages of making this adjustment to F# are:
The disadvantages of making this adjustment to F# are:
Estimated cost (XS, S, M, L, XL, XXL): S
The existing way of approaching this problem in F# would be to hand roll your own function to do this. e.g.
Affidavit (please submit!)
Please tick this by placing a cross in the box:
Please tick all that apply:
It's not clear to me what is the signature of
Your sample function gives you a signature of
I can think of 2 possibilities:
@gusty Its a good question. My makeshift code example was more that I saw a pretty typical use case that to a newcomer to the language should "just work" without having to understand SRTP errors. In my case I had a list of timings (in milliseconds of type int64) in a collection that I wanted to get the average of. LINQ in C# has overloads for a variety of different primitive number types. This means if I do a .Average() on a sequence of numbers usually "it just works" in that language. For newcomers to the language it isn't intuitive why it doesn't just work; or what the error message "int does not support the operator DivideByInt" means.
The current behavior (tested in FSI) that assumes the input type and return type are the same unless specified seems like a good default to me but I could be naive about some detail. In my case to be honest I wouldn't of minded if it returned a float or a decimal either.
Perhaps just have an
There was a reason that the function requires that member (same as with several other core functions), since it's noon trivial how to get the average of, say, a sequence of bytes, or ints: what do you do with the remainder (they aren't floats, would you accept a potential huge rounding error? That's why they leave it up to the user), and how to deal with overflow?
For F# 5.0 I believe it is planned that extension methods can be used with SRTP, which would make this much simpler to solve.