Last active
March 20, 2019 06:49
-
-
Save khanlou/319fda4ca0f50c7400398c3280538346 to your computer and use it in GitHub Desktop.
Sum on Sequence and Average on Array in Swift 4
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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