Skip to content

Instantly share code, notes, and snippets.

@leilee
Last active November 24, 2017 04:32
Show Gist options
  • Save leilee/61cda8c2bc737ed3b30dd3f2ca8a29b3 to your computer and use it in GitHub Desktop.
Save leilee/61cda8c2bc737ed3b30dd3f2ca8a29b3 to your computer and use it in GitHub Desktop.
#Foundation
extension Int {
func times(_ closure: (Int) throws -> Void) rethrows {
try (0...self).forEach {
try closure($0)
}
}
@discardableResult
func times<T>(_ closure: (Int) throws -> T) rethrows -> [T] {
var result: [T] = []
try (0..<self).forEach {
try result.append(closure($0))
}
return result
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment