Skip to content
This repository has been archived by the owner on Mar 29, 2018. It is now read-only.

Dictionary

hyeonjae edited this page May 31, 2015 · 18 revisions

Contents

Instance Methods

Iteration

each

  • each (eachFunction each: (Key, Value) -> ())

Calls eachFunction for each (key, value) couple in self.

Examples

Without indexes
let dictionary = ["A": 1, "B": 2, "C": 3]
dictionary.each { key, value in println(key, value) }
/* Prints → */
// (A, 1)
// (B, 2)
// (C, 3)

Items accessing

has

  • has (key: Key) -> Bool

Returns true if key is in self, false otherwise.

Examples

Without indexes
let dictionary = ["A": 1, "B": 2, "C": 3]
dictionary.has("A")
// → true

pick, at

  • pick (keys: Key[]) -> Dictionary
  • pick (keys: Key...) -> Dictionary
  • at (keys: Key...) -> Dictionary

Return a copy of self, containing the couples (key, value) for the whitelisted keys.

Examples

Without indexes
let dictionary = ["A": 1, "B": 2, "C": 3, "D": 4]
dictionary.pick("A", "C")
// → ["A": 1, "C": 3]

Items testing

any

  • any (test: (Key, Value) -> (Bool)) -> Bool

Executes test on each (key, value) in self and returns true if test returns true for any couple, false otherwise.

Example

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

  • all (test: (Key, Value) -> (Bool)) -> Bool

Executes test on each (key, value) in self and returns true if test returns true for each couple, false otherwise.

Example

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) in self for every key that is not in any of the dictionaries.

Example

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 input dictionaries, by keys.

Example

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 input dictionaries, by keys.

Example

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 from self such that testFunction(key, value) evaluates to true.

Example

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 of self through the mapFunction.

Example

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 the mapFunction.

Example

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).

Example

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 through groupingFunction. The corresponding value of each key is an array of the elements responsible for generating the key.

Example

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.

Example

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.

Example

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.

Example

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.

Example

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.

Example

[ "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.