Skip to content

Instantly share code, notes, and snippets.

@rnewman
Created March 23, 2015 17:03
Show Gist options
  • Save rnewman/ecc7e96dabde0e38187f to your computer and use it in GitHub Desktop.
Save rnewman/ecc7e96dabde0e38187f to your computer and use it in GitHub Desktop.
/**
* 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