-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathChapter21.scala
274 lines (236 loc) · 7.79 KB
/
Chapter21.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
import Chapter11.Fraction
import java.awt.Point
import scala.annotation.tailrec
import scala.collection.immutable.IndexedSeq
import scala.io.StdIn
import scala.language.implicitConversions
object Chapter21 {
/**
* Task 1:
*
* How does `->` work? That is, how can `"Hello" -> 42` and `42 -> "Hello"` be pairs
* `("Hello", 42)` and `(42, "Hello")`? Hint: `Predef.any2ArrowAssoc`.
*
* Solution:
*
* Currently, as of Scala 2.11.x, its implemented by using `implicit class ArrowAssoc` that
* enriches any instance with `->` method.
*/
/**
* Task 2:
*
* Define an operator `+%` that adds a given percentage to a value. For example,
* `120 +% 10` should be `132`. Hint: Since operators are methods, not functions,
* you will also need to provide an `implicit`.
*/
implicit class PercentAdder(private val value: Int) {
def +%(percent: Int): Int = value + ((value * percent) / 100d).toInt
}
// as of Scala 2.11.x not needed any more, since we can use implicit class
//implicit def int2PercentAdder(value: Int): PercentAdder = new PercentAdder(value)
/**
* Task 3:
*
* Define a `!` operator that computes the factorial of an integer. For example,
* `5!` is `120`. You will need an enrichment class and an implicit conversion.
*/
implicit class Int2Factorial(private val value: Int) {
def ! : Int = {
@tailrec
def fact(acc: Int, n: Int): Int = {
if (n == 1) acc
else fact(acc * n, n - 1)
}
fact(1, value)
}
}
/**
* Task 4:
*
* Some people are fond of "fluent APIs" that read vaguely like English sentences.
* Create such an API for reading integers, floating-point numbers, and strings from the console.
* For example:
* {{{
* Read in aString askingFor "Your name" and
* anInt askingFor "Your age" and
* aDouble askingFor "Your weight".
* }}}
*/
sealed trait FluentFieldType
object aString extends FluentFieldType
object anInt extends FluentFieldType
object aDouble extends FluentFieldType
def Read: FluentReader = new FluentReader
class FluentReader {
private var data: List[(String, Any)] = List()
private var nextType: FluentFieldType = aString
def getData: List[(String, Any)] = data
def in(fieldType: FluentFieldType): FluentReader = {
nextType = fieldType
this
}
def and(fieldType: FluentFieldType): FluentReader = in(fieldType)
def askingFor(fieldName: String): FluentReader = {
print(fieldName + ": ")
val value = nextType match {
case _: aString.type => StdIn.readLine()
case _: anInt.type => StdIn.readInt()
case _: aDouble.type => StdIn.readDouble()
}
data = data :+ (fieldName, value)
this
}
}
/**
* Task 5:
*
* Provide the machinery that is needed to compute
* {{{
* smaller(Fraction(1, 7), Fraction(2, 9))
* }}}
* in Section 21.6, "Implicit Conversions with Implicit Parameters," on page 310.
* Supply a `class RichFraction` that extends `Ordered[Fraction]`.
*/
def smaller[T](a: T, b: T)(implicit order: T => Ordered[T]): T =
if (order(a) < b) a
else b
implicit class RichFraction(self: Fraction) extends Ordered[Fraction] {
override def compare(that: Fraction): Int = (self - that).num
}
/**
* Task 6:
*
* Compare objects of the `class java.awt.Point` by lexicographic comparison.
*/
implicit class LexicographicPointOrdering(self: Point) extends Ordered[Point] {
override def compare(that: Point): Int =
if (self.x == that.x) self.y - that.y
else self.x - that.x
}
/**
* Task 7:
*
* Continue the previous exercise, comparing two points according to their distance to
* the origin. How can you switch between the two orderings?
*
* Solution:
*
* We can switch between the two orderings by importing appropriate `implicit class`:
* {{{
* import Chapter21.LexicographicPointOrdering
* }}}
* or
* {{{
* import Chapter21.DistancePointOrdering
* }}}
*/
implicit class DistancePointOrdering(self: Point) extends Ordered[Point] {
override def compare(that: Point): Int = {
val d1 = self.distance(0, 0)
val d2 = that.distance(0, 0)
if (d1 < d2) -1
else if (d1 > d2) 1
else 0
}
}
/**
* Task 8:
*
* Use the `implicitly` command in the REPL to summon the implicit objects described in
* Section 21.5, "Implicit Parameters," on page 309 and
* Section 21.6, "Implicit Conversions with Implicit Parameters," on page 310.
* What objects do you get?
*
* Solution:
*
* To start the REPL console, start the following command in the project root directory:
* {{{
* activator console
* }}}
* Here is the REPL output:
* {{{
* scala> import Chapter21._
* import Chapter21._
*
* scala> import Chapter11.Fraction
* import Chapter11.Fraction
*
* scala> implicitly[Delimiters]
* res0: Chapter21.Delimiters = Delimiters(<<,>>)
*
* scala> implicitly[Ordered[Fraction]]
* <console>:15: error: could not find implicit value for parameter e: Ordered[Chapter11.Fraction]
* implicitly[Ordered[Fraction]]
* ^
*
* scala> implicitly[Ordering[Fraction]]
* res2: Ordering[Chapter11.Fraction] = Chapter21$FractionOrdering@3af82646
*
* }}}
* For the second case, `implicitly[Ordered[Fraction]]`, it didn't work, since there is no
* appropriate implicit value defined. And our `implicit class RichFraction` is not suitable
* since it requires an input parameter.
*/
case class Delimiters(left: String, right: String)
implicit val quoteDelimiters = Delimiters("<<", ">>")
class FractionOrdering extends Ordering[Fraction] {
override def compare(x: Fraction, y: Fraction): Int = x.compare(y)
}
implicit val fractionOrdering = new FractionOrdering
/**
* Task 9:
*
* Look up the `=:=` object in `Predef.scala`. Explain how it works.
*
* Solution:
*
* It is defined like this:
* {{{
* sealed abstract class =:=[From, To] extends (From => To) with Serializable
* private[this] final val singleton_=:= = new =:=[Any,Any] { def apply(x: Any): Any = x }
* object =:= {
* implicit def tpEquals[A]: A =:= A = singleton_=:=.asInstanceOf[A =:= A]
* }
* }}}
* So, `=:=` is a `Function` class with one argument and with one singleton instance and
* implicit conversion method `tpEquals`, defined in its companion object.
* When it is used as implicit evidence parameter:
* {{{
* def someMethod[A, B](obj: A)(implicit ev: A =:= B): Unit = {
* //can call methods, defined in B
* obj.methodInB()
* }
* }}}
* compiler sees that implicit argument is a function with one parameter, so it calls it
* {{{
* ev(obj).methodInB()
* }}}
* to convert instance from one type `A` to the given type (`B` in this case).
* In this way compiler can check/prove that instance confirms to the given constraint
* (`=:=` equal types, in this case).
*/
/**
* Task 10:
*
* The result of `"abc".map(_.toUpper)` is a `String`, but the result of `"abc".map(_.toInt)`
* is a Vector. Find out why.
*
* Solution:
*
* Since `CanBuildFrom` factory trait for `String` is defined in the `WrappedString` companion
* object like this:
* {{{
* implicit def canBuildFrom: CanBuildFrom[WrappedString, Char, WrappedString]
* }}}
* it can build result `String` collection only for `Char` elements.
* For other element types, like `Int`, as defined in the `IndexedSeq` companion object
* {{{
* def newBuilder[A]: Builder[A, IndexedSeq[A]] = Vector.newBuilder[A]
* }}}
* `Vector` is the current default implementation.
*/
def stringMapTest(): Unit = {
val str: String = "abc".map(_.toUpper)
val seq: IndexedSeq[Int] = "abc".map(_.toInt)
}
}