Skip to content

Instantly share code, notes, and snippets.

@cakper
Created March 3, 2015 12:58
Show Gist options
  • Save cakper/ef0c03bca0a2fb603e8e to your computer and use it in GitHub Desktop.
Save cakper/ef0c03bca0a2fb603e8e to your computer and use it in GitHub Desktop.
Statistics
class Statistics {
let minimum: Int
let maximum: Int
let average: Int
init(minimum: Int, maximum: Int, average: Int) {
self.minimum = minimum
self.maximum = maximum
self.average = average
}
}
protocol MinimumCalculator {
func calculate(input: [Int]) -> Int
}
protocol MaximumCalculator {
func calculate(input: [Int]) -> Int
}
protocol AverageCalculator {
func calculate(input: [Int]) -> Int
}
class Stats {
let minimumCalculator: MinimumCalculator
let maximumCalculator: MaximumCalculator
let averageCalculator: AverageCalculator
init(minimumCalculator: MinimumCalculator, maximumCalculator: MaximumCalculator, averageCalculator: AverageCalculator) {
self.minimumCalculator = minimumCalculator
self.maximumCalculator = maximumCalculator
self.averageCalculator = averageCalculator
}
func calculate(input: [Int]) -> Statistics {
return Statistics(
minimum: self.minimumCalculator.calculate(input),
maximum: self.maximumCalculator.calculate(input),
average: self.averageCalculator.calculate(input)
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment