-
Notifications
You must be signed in to change notification settings - Fork 317
Dictionary
each (eachFunction each: (KeyType, ValueType) -> ())
Calls
eachFunction
for each(key, value)
couple inself
.
let dictionary = ["A": 1, "B": 2, "C": 3]
dictionary.each { key, value in println(key, value) }
/* Prints → */
// (A, 1)
// (B, 2)
// (C, 3)
has (key: KeyType) -> Bool
Returns
true
ifkey
is inself
,false
otherwise.
let dictionary = ["A": 1, "B": 2, "C": 3]
dictionary.has("A")
// → true
pick (keys: KeyType[]) -> Dictionary
pick (keys: KeyType...) -> Dictionary
at (keys: KeyType...) -> Dictionary
Return a copy of
self
, containing the couples(key, value)
for the whitelistedkeys
.
let dictionary = ["A": 1, "B": 2, "C": 3, "D": 4]
dictionary.pick("A", "C")
// → ["A": 1, "C": 3]
any (test: (KeyType, ValueType) -> (Bool)) -> Bool
Executes
test
on each(key, value)
inself
and returnstrue
iftest
returnstrue
for any couple,false
otherwise.
let dictionary = ["A": 1, "B": 2, "C": 3, "D": 4]
dictionary.any { key, value -> Bool in value < 0 }
// → false
dictionary.any { key, value -> Bool in key == "A" }
// → true
all (test: (KeyType, ValueType) -> (Bool)) -> Bool
Executes
test
on each(key, value)
inself
and returnstrue
iftest
returnstrue
for each couple,false
otherwise.
let dictionary = ["A": 1, "B": 2, "C": 3, "D": 4]
dictionary.all { key, value -> Bool in value > 0 }
// → true
dictionary.all { key, value -> Bool in key == "A" }
// → false
##Sets
###difference
difference <V: Equatable> (dictionaries: Dictionary<KeyType, V>...) -> Dictionary<KeyType, V>
Returns the couples
(key, value)
inself
for everykey
that is not in any of thedictionaries
.
let dictionary1 = [ "A": 1, "B": 2, "C": 3 ]
let dictionary2 = [ "A": 1 ]
let diff1 = dictionary1.difference(dictionary2)
// → [C: 3, B: 2]
###union
union (dictionaries: Dictionary<KeyType, ValueType>...) -> Dictionary<KeyType, ValueType>
Computes the union of
self
and the inputdictionaries
, by keys.
let dictionary1 = [ "A": 1, "B": 2, "C": 3 ]
let dictionary2 = [ "A": 1 ]
let dictionary3 = [ "D": 4 ]
dictionary1.union(dictionary2, dictionary3)
// → [A: 1, B:2, C: 3, D: 4]
###intersection
intersection <K, V where K: Equatable, V: Equatable> (dictionaries: Dictionary<K, V>...) -> Dictionary<K, V>
Computes the intersection of
self
and the inputdictionaries
, by keys.
let dictionary1 = [ "A": 1, "B": 2, "C": 3 ]
let dictionary2 = [ "A": 1 ]
let dictionary3 = [ "D": 4 ]
dictionary1.intersection(dictionary2)
// → [ A: 1 ]
##Transformation
###filter
filter (testFunction test: (KeyType, ValueType) -> Bool) -> Dictionary<KeyType, ValueType>
Constructs a dictionary containing each
key, value
pair fromself
such thattestFunction(key, value)
evaluates totrue
.
let dictionary = [ "A": 1, "B": 2, "C": 3 ]
let filtered = dictionary.filter {
key, value in return key != "A"
}
println(filtered)
// → [B: 2, C: 3]
###map
map <K, V> (mapFunction map: (KeyType, ValueType) -> (K, V)) -> Dictionary<K, V>
Creates a Dictionary with keys and values generated by running each
key, value
ofself
through themapFunction
.
let dictionary = [ "A": 1, "B": 2, "C": 3 ]
let mapped = dictionary.map(mapFunction: { return ($0 + "!", $1 + 1) })
println(mapped)
// → [A!: 2, B!: 3, C!: 4]
###mapValues
mapValues <V> (mapFunction map: (KeyType, ValueType) -> (V)) -> Dictionary<KeyType, V>
Creates a Dictionary with the same keys as self and values generated by running each
key, value
of self through themapFunction
.
let dictionary = [ "A": 1, "B": 2, "C": 3 ]
let mapped = dictionary.mapValues(mapFunction: { return $1 + 1 })
println(mapped)
// → [A: 2, B: 3, C: 4]
###reduce
reduce <U> (initial: U, combine: (U, Element) -> U) -> U
Equivalent to
Swift.reduce(self, initial, combine)
.
let dictionary = [ "A": 1, "B": 2, "C": 3 ]
let reduced = dictionary.reduce(Dictionary<Int, String>(), {
(var initial: Dictionary<Int, String>, couple: (String, Int)) in
initial.updateValue(couple.0, forKey: couple.1)
return initial
})
// → [2: B, 3: C, 1: A]
###groupBy
groupBy <T> (groupingFunction group: (KeyType, ValueType) -> T) -> Dictionary<T, Array<ValueType>>
Creates a dictionary composed of keys generated from the results of running each element of
self
throughgroupingFunction
. The corresponding value of each key is an array of the elements responsible for generating the key.
let group = [
"A": 2,
"B": 4,
"C": 5
]
let g = group.groupBy(groupingFunction: {
(key: String, value: Int) -> Bool in
return (value % 2 == 0)
})
// → [false: [5], true: [2, 4]]
###countBy
countBy <T> (groupingFunction group: (KeyType, ValueType) -> (T)) -> Dictionary<T, Int>
Similar to
groupBy
. Instead of returning a list of values, returns the number of values for each group.
let group = [
"A": 2,
"B": 4,
"C": 5
]
let g = group.countBy(groupingFunction: {
(key: String, value: Int) -> Bool in
return (value % 2 == 0)
})
// → [false: 1, true: 2]
##Mutating methods
###shift
shift () -> (KeyType, ValueType)
Removes a
key, value
pair from self and returns it as tuple.
let dictionary = [ "A": 1, "B": 2, "C": 3 ]
dictionary.shift()
// → (B, 2)
##More
###isEmpty
isEmpty () -> Bool
Returns
true
if self doesn't contain any key,false
otherwise.
[ "A": 1, "B": 2, "C": 3 ].isEmpty()
// → false
[:].isEmpty()
// → true