-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathAboutSets.scala
125 lines (89 loc) · 3.71 KB
/
AboutSets.scala
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
113
114
115
116
117
118
119
120
121
122
123
124
125
package org.functionalkoans.forscala
import support.KoanSuite
class AboutSets extends KoanSuite {
koan("Sets can be created easily") {
val mySet = Set("Michigan", "Ohio", "Wisconsin", "Iowa")
mySet.size should be(4)
}
koan("Sets contain distinct values") {
val mySet = Set("Michigan", "Ohio", "Wisconsin", "Michigan")
mySet.size should be(3)
}
koan("Sets can be added to easily") {
val mySet = Set("Michigan", "Ohio", "Wisconsin", "Iowa")
val aNewSet = mySet + "Illinois"
aNewSet.contains("Illinois") should be(true)
}
koan("Sets may be of mixed type") {
val mySet = Set("Michigan", "Ohio", 12)
mySet.contains(12) should be(true)
mySet.contains("MI") should be(false)
}
koan("Sets can be checked for member existence") {
val mySet = Set("Michigan", "Ohio", 12)
mySet(12) should be(true)
mySet("MI") should be(false)
}
koan("Set elements can be removed easily") {
val mySet = Set("Michigan", "Ohio", "Wisconsin", "Iowa")
val aNewSet = mySet - "Michigan"
aNewSet.contains("Michigan") should be(false)
}
koan("Set elements can be removed in multiple") {
val mySet = Set("Michigan", "Ohio", "Wisconsin", "Iowa")
val aNewSet = mySet -- List("Michigan", "Ohio")
aNewSet.contains("Michigan") should be(false)
aNewSet.contains("Wisconsin") should be(true)
aNewSet.size should be(2)
}
koan("Set elements can be removed with a tuple") {
val mySet = Set("Michigan", "Ohio", "Wisconsin", "Iowa")
val aNewSet = mySet - ("Michigan", "Ohio") // Notice: single '-' operator for tuples
aNewSet.contains("Michigan") should be(false)
aNewSet.contains("Wisconsin") should be(true)
aNewSet.size should be(2)
}
koan("Attempted removal of nonexistent elements from a set is handled gracefully") {
val mySet = Set("Michigan", "Ohio", "Wisconsin", "Iowa")
val aNewSet = mySet - "Minnesota"
aNewSet.equals(mySet) should be(true)
}
koan("Sets can be iterated easily") {
val mySet = Set(1, 3, 4, 9)
var sum = 0
for (i <- mySet)
sum = sum + i //Of course this is the same thing as mySet.reduce(_ + _)
sum should be(17)
}
koan("Two sets can be intersected easily") {
val mySet1 = Set("Michigan", "Ohio", "Wisconsin", "Iowa")
val mySet2 = Set("Wisconsin", "Michigan", "Minnesota")
val aNewSet = mySet1 intersect mySet2
// NOTE: Scala 2.7 used **, deprecated for & or intersect in Scala 2.8
aNewSet.equals(Set("Michigan", "Wisconsin")) should be(true)
}
koan("Two sets can be joined as their union easily") {
val mySet1 = Set("Michigan", "Ohio", "Wisconsin", "Iowa")
val mySet2 = Set("Wisconsin", "Michigan", "Minnesota")
val aNewSet = mySet1 union mySet2 // NOTE: You can also use the "|" operator
aNewSet.equals(Set("Michigan", "Wisconsin", "Ohio", "Iowa", "Minnesota")) should be(true)
}
koan("A set is either a subset of another set or it isn't") {
val mySet1 = Set("Michigan", "Ohio", "Wisconsin", "Iowa")
val mySet2 = Set("Wisconsin", "Michigan", "Minnesota")
val mySet3 = Set("Wisconsin", "Michigan")
mySet2 subsetOf mySet1 should be(false)
mySet3 subsetOf mySet1 should be(true)
}
koan("The difference between two sets can be obtained easily") {
val mySet1 = Set("Michigan", "Ohio", "Wisconsin", "Iowa")
val mySet2 = Set("Wisconsin", "Michigan")
val aNewSet = mySet1 diff mySet2 // Note: you can use the "&~" operator if you *really* want to.
aNewSet.equals(Set("Iowa", "Ohio")) should be(true)
}
koan("Set equivalency is independent of order") {
val mySet1 = Set("Michigan", "Ohio", "Wisconsin", "Iowa")
val mySet2 = Set("Wisconsin", "Michigan", "Ohio", "Iowa")
mySet1.equals(mySet2) should be(true)
}
}