Created
March 27, 2015 04:24
-
-
Save JadenGeller/f8579fa97246e7dd104a to your computer and use it in GitHub Desktop.
Swift Dictionary Map/Filter/Reduce
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 Dictionary { | |
init(_ elements: [Element]){ | |
self.init() | |
for (k, v) in elements { | |
self[k] = v | |
} | |
} | |
func map<U>(transform: Value -> U) -> [Key : U] { | |
return Dictionary<Key, U>(Swift.map(self, { (key, value) in (key, transform(value)) })) | |
} | |
func map<T : Hashable, U>(transform: (Key, Value) -> (T, U)) -> [T : U] { | |
return Dictionary<T, U>(Swift.map(self, transform)) | |
} | |
func filter(includeElement: Element -> Bool) -> [Key : Value] { | |
return Dictionary(Swift.filter(self, includeElement)) | |
} | |
func reduce<U>(initial: U, @noescape combine: (U, Element) -> U) -> U { | |
return Swift.reduce(self, initial, combine) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This doesn't compile (anymore). All the
Swift.*
calls seem to be unavailable.