Skip to content

Instantly share code, notes, and snippets.

@patzearfoss
Created February 19, 2017 16:02
Show Gist options
  • Save patzearfoss/1f61ee672500519bed29f9031bd9f5c0 to your computer and use it in GitHub Desktop.
Save patzearfoss/1f61ee672500519bed29f9031bd9f5c0 to your computer and use it in GitHub Desktop.
//: 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