Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save JadenGeller/f8579fa97246e7dd104a to your computer and use it in GitHub Desktop.
Save JadenGeller/f8579fa97246e7dd104a to your computer and use it in GitHub Desktop.
Swift Dictionary Map/Filter/Reduce
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)
}
}
@tcurdt
Copy link

tcurdt commented Aug 25, 2016

This doesn't compile (anymore). All the Swift.* calls seem to be unavailable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment