Skip to content

Instantly share code, notes, and snippets.

@leodabus
Last active January 30, 2017 17:29
Show Gist options
  • Save leodabus/f0e9bc9886fca6d7f3e28ff9cc1abe96 to your computer and use it in GitHub Desktop.
Save leodabus/f0e9bc9886fca6d7f3e28ff9cc1abe96 to your computer and use it in GitHub Desktop.
extension BidirectionalCollection where Iterator.Element: FloatingPoint {
func multiplied(by multiplier: Iterator.Element) -> [Iterator.Element] {
return map{$0 * multiplier}
}
var squared: [Iterator.Element] {
return map{$0 * $0}
}
var cubed: [Iterator.Element] {
return map{ $0 * $0 * $0 }
}
var progressivePow: [Iterator.Element] {
return enumerated().map{ $0.element * Iterator.Element($0.offset) }
}
var acumulate: [Iterator.Element] {
var sum: Iterator.Element = 0
return map {
sum += $0
return sum
}
}
}
let array = (1...10).map(Double.init) // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
array.multiplied(by: 2.5) // [2.5, 5, 7.5, 10, 12.5, 15, 17.5, 20, 22.5, 25]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment