Skip to content

Instantly share code, notes, and snippets.

@davbeck
Created September 30, 2019 14:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davbeck/2c5d608f984cf25bac2c4d56cfeada9c to your computer and use it in GitHub Desktop.
Save davbeck/2c5d608f984cf25bac2c4d56cfeada9c to your computer and use it in GitHub Desktop.
extension Collection where Element: FloatingPoint {
func average() -> Element {
return self.reduce(Element.zero, +) / Element(self.count)
}
}
extension Collection where Element: BinaryInteger {
func average() -> Double {
return Double(self.reduce(0, +)) / Double(self.count)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment