Last active
August 29, 2015 14:02
-
-
Save SiarheiFedartsou/a31baa46f630a5c6e8ed to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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