-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathAboutLists.scala
executable file
·131 lines (99 loc) · 3.17 KB
/
AboutLists.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
126
127
128
129
130
131
package org.functionalkoans.forscala
import support.KoanSuite
class AboutLists extends KoanSuite {
koan("Eq tests identity (same object)") {
val a = List(1, 2, 3)
val b = List(1, 2, 3)
(a eq b) should be(false)
}
koan("== tests equality (same content)") {
val a = List(1, 2, 3)
val b = List(1, 2, 3)
(a == b) should be(true)
}
koan("Nil lists are identical, even of different types") {
val a: List[String] = Nil
val b: List[Int] = Nil
(a == Nil) should be(true)
(a eq Nil) should be(true)
(b == Nil) should be(true)
(b eq Nil) should be(true)
(a == b) should be(true)
(a eq b) should be(true)
}
koan("Lists are easily created") {
val a = List(1, 2, 3)
a should equal(List(1, 2, 3))
}
koan("Lists can be accessed via head and tail") {
val a = List(1, 2, 3)
a.head should equal(1)
a.tail should equal(List(2, 3))
}
koan("Lists can accessed by position") {
val a = List(1, 3, 5, 7, 9)
a(0) should equal(1)
a(2) should equal(5)
a(4) should equal(9)
intercept[IndexOutOfBoundsException] {
println(a(5))
}
}
koan("Lists are immutable") {
val a = List(1, 3, 5, 7, 9)
val b = a.filterNot(v => v == 5) // remove where value is 5
a should equal(List(1, 3, 5, 7, 9))
b should equal(List(1, 3, 7, 9))
}
koan("Lists have many useful methods") {
val a = List(1, 3, 5, 7, 9)
// get the length of the list
a.length should equal(5)
// reverse the list
a.reverse should equal(List(9, 7, 5, 3, 1))
// convert the list to a string representation
a.toString should equal("List(1, 3, 5, 7, 9)")
// map a function to double the numbers over the list
a.map {v => v * 2} should equal(List(2, 6, 10, 14, 18))
// filter any values divisible by 3 in the list
a.filter {v => v % 3 == 0} should equal(List(3, 9))
}
koan("Functions over lists can use _ as shorthand") {
val a = List(1, 2, 3)
a.map {_ * 2} should equal(List(2, 4, 6))
a.filter {_ % 2 == 0} should equal(List(2))
}
koan("Functions over lists can use () instead of {}") {
val a = List(1, 2, 3)
a.map(_ * 2) should equal(List(2, 4, 6))
a.filter(_ % 2 != 0) should equal(List(1, 3))
}
koan("Lists can be 'reduced' with a mathematical operation") {
val a = List(1, 3, 5, 7)
// note the two _s below indicate the first and second args respectively
a.reduceLeft(_ + _) should equal(16)
a.reduceLeft(_ * _) should equal(105)
}
koan("Foldleft is like reduce, but with an explicit starting value") {
val a = List(1, 3, 5, 7)
// NOTE: foldLeft uses a form called currying that we will explore later
a.foldLeft(0)(_ + _) should equal(16)
a.foldLeft(10)(_ + _) should equal(26)
a.foldLeft(1)(_ * _) should equal(105)
a.foldLeft(0)(_ * _) should equal(0)
}
koan("You can create a list from a range") {
val a = (1 to 5).toList
a should be(List(1, 2, 3, 4, 5))
}
koan("Lists reuse their tails") {
val d = Nil
val c = 3 :: d
val b = 2 :: c
val a = 1 :: b
a should be(List(1, 2, 3))
a.tail should be(List(2, 3))
b.tail should be(List(3))
c.tail should be(List())
}
}