Forked from JadenGeller/Swift Dictionary Map Filter Reduce.swift
Last active
August 29, 2015 14:25
-
-
Save kostiakoval/814730ad246bf95b08f6 to your computer and use it in GitHub Desktop.
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 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