Skip to content

Instantly share code, notes, and snippets.

@khanlou
Last active March 20, 2019 06:49
Show Gist options
  • Select an option

  • Save khanlou/319fda4ca0f50c7400398c3280538346 to your computer and use it in GitHub Desktop.

Select an option

Save khanlou/319fda4ca0f50c7400398c3280538346 to your computer and use it in GitHub Desktop.
Sum on Sequence and Average on Array in Swift 4
extension Sequence where Self.Iterator.Element: Numeric {
var sum: Self.Iterator.Element {
return self.reduce(0, +)
}
}
extension Collection where Element: BinaryFloatingPoint {
var average: Element {
return self.reduce(0, +) / Element((0 as IndexDistance).distance(to: self.count))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment