Created
March 23, 2015 17:03
-
-
Save rnewman/ecc7e96dabde0e38187f 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
/** | |
* Map a function over the values of a map, returning only entries | |
* for which the function returns non-nil. Works seamlessly with a | |
* function that has a non-optional type signature, too. | |
*/ | |
public func mapValues<K, T, U>(source: [K: T], f: (T -> U?)) -> [K: U] { | |
var m = [K: U]() | |
for (k, v) in source { | |
if let u = f(v) { | |
m[k] = u | |
} | |
} | |
return m | |
} | |
… | |
public static func mapFromJSON(map: [String: JSON]?) -> [String: EngineMeta]? { | |
if map == nil { | |
return nil | |
} | |
return mapValues(map!) { json in | |
return EngineMeta.fromJSON(json) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment