Skip to content

Instantly share code, notes, and snippets.

@sooop
Created June 15, 2015 09:44
Show Gist options
  • Save sooop/839bc282821784e2a116 to your computer and use it in GitHub Desktop.
Save sooop/839bc282821784e2a116 to your computer and use it in GitHub Desktop.
Convenience Function for build Dictionary
let k = ["a", "b", "c", "d"]
let v = [1, 2, 3, 4]
let p = zip(k, v)
//: build fast dictionary from zipped object
func fastDictionary<K:Hashable, V>(data:Zip2<[K], [V]>) -> Dictionary<K, V> {
var result:[K:V] = [:]
for i in data {
result[i.0] = i.1
}
return result
}
//: build fast dictionary from two sequence types.
func fastDictionary<K:Hashable, V>(s1:[K], s2:[V]) -> Dictionary<K, V> {
return fastDictionary(zip(s1, s2))
}
let d = fastDictionary(k, v)
println(d)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment