-
Notifications
You must be signed in to change notification settings - Fork 318
Dictionary
each (eachFunction each: (Key, Value) -> ())
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: Key) -> Bool
Returns
true
ifkey
is inself
,false
otherwise.
let dictionary = ["A": 1, "B": 2, "C": 3]
dictionary.has("A")
// → true
pick (keys: Key[]) -> Dictionary
pick (keys: Key...) -> Dictionary
at (keys: Key...) -> 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: (Key, Value) -> (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: (Key, Value) -> (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: [Key: V]...) -> [Key: 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: [Key: Value]...) -> [Key: Value]
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: [K: V]...) -> [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: (Key, Value) -> Bool) -> [Key: Value]
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> (map: (Key, Value) -> (K, V)) -> [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{ ($0 + "!", $1 + 1) }
println(mapped)
// → [A!: 2, B!: 3, C!: 4]
###mapValues
mapValues <V> (map: (Key, Value) -> (V)) -> [Key: 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{ $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([Int: String](), {
(var initial: [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: (Key, Value) -> T) -> [T: [Value]]
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: (Key, Value) -> (T)) -> [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]
###mapFilter
mapFilter <K, V> (mapFunction map: (Key, Value) -> (K, V)?) -> [K: V]
Creates a Dictionary with keys and values generated by running each [key: value] of self through the mapFunction discarding nil return values.
let dict = [
"A": 1,
"B": 2,
"C": 3
]
let mapped = dictionary.mapFilter(mapFunction: { key, value -> (String, String)? in
if key == "C" {
return ("D", key)
}
return nil
})
// → ["D": "C"]
###mapFilterValues
mapFilterValues <V> (mapFunction map: (Key, Value) -> V?) -> [Key: V]
Creates a Dictionary with the same keys as self and values generated by running each [key: value] of self through the mapFunction discarding nil return values.
let dict = [
"A": 1,
"B": 2,
"C": 3
]
let result = dictionary.mapFilterValues { (key, value) -> Int? in
if key == "B" {
return nil
}
return value + 1
}
// → ["A": 2, "C": 4]
##Mutating methods
###shift
shift () -> (Key, Value)
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
#Operators ##Minus
- <K, V: Equatable> (first: [K: V], second: [K: V]) -> [K: V]
Equivalent to
difference
.a - b
.
##And
& <K, V: Equatable> (first: [K: V], second: [K: V]) -> [K: V]
Equivalent to
intersection
.a & b
.
##Or
| <K, V: Equatable> (first: [K: V], second: [K: V]) -> [K: V]
Equivalent to
union
.a | b
.