-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathChapter14.scala
217 lines (190 loc) · 6.39 KB
/
Chapter14.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
object Chapter14 {
/**
* Task 1:
*
* Your Java Development Kit distribution has the source code for much of the JDK in the
* `src.zip` file. Unzip and search for case labels (regular expression `case [^:]+:`).
* Then look for comments starting with `//` and containing `[Ff]alls? thr` to catch comments
* such as `// Falls through` or `// just fall thru`.
* Assuming the JDK programmers follow the Java code convention, which requires such a comment,
* what percentage of cases falls through?
*
* Solution:
* {{{
* mkdir /tmp/java-test/src
* unzip /usr/lib/jvm/java-7-oracle/src.zip -d /tmp/java-test/src
* cd /tmp/java-test
* grep --include=*.java -r -E 'case [^:]+:' src > 1.txt
* grep --include=*.java -r -E '//.*[Ff]alls? thr' src > 2.txt
* }}}
* As a result on my machine there is around 9500 lines in the first file and
* 100 lines in the second file. Then:
* {{{
* (100 * 100%) / 9500 = 1%
* }}}
*/
/**
* Task 2:
*
* Using pattern matching, write a function `swap` that receives a pair of integers and
* returns the pair with the components swapped.
*/
def swap(pair: (Int, Int)): (Int, Int) = pair match {
case (one, two) => (two, one)
}
/**
* Task 3:
*
* Using pattern matching, write a function `swap` that swaps the first two elements of
* an array provided its length is at least two.
*/
def swap2(arr: Array[Int]): Array[Int] = arr match {
case Array(first, second, _*) =>
arr(0) = second
arr(1) = first
arr
case _ => arr
}
/**
* Task 4:
*
* Add a case class `Multiple` that is a subclass of the `Item` class. For example,
* `Multiple(10, Article("Blackwell Toaster", 29.95))` describes ten toasters. Of course,
* you should be able to handle any items, such as bundles or multiples, in the second argument.
* Extend the `price` function to handle this new case.
*/
sealed abstract class Item
case class Article(description: String, price: Double) extends Item
case class Bundle(description: String, discount: Double, items: Item*) extends Item
case class Multiple(count: Int, item: Item) extends Item
def price(it: Item): Double = it match {
case Article(_, p) => p
case Bundle(_, disc, its @ _*) => its.map(price).sum - disc
case Multiple(count, item) => count * price(item)
}
/**
* Task 5:
*
* One can use lists to model trees that store values only in the leaves. For example, the list
* `((3 8) 2 (5))` describes the tree
* {{{
* *
* /|\
* * 2 *
* /\ |
* 3 8 5
* }}}
* However, some of the list elements are numbers and others are lists. In Scala, you cannot
* have heterogeneous lists, so you have to use a `List[Any]`. Write a `leafSum` function to
* compute the sum of all elements in the leaves, using pattern matching to differentiate
* between numbers and lists.
*/
def leafSum(xs: List[Any]): Double = {
xs.foldLeft(0.0) {(acc: Double, item) => item match {
case n: Number => acc + n.doubleValue()
case list: List[_] => acc + leafSum(list)
case _ => acc
}
}
}
/**
* Task 6:
*
* A better way of modeling such trees is with case classes. Let's start with binary trees.
* {{{
* sealed abstract class BinaryTree
* case class Leaf(value: Int) extends BinaryTree
* case class Node(left: BinaryTree, right: BinaryTree) extends BinaryTree
* }}}
* Write a function to compute the sum of all elements in the leaves.
*/
object Task6 {
sealed abstract class BinaryTree
case class Leaf(value: Int) extends BinaryTree
case class Node(left: BinaryTree, right: BinaryTree) extends BinaryTree
def leafSum(bt: BinaryTree): Int = bt match {
case leaf: Leaf => leaf.value
case left Node right => leafSum(left) + leafSum(right)
}
}
/**
* Task 7:
*
* Extend the tree in the preceding exercise so that each node can have an arbitrary number
* of children, and reimplement the `leafSum` function. The tree in exercise 5 should be as
* {{{
* Node(Node(Leaf(3), Leaf(8)), Leaf(2), Node(Leaf(5)))
* }}}
*/
object Task7 {
sealed abstract class BinaryTree
case class Leaf(value: Int) extends BinaryTree
case class Node(children: BinaryTree*) extends BinaryTree
def leafSum(bt: BinaryTree): Int = bt match {
case leaf: Leaf => leaf.value
case node: Node => node.children.foldLeft(0)((acc, item) => acc + leafSum(item))
}
}
/**
* Task 8:
*
* Extend the tree in the preceding exercise so that each non-leaf node stores an operator
* in addition to the child nodes. Then write a function `eval` that computes the value.
* For example, the tree
* {{{
* +
* /|\
* * 2 -
* /\ |
* 3 8 5
* }}}
* has value `(3 x 8) + 2 + (-5) = 21`.
*/
object Task8 {
sealed abstract class BinaryTree
case class Leaf(value: Int) extends BinaryTree
case class Node(op: Op, children: BinaryTree*) extends BinaryTree
class Op private(val identity: Int, op: (Int, Int) => Int) {
def apply(a: Int, b: Int): Int = op(a, b)
}
object Op {
val Plus = new Op(0, _ + _)
val Minus = new Op(0, _ - _)
val Product = new Op(1, _ * _)
}
def eval(bt: BinaryTree): Int = bt match {
case leaf: Leaf => leaf.value
case node: Node => node.children.foldLeft(node.op.identity) { (acc, item) =>
node.op(acc, eval(item))
}
}
}
/**
* Task 9:
*
* Write a function that computes the sum of the non-None values in a `List[Option[Int]]`.
* Don't use a match statement.
*/
def sumOfNonNoneValues(xs: List[Option[Int]]): Int = xs.foldLeft(0) { (acc, item) =>
acc + item.getOrElse(0)
}
/**
* Task 10:
*
* Write a function that composes two functions of type `Double => Option[Double]`, yielding
* another function of the same type. The composition should yield `None` if either function does.
* For example,
* {{{
* def f(x: Double) = if (x >= 0) Some(sqrt(x)) else None
* def g(x: Double) = if (x != 1) Some(1 / (x - 1)) else None
* val h = compose(f, g)
* }}}
* Then `h(2)` is `Some(1)`, and `h(1)` and `h(0)` are `None`.
*/
def compose(f1: Double => Option[Double], f2: Double => Option[Double]): Double => Option[Double] = {
(x: Double) => f2(x) match {
case Some(y) => f1(y)
case None => None
}
}
}