Created
August 29, 2014 20:38
-
-
Save cruffenach/3ed23d6d80ed27fd6928 to your computer and use it in GitHub Desktop.
Index in Array's map and reduce
This file contains hidden or 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 map<U>(function : (index : Int, object : T) -> U) -> [U] { | |
var result = [U]() | |
for i in 0..<self.count { | |
result.append(function(index: i, object: self[i])) | |
} | |
return result | |
} | |
func reduce<U>(initial : U, combine : (product : U, index : Int, object : T) -> U) -> U { | |
var result = initial; | |
for i in 0..<self.count { | |
result = combine(product: result, index: i, object: self[i]) | |
} | |
return result | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I think you're looking for
enumerate()