Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save kostiakoval/814730ad246bf95b08f6 to your computer and use it in GitHub Desktop.
Save kostiakoval/814730ad246bf95b08f6 to your computer and use it in GitHub Desktop.
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<U>(transform: Key -> U) -> [U : Value] {
return Dictionary<U, Value>(Swift.map(self, { (key, value) in (transform(key), 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