Median

The global min and max functions have an overload with a variadic parameter, so it seemed natural to use a similar design in this proposal. Also, the median of two values (i.e. an empty variadic array) returns the arithmetic mean, which may be useful as a global function?

The most general requirements I tried were:

  • T: Strideable where T.Stride: FloatingPoint
    but that wouldn't include any additional types, except maybe Foundation.Date — if a conformance was added (FB9452917).

  • T: Strideable where T.Stride: ExpressibleByFloatLiteral
    but that would include Foundation.Decimal, which had a broken distance(to:) method for the last five years — only recently fixed by @xwu in SR-6785.

Also, the distance(to:) method would return infinity for some floating-point values.

I agree that median methods on sequences would be useful, but I didn't think they would be accepted for the standard library. There was a previous discussion on adding sum and product methods to Swift Numerics, so perhaps mean and median methods would also be considered for that package?

Sorting by key paths was recently discussed in the apple/swift-algorithms#89 issue.

The benchmark suite returns an Int median for an [Int] array, by rounding to the nearest index.