Skip to content

Instantly share code, notes, and snippets.

@boraseoksoon
Last active January 6, 2023 22:04
Show Gist options
  • Save boraseoksoon/80317ec249ba77d05c40919ac6923e0d to your computer and use it in GitHub Desktop.
Save boraseoksoon/80317ec249ba77d05c40919ac6923e0d to your computer and use it in GitHub Desktop.
loop (forEach wrapper)
import Foundation
func loop(_ count: Int, body: @escaping ((Range<Int>.Element)) throws -> Void) rethrows {
try (0..<count).forEach(body)
}
func loop(_ count: Int, body: @escaping (() throws -> Void)) rethrows {
(0..<count).forEach { _ in
try? body()
}
}
loop(3) {
print("loop!")
}
// loop!
// loop!
// loop!
loop(3) {
print("loop : \($0)")
}
// loop : 0
// loop : 1
// loop : 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment