Skip to content

Instantly share code, notes, and snippets.

@sindresorhus
Forked from moiseev/dividable.swift
Created October 17, 2017 11:07
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 sindresorhus/c437ccc59a651e5d59235ef49291faf5 to your computer and use it in GitHub Desktop.
Save sindresorhus/c437ccc59a651e5d59235ef49291faf5 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