Skip to content

Instantly share code, notes, and snippets.

@moiseev
Created October 6, 2017 18:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save moiseev/afd255ebd03938db848cd02021bc4a0b to your computer and use it in GitHub Desktop.
Save moiseev/afd255ebd03938db848cd02021bc4a0b to your computer and use it in GitHub Desktop.
If you really really miss the `/` from `Numeric`...
public protocol Dividable {
static func / (lhs: Self, rhs: Self) -> Self
}
extension Int : Dividable {}
extension Double : Dividable {}
extension Sequence where Element : Numeric & Dividable {
func average() -> Element {
var i: Element = 0
var total: Element = 0
for value in self {
total = total + value
i += 1
}
return total / i
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment