-
Notifications
You must be signed in to change notification settings - Fork 6
/
Contents.swift
112 lines (93 loc) · 4.03 KB
/
Contents.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
* Copyright (C) 2015 47 Degrees, LLC http://47deg.com [email protected]
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License. You may obtain
* a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import UIKit
import SecondBridge
/*:
# Maps
SecondBridge's **Maps** are essentially dictionaries, but we provide support for a syntax closer to those in Scala.
Maps (as the rest of our implemented data types) are completely **functional**, so any operation will always
return a new Map with the resulting changes.
Maps implement both *Traversable* and *Iterable* protocols, which means that they can be used a lot of really
useful functions. In fact, most of them are implemented as accesors in the Map struct.
In the next lines of code, we explore some basics of the use of SecondBridge's Maps. Remember that you can take a look
at our test classes to learn more about all their available functions.
*/
//: Maps can be created easily:
let myMap1 : Map<String> = ["MI" : "Michigan", "OH" : "Ohio", "WI" : "Wisconsin", "IA" : "Iowa"]
println(myMap1)
myMap1.size == 4
//: Maps contain distinct pairings:
let myMap2 : Map<String> = ["MI" : "Michigan", "OH" : "Ohio", "WI" : "Wisconsin", "WI" : "Wisconsin"]
println(myMap2)
myMap2.size == 3
//: Maps can be added to easily:
let myMap3 : Map<String> = ["MI" : "Michigan", "OH" : "Ohio", "WI" : "Wisconsin", "WI" : "Wisconsin"]
println(myMap3)
let aNewMap = myMap3 + ["IL":"Illinois"]
println(aNewMap)
aNewMap.contains("IL")
//: Map values can be iterated:
let myMap4 : Map<String> = ["MI" : "Michigan", "OH" : "Ohio", "WI" : "Wisconsin", "MI" : "Michigan"]
println(myMap4)
let map4Values = myMap4.values()
println(map4Values)
map4Values.count == 3
map4Values.first
let lastElement = map4Values.last
//: Maps insertion with duplicate key updates previous entry with subsequent value:
let myMap5 : Map<String> = ["MI" : "Michigan", "OH" : "Ohio", "WI" : "Wisconsin", "MI" : "Meechigan"]
let map5Values = myMap5.values()
map5Values.count == 3
myMap5["MI"] == "Meechigan"
//: Map keys may be of mixed type:
let myMap6 : Map = ["Ann Arbor" : "MI", 49931 : "MI"]
myMap6["Ann Arbor"] == "MI"
myMap6[49931] == "MI"
//: Mixed type values can be added to a map:
let myMap7 : Map = ["Ann Arbor" : [48103, 48104, 48108], 49931 : "MI"]
//: Maps may be accessed:
let myMap8 : Map = ["MI" : "Michigan", "OH" : "Ohio", "WI" : "Wisconsin", "IA" : "Iowa"]
myMap8["MI"] == "Michigan"
myMap8["IA"] == "Iowa"
//: Map elements can be removed easily:
let myMap9 : Map = ["MI" : "Michigan", "OH" : "Ohio", "WI" : "Wisconsin", "IA" : "Iowa"]
let anewMap9 = myMap9 - "MI"
anewMap9.contains("MI")
myMap9.contains("MI")
//: Map elements can be removed in multiple:
let myMap10 : Map = ["MI" : "Michigan", "OH" : "Ohio", "WI" : "Wisconsin", "IA" : "Iowa"]
let aNewMap10 = myMap10 -- ["MI", "OH"]
aNewMap10.contains("MI")
myMap10.contains("MI")
aNewMap10.contains("WI")
aNewMap10.size == 2
myMap10.size == 4
//: Map elements can be removed with a tuple:
let myMap11 : Map = ["MI" : "Michigan", "OH" : "Ohio", "WI" : "Wisconsin", "IA" : "Iowa"]
let aNewMap11 = myMap11 -- ["MI", "WI"]
aNewMap11.contains("MI")
myMap11.contains("MI")
aNewMap11.contains("OH")
aNewMap11.size == 2
myMap11.size == 4
//: Attempted removal of nonexistent elements from a map is handled gracefully:
let myMap12 : Map = ["MI" : "Michigan", "OH" : "Ohio", "WI" : "Wisconsin", "IA" : "Iowa"]
let aNewMap12 = myMap12 - "MN"
aNewMap12 == myMap12
//: Map equivalency is independent of order:
let myMap13 : Map = ["MI" : "Michigan", "OH" : "Ohio", "WI" : "Wisconsin", "IA" : "Iowa"]
let myMap14 : Map = ["WI" : "Wisconsin", "MI" : "Michigan", "IA" : "Iowa", "OH" : "Ohio"]
myMap13 == (myMap14)