51
|
1 |
// Scala Lecture 2
|
|
2 |
//=================
|
|
3 |
|
|
4 |
|
55
|
5 |
// Implicits
|
|
6 |
//===========
|
|
7 |
//
|
|
8 |
// for example adding your own methods to Strings:
|
|
9 |
// imagine you want to increment strings, like
|
|
10 |
//
|
|
11 |
// "HAL".increment
|
|
12 |
//
|
|
13 |
// you can avoid ugly fudges, like a MyString, by
|
|
14 |
// using implicit conversions
|
|
15 |
|
|
16 |
|
|
17 |
implicit class MyString(s: String) {
|
|
18 |
def increment = for (c <- s) yield (c + 1).toChar
|
|
19 |
}
|
|
20 |
|
|
21 |
"HAL".increment
|
|
22 |
|
|
23 |
|
53
|
24 |
|
51
|
25 |
// Option type
|
|
26 |
//=============
|
53
|
27 |
|
|
28 |
//in Java if something unusually happens, you return null
|
|
29 |
//in Scala you use Option
|
|
30 |
// - if the value is present, you use Some(value)
|
|
31 |
// - if no value is present, you use None
|
|
32 |
|
|
33 |
|
|
34 |
List(7,2,3,4,5,6).find(_ < 4)
|
|
35 |
List(5,6,7,8,9).find(_ < 4)
|
|
36 |
|
51
|
37 |
val lst = List(None, Some(1), Some(2), None, Some(3))
|
|
38 |
|
|
39 |
lst.flatten
|
53
|
40 |
|
51
|
41 |
Some(1).get
|
|
42 |
|
53
|
43 |
Some(1).isDefined
|
|
44 |
None.isDefined
|
|
45 |
|
51
|
46 |
val ps = List((3, 0), (3, 2), (4, 2), (2, 0), (1, 0), (1, 1))
|
|
47 |
|
|
48 |
for ((x, y) <- ps) yield {
|
|
49 |
if (y == 0) None else Some(x / y)
|
|
50 |
}
|
|
51 |
|
53
|
52 |
// getOrElse is to set a default value
|
|
53 |
|
|
54 |
val lst = List(None, Some(1), Some(2), None, Some(3))
|
|
55 |
for (x <- lst) yield x getOrElse 0
|
|
56 |
|
|
57 |
|
|
58 |
import scala.util._
|
|
59 |
import io.Source
|
|
60 |
// error handling with option
|
|
61 |
//
|
|
62 |
// Try(something).getOrElse(what_to_do_in_an_exception)
|
|
63 |
|
|
64 |
Source.fromURL("""http://www.inf.kcl.ac.uk/staff/urbanccc/""").mkString
|
|
65 |
|
|
66 |
Try(Source.fromURL("""http://www.inf.kcl.ac.uk/staff/urbanc/""").mkString).getOrElse("")
|
|
67 |
|
|
68 |
Try(Some(Source.fromURL("""http://www.inf.kcl.ac.uk/staff/urbanc/""").mkString)).getOrElse(None)
|
|
69 |
|
|
70 |
|
|
71 |
Integer.parseInt("12u34")
|
|
72 |
|
|
73 |
def get_me_an_int(s: String): Option[Int] =
|
|
74 |
Try(Some(Integer.parseInt(s))).getOrElse(None)
|
|
75 |
|
|
76 |
val lst = List("12345", "foo", "5432", "bar", "x21")
|
|
77 |
for (x <- lst) yield get_me_an_int(x)
|
|
78 |
|
|
79 |
// summing all the numbers
|
|
80 |
val sum = lst.flatMap(get_me_an_int(_)).sum
|
|
81 |
|
|
82 |
|
|
83 |
// This may not look any better than working with null in Java, but to
|
|
84 |
// see the value, you have to put yourself in the shoes of the
|
|
85 |
// consumer of the get_me_an_int function, and imagine you didn't
|
|
86 |
// write that function.
|
|
87 |
//
|
|
88 |
// In Java, if you didn't write this function, you'd have to depend on
|
|
89 |
// the Javadoc of the get_me_an_int. If you didn't look at the Javadoc
|
|
90 |
// for the Java, you might not know that get_me_an_int could return a
|
|
91 |
// null, and your code could potentially throw a NullPointerException.
|
|
92 |
|
|
93 |
|
|
94 |
|
|
95 |
|
|
96 |
// Type abbreviations
|
|
97 |
//====================
|
|
98 |
|
|
99 |
// some syntactic convenience
|
|
100 |
type Pos = (int, Int)
|
|
101 |
|
|
102 |
type Board = List[List[Int]]
|
|
103 |
|
|
104 |
|
|
105 |
|
|
106 |
// No return in Scala
|
|
107 |
//====================
|
|
108 |
|
|
109 |
//You should not use "return" in Scala:
|
|
110 |
//
|
|
111 |
// A return expression, when evaluated, abandons the
|
|
112 |
// current computation and returns to the caller of the
|
|
113 |
// function in which return appears."
|
|
114 |
|
|
115 |
def sq1(x: Int): Int = x * x
|
|
116 |
def sq2(x: Int): Int = return x * x
|
|
117 |
|
|
118 |
def sumq(ls: List[Int]): Int = {
|
|
119 |
(for (x <- ls) yield (return x * x)).sum[Int]
|
|
120 |
}
|
|
121 |
|
|
122 |
sumq(List(1,2,3,4))
|
36
|
123 |
|
53
|
124 |
// last expression in a function is the return statement
|
|
125 |
def square(x: Int): Int = {
|
|
126 |
println(s"The argument is ${x}.")
|
|
127 |
x * x
|
|
128 |
}
|
|
129 |
|
55
|
130 |
|
|
131 |
|
53
|
132 |
// Pattern Matching
|
|
133 |
//==================
|
|
134 |
|
|
135 |
// A powerful tool which is supposed to come to Java in a few years
|
|
136 |
// time (https://www.youtube.com/watch?v=oGll155-vuQ)...Scala already
|
|
137 |
// has it for many years ;o)
|
|
138 |
|
|
139 |
// The general schema:
|
|
140 |
//
|
|
141 |
// expression match {
|
|
142 |
// case pattern1 => expression1
|
|
143 |
// case pattern2 => expression2
|
|
144 |
// ...
|
|
145 |
// case patternN => expressionN
|
|
146 |
// }
|
|
147 |
|
|
148 |
|
|
149 |
// remember
|
|
150 |
val lst = List(None, Some(1), Some(2), None, Some(3)).flatten
|
|
151 |
|
|
152 |
|
|
153 |
def my_flatten(xs: List[Option[Int]]): List[Int] = {
|
|
154 |
...
|
|
155 |
}
|
|
156 |
|
|
157 |
|
|
158 |
def my_flatten(lst: List[Option[Int]]): List[Int] = lst match {
|
|
159 |
case Nil => Nil
|
|
160 |
case None::xs => my_flatten(xs)
|
|
161 |
case Some(n)::xs => n::my_flatten(xs)
|
|
162 |
}
|
|
163 |
|
|
164 |
|
|
165 |
// another example
|
|
166 |
def get_me_a_string(n: Int): String = n match {
|
|
167 |
case 0 => "zero"
|
|
168 |
case 1 => "one"
|
|
169 |
case 2 => "two"
|
|
170 |
case _ => "many"
|
|
171 |
}
|
|
172 |
|
55
|
173 |
// User-defined Datatypes
|
|
174 |
//========================
|
|
175 |
|
|
176 |
abstract class Tree
|
|
177 |
case class Node(elem: Int, left: Tree, right: Tree) extends Tree
|
|
178 |
case class Leaf() extends Tree
|
|
179 |
|
|
180 |
def insert(tr: Tree, n: Int): Tree = tr match {
|
|
181 |
case Leaf() => Node(n, Leaf(), Leaf())
|
|
182 |
case Node(m, left, right) =>
|
|
183 |
if (n == m) Node(m, left, right)
|
|
184 |
else if (n < m) Node(m, insert(left, n), right)
|
|
185 |
else Node(m, left, insert(right, n))
|
|
186 |
}
|
|
187 |
|
|
188 |
|
|
189 |
val t1 = Node(4, Node(2, Leaf(), Leaf()), Node(7, Leaf(), Leaf()))
|
|
190 |
insert(t1, 3)
|
|
191 |
|
|
192 |
def balance(tr: Tree): Int = tr match {
|
|
193 |
case Leaf() => 0
|
|
194 |
case Node(_, left, right) => balance(left) - balance(right)
|
|
195 |
}
|
|
196 |
|
|
197 |
|
|
198 |
// another example
|
|
199 |
|
|
200 |
abstract class Person
|
|
201 |
case class King() extends Person
|
|
202 |
case class Peer(deg: String, terr: String, succ: Int) extends Person
|
|
203 |
case class Knight(name: String) extends Person
|
|
204 |
case class Peasant(name: String) extends Person
|
|
205 |
case class Clown() extends Person
|
|
206 |
|
|
207 |
def title(p: Person): String = p match {
|
|
208 |
case King() => "His Majesty the King"
|
|
209 |
case Peer(deg, terr, _) => s"The ${deg} of ${terr}"
|
|
210 |
case Knight(name) => s"Sir ${name}"
|
|
211 |
case Peasant(name) => name
|
|
212 |
}
|
|
213 |
|
|
214 |
def superior(p1: Person, p2: Person): Boolean = (p1, p2) match {
|
|
215 |
case (King(), _) => true
|
|
216 |
case (Peer(_,_,_), Knight(_)) => true
|
|
217 |
case (Peer(_,_,_), Peasant(_)) => true
|
|
218 |
case (Peer(_,_,_), Clown()) => true
|
|
219 |
case (Knight(_), Peasant(_)) => true
|
|
220 |
case (Knight(_), Clown()) => true
|
|
221 |
case (Clown(), Peasant(_)) => true
|
|
222 |
case _ => false
|
|
223 |
}
|
|
224 |
|
|
225 |
val people = List(Knight("David"),
|
|
226 |
Peer("Duke", "Norfolk", 42),
|
|
227 |
Peasant("Christian"),
|
|
228 |
King(),
|
|
229 |
Clown())
|
|
230 |
|
|
231 |
println(people.sortWith(superior(_, _)))
|
53
|
232 |
|
|
233 |
// Higher-Order Functions
|
|
234 |
//========================
|
|
235 |
|
|
236 |
// functions can take functions as arguments
|
|
237 |
|
|
238 |
val lst = (1 to 10).toList
|
|
239 |
|
|
240 |
def even(x: Int): Boolean = x % 2 == 0
|
|
241 |
def odd(x: Int): Boolean = x % 2 == 1
|
|
242 |
|
|
243 |
lst.filter(x => even(x))
|
|
244 |
lst.filter(even(_))
|
|
245 |
lst.filter(even)
|
|
246 |
|
|
247 |
lst.find(_ > 8)
|
|
248 |
|
|
249 |
def square(x: Int): Int = x * x
|
|
250 |
|
|
251 |
lst.map(square)
|
|
252 |
|
|
253 |
lst.map(square).filter(_ > 4)
|
|
254 |
|
55
|
255 |
lst.map(square).filter(_ > 4).map(square)
|
53
|
256 |
|
55
|
257 |
// in my collatz.scala
|
|
258 |
//(1 to bnd).map(i => (collatz(i), i)).maxBy(_._1)
|
|
259 |
|
|
260 |
|
|
261 |
// type of functions
|
|
262 |
def my_map_int(lst: List[Int], f: Int => Int): List[Int] = lst match {
|
|
263 |
case Nil => Nil
|
|
264 |
case x::xs => f(x)::my_map_int(xs, f)
|
|
265 |
}
|
|
266 |
|
|
267 |
my_map_int(lst, square)
|
|
268 |
|
|
269 |
|
|
270 |
def sumOf(f: Int => Int, lst: List[Int]): Int = lst match {
|
|
271 |
case Nil => 0
|
|
272 |
case x::xs => f(x) + sumOf(f, xs)
|
|
273 |
}
|
|
274 |
|
|
275 |
def sum_squares(lst: List[Int]) = sumOf(square, lst)
|
|
276 |
def sum_cubes(lst: List[Int]) = sumOf(x => x * x * x, lst)
|
|
277 |
|
|
278 |
sum_squares(lst)
|
|
279 |
sum_cubes(lst)
|
53
|
280 |
|
|
281 |
|
|
282 |
// Sudoku
|
|
283 |
//========
|
|
284 |
|
55
|
285 |
val game0 = """.14.6.3..
|
|
286 |
|62...4..9
|
|
287 |
|.8..5.6..
|
|
288 |
|.6.2....3
|
|
289 |
|.7..1..5.
|
|
290 |
|5....9.6.
|
|
291 |
|..6.2..3.
|
|
292 |
|1..5...92
|
|
293 |
|..7.9.41.""".stripMargin.replaceAll("\\n", "")
|
|
294 |
|
|
295 |
|
|
296 |
type Pos = (Int, Int)
|
|
297 |
val EmptyValue = '.'
|
|
298 |
val MaxValue = 9
|
|
299 |
|
|
300 |
val allValues = "123456789".toList
|
|
301 |
val indexes = (0 to 8).toList
|
|
302 |
|
|
303 |
def empty(game: String) = game.indexOf(EmptyValue)
|
|
304 |
def emptyPosition(game: String) = (empty(game) % MaxValue, empty(game) / MaxValue)
|
|
305 |
def isDone(game: String) = empty(game) == -1
|
|
306 |
|
|
307 |
def row(game: String, y: Int): List[Char] = indexes.map(col => game(y * MaxValue + col))
|
|
308 |
def col(game: String, x: Int): List[Char] = indexes.map(row => game(x + row * MaxValue))
|
|
309 |
|
|
310 |
def box(game: String, pos: Pos): List[Char] = {
|
|
311 |
def base(p: Int): Int = (p / 3) * 3
|
|
312 |
val x0 = base(pos._1)
|
|
313 |
val y0 = base(pos._2)
|
|
314 |
val ys = (y0 until y0 + 3).toList
|
|
315 |
(x0 until x0 + 3).toList.flatMap(x => ys.map(y => game(x + y * MaxValue)))
|
|
316 |
}
|
|
317 |
|
|
318 |
|
|
319 |
//row(game0, 0)
|
|
320 |
//row(game0, 1)
|
|
321 |
//box(game0, (3,1))
|
|
322 |
|
|
323 |
def update(game: String, pos: Int, value: Char): String = game.updated(pos, value)
|
|
324 |
|
|
325 |
def toAvoid(game: String, pos: Pos): List[Char] =
|
|
326 |
(col(game, pos._1) ++ row(game, pos._2) ++ box(game, pos)).distinct
|
|
327 |
|
|
328 |
def candidates(game: String, pos: Pos): List[Char] = allValues diff toAvoid(game,pos)
|
|
329 |
|
|
330 |
//candidates(game0, (0,0))
|
|
331 |
|
|
332 |
def pretty(game: String): String = "\n" + (game sliding (MaxValue, MaxValue) mkString "\n")
|
|
333 |
|
|
334 |
def search(game: String): List[String] = {
|
|
335 |
if (isDone(game)) List(game)
|
|
336 |
else
|
|
337 |
candidates(game, emptyPosition(game)).par.map(c => search(update(game, empty(game), c))).toList.flatten
|
|
338 |
}
|
|
339 |
|
|
340 |
|
|
341 |
val game1 = """23.915...
|
|
342 |
|...2..54.
|
|
343 |
|6.7......
|
|
344 |
|..1.....9
|
|
345 |
|89.5.3.17
|
|
346 |
|5.....6..
|
|
347 |
|......9.5
|
|
348 |
|.16..7...
|
|
349 |
|...329..1""".stripMargin.replaceAll("\\n", "")
|
|
350 |
|
|
351 |
// game that is in the hard category
|
|
352 |
val game2 = """8........
|
|
353 |
|..36.....
|
|
354 |
|.7..9.2..
|
|
355 |
|.5...7...
|
|
356 |
|....457..
|
|
357 |
|...1...3.
|
|
358 |
|..1....68
|
|
359 |
|..85...1.
|
|
360 |
|.9....4..""".stripMargin.replaceAll("\\n", "")
|
|
361 |
|
|
362 |
// game with multiple solutions
|
|
363 |
val game3 = """.8...9743
|
|
364 |
|.5...8.1.
|
|
365 |
|.1.......
|
|
366 |
|8....5...
|
|
367 |
|...8.4...
|
|
368 |
|...3....6
|
|
369 |
|.......7.
|
|
370 |
|.3.5...8.
|
|
371 |
|9724...5.""".stripMargin.replaceAll("\\n", "")
|
|
372 |
|
|
373 |
search(game0).map(pretty)
|
|
374 |
search(game1).map(pretty)
|
|
375 |
|
|
376 |
// for measuring time
|
|
377 |
def time_needed[T](i: Int, code: => T) = {
|
|
378 |
val start = System.nanoTime()
|
|
379 |
for (j <- 1 to i) code
|
|
380 |
val end = System.nanoTime()
|
|
381 |
((end - start) / i / 1.0e9) + " secs"
|
|
382 |
}
|
|
383 |
|
|
384 |
search(game2).map(pretty)
|
|
385 |
search(game3).distinct.map(pretty).length
|
|
386 |
time_needed(3, search(game2))
|
|
387 |
time_needed(3, search(game3))
|
|
388 |
|
53
|
389 |
|
|
390 |
|
|
391 |
|
39
|
392 |
|