extension Array where Element: BinaryInteger {
/// The average value of all the items in the array
var average: Double {
if self.isEmpty {
return 0.0
} else {
let sum = self.reduce(0, +)
return Double(sum) / Double(self.count)
}
}
}
extension Array where Element: BinaryFloatingPoint {
/// The average value of all the items in the array
var average: Double {
if self.isEmpty {
return 0.0
} else {
let sum = self.reduce(0, +)
return Double(sum) / Double(self.count)
}
}
}
let sumArray = intArray.reduce(0, +)
let avgArrayValue = sumArray / intArray.count