Created
February 19, 2017 16:02
-
-
Save patzearfoss/1f61ee672500519bed29f9031bd9f5c0 to your computer and use it in GitHub Desktop.
This file contains 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
//: Playground - noun: a place where people can play | |
import UIKit | |
extension Array where Element: FloatingPoint { | |
/// Returns the sum of all elements in the array | |
var total: Element { | |
return reduce(0, +) | |
} | |
/// Returns the average of all elements in the array | |
var average: Element { | |
return isEmpty ? 0 : total / Element(count) | |
} | |
var variance: Element { | |
let mean = average | |
let variance = map({ ($0 - mean) * ($0 - mean) }).average | |
return variance | |
} | |
var standardDeviation: Element { | |
let stdDev = sqrt(variance) | |
return stdDev | |
} | |
} | |
let values: [Double] = [1,2,3] | |
print(values.standardDeviation) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment