-
Notifications
You must be signed in to change notification settings - Fork 317
Dictionary
-
Instance methods
-
Operators
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]