Skip to content

Instantly share code, notes, and snippets.

@SiarheiFedartsou
Last active August 29, 2015 14:02
Show Gist options
  • Save SiarheiFedartsou/a31baa46f630a5c6e8ed to your computer and use it in GitHub Desktop.
Save SiarheiFedartsou/a31baa46f630a5c6e8ed to your computer and use it in GitHub Desktop.
extension Array {
func each(closure: ((item: T, idx: Int) -> Void)) {
var idx : Int = 0
for item in self {
closure(item: item, idx: idx)
idx += 1
}
}
}
....
Variant 1:
var arr = [1, 2, 3]
arr.each {
println("Value: \($0)")
}
Output:
Value: (1, 0)
Value: (2, 1)
Value: (3, 2)
Variant 2:
var arr = [1, 2, 3]
arr.each {
println("Value: \($0) Index: \($1)")
}
Value: 1 Index: 0
Value: 2 Index: 1
Value: 3 Index: 2
Variant 3:
var arr = [1, 2, 3]
arr.each { (item, idx) in
println("Value: \(item)")
}
Output:
Value: 1
Value: 2
Value: 3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment