I'm trying to extend a Collection of FloatingPoint-conforming elements to work out an average.
extension Collection where Element: FloatingPoint {
func sum() -> Element {
return reduce(0, +)
}
func average() -> Element {
return sum() / Int(count)
}
}
sum()
works fine but average()
has an error.
Binary operator '/' cannot be applied to operands of type 'Self.Element' and 'Int'
I'm not sure why this is. Self.Element
is a FloatingPoint
. I would expect to be able to divide this.
(I'm also aware that there's a divide-by-zero issue, but I'll fix that later.)