67
|
1 |
// Scala Lecture 3
|
|
2 |
//=================
|
|
3 |
|
320
|
4 |
// - last week
|
|
5 |
//
|
|
6 |
// option type
|
|
7 |
// higher-order function
|
|
8 |
|
|
9 |
|
323
|
10 |
def add(x: Int, y: Int) : Int = x + y
|
|
11 |
|
|
12 |
def plus5(x: Int) : Int = add(5, x)
|
|
13 |
|
|
14 |
plus5(6)
|
|
15 |
|
|
16 |
def add2(x: Int)(y: Int) : Int = x + y
|
|
17 |
|
|
18 |
def plus3(y: Int) : Int => Int = add2(3)(y)
|
|
19 |
|
|
20 |
plus3(9)
|
|
21 |
|
|
22 |
List(1,2,3,4,5).map(add2(3))
|
|
23 |
List(1,2,3,4,5).map(add(3, _))
|
|
24 |
|
|
25 |
type Pos = (Int, Int)
|
|
26 |
|
|
27 |
def test(p: Pos) = {
|
|
28 |
if (p._1 < 5 && p._2 < 5) {
|
|
29 |
Some(p)
|
|
30 |
}
|
|
31 |
}
|
|
32 |
|
|
33 |
val l = List((1,2), (5,3), (2,5), (1,3))
|
|
34 |
|
|
35 |
l.map(test).flatten
|
320
|
36 |
|
|
37 |
// Recursion Again ;o)
|
|
38 |
//====================
|
|
39 |
|
217
|
40 |
|
|
41 |
// A Web Crawler / Email Harvester
|
|
42 |
//=================================
|
|
43 |
//
|
|
44 |
// the idea is to look for links using the
|
|
45 |
// regular expression "https?://[^"]*" and for
|
218
|
46 |
// email addresses using yet another regex.
|
217
|
47 |
|
|
48 |
import io.Source
|
|
49 |
import scala.util._
|
155
|
50 |
|
217
|
51 |
// gets the first 10K of a web-page
|
|
52 |
def get_page(url: String) : String = {
|
|
53 |
Try(Source.fromURL(url)("ISO-8859-1").take(10000).mkString).
|
320
|
54 |
getOrElse { println(s" Problem with: $url"); ""}
|
217
|
55 |
}
|
155
|
56 |
|
217
|
57 |
// regex for URLs and emails
|
|
58 |
val http_pattern = """"https?://[^"]*"""".r
|
|
59 |
val email_pattern = """([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})""".r
|
|
60 |
|
218
|
61 |
// val s = "foo bla christian@kcl.ac.uk 1234567"
|
|
62 |
// email_pattern.findAllIn(s).toList
|
155
|
63 |
|
217
|
64 |
// drops the first and last character from a string
|
|
65 |
def unquote(s: String) = s.drop(1).dropRight(1)
|
155
|
66 |
|
217
|
67 |
def get_all_URLs(page: String): Set[String] =
|
|
68 |
http_pattern.findAllIn(page).map(unquote).toSet
|
155
|
69 |
|
320
|
70 |
// a naive version of crawl - searches until a given depth,
|
217
|
71 |
// visits pages potentially more than once
|
320
|
72 |
def crawl(url: String, n: Int) : Unit = {
|
|
73 |
if (n == 0) ()
|
217
|
74 |
else {
|
|
75 |
println(s" Visiting: $n $url")
|
321
|
76 |
val page = get_page(url)
|
|
77 |
for (u <- get_all_URLs(page)) crawl(u, n - 1)
|
217
|
78 |
}
|
155
|
79 |
}
|
|
80 |
|
217
|
81 |
// some starting URLs for the crawler
|
|
82 |
val startURL = """https://nms.kcl.ac.uk/christian.urban/"""
|
320
|
83 |
|
217
|
84 |
crawl(startURL, 2)
|
|
85 |
|
323
|
86 |
for (x <- List(1,2,3,4,5,6)) println(x)
|
318
|
87 |
|
320
|
88 |
// a primitive email harvester
|
|
89 |
def emails(url: String, n: Int) : Set[String] = {
|
|
90 |
if (n == 0) Set()
|
|
91 |
else {
|
|
92 |
println(s" Visiting: $n $url")
|
|
93 |
val page = get_page(url)
|
|
94 |
val new_emails = email_pattern.findAllIn(page).toSet
|
323
|
95 |
new_emails ++ (for (u <- get_all_URLs(page).par) yield emails(u, n - 1)).flatten
|
320
|
96 |
}
|
218
|
97 |
}
|
|
98 |
|
323
|
99 |
emails(startURL, 3)
|
218
|
100 |
|
|
101 |
|
320
|
102 |
// if we want to explore the internet "deeper", then we
|
|
103 |
// first have to parallelise the request of webpages:
|
|
104 |
//
|
|
105 |
// scala -cp scala-parallel-collections_2.13-0.2.0.jar
|
|
106 |
// import scala.collection.parallel.CollectionConverters._
|
155
|
107 |
|
|
108 |
|
|
109 |
|
320
|
110 |
// another well-known example
|
|
111 |
//============================
|
178
|
112 |
|
320
|
113 |
def move(from: Char, to: Char) =
|
|
114 |
println(s"Move disc from $from to $to!")
|
67
|
115 |
|
320
|
116 |
def hanoi(n: Int, from: Char, via: Char, to: Char) : Unit = {
|
|
117 |
if (n == 0) ()
|
|
118 |
else {
|
|
119 |
hanoi(n - 1, from, to, via)
|
|
120 |
move(from, to)
|
|
121 |
hanoi(n - 1, via, from, to)
|
|
122 |
}
|
|
123 |
}
|
67
|
124 |
|
320
|
125 |
hanoi(4, 'A', 'B', 'C')
|
67
|
126 |
|
155
|
127 |
|
|
128 |
|
217
|
129 |
// Jumping Towers
|
|
130 |
//================
|
|
131 |
|
|
132 |
|
|
133 |
// the first n prefixes of xs
|
|
134 |
// for 1 => include xs
|
|
135 |
|
|
136 |
def moves(xs: List[Int], n: Int) : List[List[Int]] = (xs, n) match {
|
|
137 |
case (Nil, _) => Nil
|
323
|
138 |
case (_, 0) => Nil
|
|
139 |
case (y::ys, n) => xs :: moves(ys, n - 1)
|
217
|
140 |
}
|
|
141 |
|
|
142 |
|
|
143 |
moves(List(5,1,0), 1)
|
|
144 |
moves(List(5,1,0), 2)
|
|
145 |
moves(List(5,1,0), 5)
|
|
146 |
|
|
147 |
// checks whether a jump tour exists at all
|
|
148 |
|
|
149 |
def search(xs: List[Int]) : Boolean = xs match {
|
|
150 |
case Nil => true
|
321
|
151 |
case x::xs =>
|
|
152 |
if (xs.length < x) true
|
|
153 |
else moves(xs, x).exists(search(_))
|
217
|
154 |
}
|
|
155 |
|
|
156 |
|
|
157 |
search(List(5,3,2,5,1,1))
|
|
158 |
search(List(3,5,1,0,0,0,1))
|
|
159 |
search(List(3,5,1,0,0,0,0,1))
|
|
160 |
search(List(3,5,1,0,0,0,1,1))
|
|
161 |
search(List(3,5,1))
|
|
162 |
search(List(5,1,1))
|
|
163 |
search(Nil)
|
|
164 |
search(List(1))
|
|
165 |
search(List(5,1,1))
|
|
166 |
search(List(3,5,1,0,0,0,0,0,0,0,0,1))
|
|
167 |
|
|
168 |
// generates *all* jump tours
|
321
|
169 |
// if we are only interested in the shortest one, we could
|
217
|
170 |
// shortcircut the calculation and only return List(x) in
|
|
171 |
// case where xs.length < x, because no tour can be shorter
|
|
172 |
// than 1
|
|
173 |
//
|
|
174 |
|
|
175 |
def jumps(xs: List[Int]) : List[List[Int]] = xs match {
|
|
176 |
case Nil => Nil
|
321
|
177 |
case x::xs => {
|
217
|
178 |
val children = moves(xs, x)
|
320
|
179 |
val results = children.map(cs => jumps(cs).map(x :: _)).flatten
|
|
180 |
if (xs.length < x) List(x)::results else results
|
217
|
181 |
}
|
|
182 |
}
|
|
183 |
|
320
|
184 |
jumps(List(5,3,2,5,1,1)).minBy(_.length)
|
217
|
185 |
jumps(List(3,5,1,2,1,2,1))
|
|
186 |
jumps(List(3,5,1,2,3,4,1))
|
|
187 |
jumps(List(3,5,1,0,0,0,1))
|
|
188 |
jumps(List(3,5,1))
|
|
189 |
jumps(List(5,1,1))
|
|
190 |
jumps(Nil)
|
|
191 |
jumps(List(1))
|
|
192 |
jumps(List(5,1,2))
|
|
193 |
moves(List(1,2), 5)
|
|
194 |
jumps(List(1,5,1,2))
|
|
195 |
jumps(List(3,5,1,0,0,0,0,0,0,0,0,1))
|
|
196 |
|
|
197 |
jumps(List(5,3,2,5,1,1)).minBy(_.length)
|
|
198 |
jumps(List(1,3,5,8,9,2,6,7,6,8,9)).minBy(_.length)
|
|
199 |
jumps(List(1,3,6,1,0,9)).minBy(_.length)
|
|
200 |
jumps(List(2,3,1,1,2,4,2,0,1,1)).minBy(_.length)
|
|
201 |
|
|
202 |
|
|
203 |
|
318
|
204 |
|
|
205 |
|
|
206 |
|
320
|
207 |
// User-defined Datatypes
|
|
208 |
//========================
|
|
209 |
|
323
|
210 |
abstract class Tree
|
|
211 |
case class Leaf(x: Int) extends Tree
|
|
212 |
case class Node(s: String, left: Tree, right: Tree) extends Tree
|
|
213 |
|
|
214 |
List(Leaf(20), Node("foo", Leaf(1), Leaf(2)))
|
320
|
215 |
|
321
|
216 |
sealed abstract class Colour
|
320
|
217 |
case object Red extends Colour
|
|
218 |
case object Green extends Colour
|
|
219 |
case object Blue extends Colour
|
323
|
220 |
case object Yellow extends Colour
|
320
|
221 |
|
|
222 |
|
|
223 |
def fav_colour(c: Colour) : Boolean = c match {
|
|
224 |
case Green => true
|
323
|
225 |
case _ => false
|
320
|
226 |
}
|
|
227 |
|
|
228 |
fav_colour(Green)
|
|
229 |
|
|
230 |
// ... a tiny bit more useful: Roman Numerals
|
|
231 |
|
321
|
232 |
sealed abstract class RomanDigit
|
320
|
233 |
case object I extends RomanDigit
|
|
234 |
case object V extends RomanDigit
|
|
235 |
case object X extends RomanDigit
|
|
236 |
case object L extends RomanDigit
|
|
237 |
case object C extends RomanDigit
|
|
238 |
case object D extends RomanDigit
|
|
239 |
case object M extends RomanDigit
|
|
240 |
|
|
241 |
type RomanNumeral = List[RomanDigit]
|
|
242 |
|
323
|
243 |
List(X,I,M,D)
|
320
|
244 |
|
|
245 |
/*
|
|
246 |
I -> 1
|
|
247 |
II -> 2
|
|
248 |
III -> 3
|
|
249 |
IV -> 4
|
|
250 |
V -> 5
|
|
251 |
VI -> 6
|
|
252 |
VII -> 7
|
|
253 |
VIII -> 8
|
|
254 |
IX -> 9
|
|
255 |
X -> 10
|
|
256 |
*/
|
|
257 |
|
|
258 |
def RomanNumeral2Int(rs: RomanNumeral): Int = rs match {
|
|
259 |
case Nil => 0
|
|
260 |
case M::r => 1000 + RomanNumeral2Int(r)
|
|
261 |
case C::M::r => 900 + RomanNumeral2Int(r)
|
|
262 |
case D::r => 500 + RomanNumeral2Int(r)
|
|
263 |
case C::D::r => 400 + RomanNumeral2Int(r)
|
|
264 |
case C::r => 100 + RomanNumeral2Int(r)
|
|
265 |
case X::C::r => 90 + RomanNumeral2Int(r)
|
|
266 |
case L::r => 50 + RomanNumeral2Int(r)
|
|
267 |
case X::L::r => 40 + RomanNumeral2Int(r)
|
|
268 |
case X::r => 10 + RomanNumeral2Int(r)
|
|
269 |
case I::X::r => 9 + RomanNumeral2Int(r)
|
|
270 |
case V::r => 5 + RomanNumeral2Int(r)
|
|
271 |
case I::V::r => 4 + RomanNumeral2Int(r)
|
|
272 |
case I::r => 1 + RomanNumeral2Int(r)
|
|
273 |
}
|
|
274 |
|
|
275 |
RomanNumeral2Int(List(I,V)) // 4
|
|
276 |
RomanNumeral2Int(List(I,I,I,I)) // 4 (invalid Roman number)
|
|
277 |
RomanNumeral2Int(List(V,I)) // 6
|
|
278 |
RomanNumeral2Int(List(I,X)) // 9
|
|
279 |
RomanNumeral2Int(List(M,C,M,L,X,X,I,X)) // 1979
|
|
280 |
RomanNumeral2Int(List(M,M,X,V,I,I)) // 2017
|
|
281 |
|
|
282 |
|
|
283 |
// String interpolations as patterns
|
|
284 |
|
|
285 |
val date = "2019-11-26"
|
|
286 |
val s"$year-$month-$day" = date
|
|
287 |
|
|
288 |
def parse_date(date: String) : Option[(Int, Int, Int)]= date match {
|
|
289 |
case s"$year-$month-$day" => Some((day.toInt, month.toInt, year.toInt))
|
|
290 |
case s"$day/$month/$year" => Some((day.toInt, month.toInt, year.toInt))
|
|
291 |
case s"$day.$month.$year" => Some((day.toInt, month.toInt, year.toInt))
|
|
292 |
case _ => None
|
|
293 |
}
|
318
|
294 |
|
320
|
295 |
parse_date("2019-11-26")
|
|
296 |
parse_date("26/11/2019")
|
|
297 |
parse_date("26.11.2019")
|
|
298 |
|
|
299 |
|
|
300 |
// User-defined Datatypes and Pattern Matching
|
|
301 |
//=============================================
|
|
302 |
|
|
303 |
|
|
304 |
|
|
305 |
|
|
306 |
// Tail recursion
|
|
307 |
//================
|
|
308 |
|
|
309 |
|
|
310 |
def fact(n: Long): Long =
|
|
311 |
if (n == 0) 1 else n * fact(n - 1)
|
|
312 |
|
|
313 |
def factB(n: BigInt): BigInt =
|
|
314 |
if (n == 0) 1 else n * factB(n - 1)
|
|
315 |
|
|
316 |
factB(100000)
|
|
317 |
|
|
318 |
fact(10) //ok
|
|
319 |
fact(10000) // produces a stackoverflow
|
|
320 |
|
|
321 |
def factT(n: BigInt, acc: BigInt): BigInt =
|
|
322 |
if (n == 0) acc else factT(n - 1, n * acc)
|
|
323 |
|
|
324 |
factT(10, 1)
|
|
325 |
println(factT(100000, 1))
|
|
326 |
|
|
327 |
// there is a flag for ensuring a function is tail recursive
|
|
328 |
import scala.annotation.tailrec
|
|
329 |
|
|
330 |
@tailrec
|
|
331 |
def factT(n: BigInt, acc: BigInt): BigInt =
|
|
332 |
if (n == 0) acc else factT(n - 1, n * acc)
|
|
333 |
|
|
334 |
|
|
335 |
|
|
336 |
// for tail-recursive functions the Scala compiler
|
|
337 |
// generates loop-like code, which does not need
|
|
338 |
// to allocate stack-space in each recursive
|
|
339 |
// call; Scala can do this only for tail-recursive
|
|
340 |
// functions
|
|
341 |
|
155
|
342 |
// tail recursive version that searches
|
158
|
343 |
// for all solutions
|
|
344 |
|
155
|
345 |
def searchT(games: List[String], sols: List[String]): List[String] = games match {
|
|
346 |
case Nil => sols
|
|
347 |
case game::rest => {
|
|
348 |
if (isDone(game)) searchT(rest, game::sols)
|
|
349 |
else {
|
|
350 |
val cs = candidates(game, emptyPosition(game))
|
|
351 |
searchT(cs.map(c => update(game, empty(game), c)) ::: rest, sols)
|
|
352 |
}
|
|
353 |
}
|
67
|
354 |
}
|
|
355 |
|
158
|
356 |
searchT(List(game3), List()).map(pretty)
|
|
357 |
|
|
358 |
|
155
|
359 |
// tail recursive version that searches
|
|
360 |
// for a single solution
|
158
|
361 |
|
155
|
362 |
def search1T(games: List[String]): Option[String] = games match {
|
67
|
363 |
case Nil => None
|
155
|
364 |
case game::rest => {
|
|
365 |
if (isDone(game)) Some(game)
|
|
366 |
else {
|
|
367 |
val cs = candidates(game, emptyPosition(game))
|
|
368 |
search1T(cs.map(c => update(game, empty(game), c)) ::: rest)
|
|
369 |
}
|
|
370 |
}
|
67
|
371 |
}
|
|
372 |
|
158
|
373 |
search1T(List(game3)).map(pretty)
|
217
|
374 |
time_needed(10, search1T(List(game3)))
|
|
375 |
|
158
|
376 |
|
155
|
377 |
// game with multiple solutions
|
|
378 |
val game3 = """.8...9743
|
|
379 |
|.5...8.1.
|
|
380 |
|.1.......
|
|
381 |
|8....5...
|
|
382 |
|...8.4...
|
|
383 |
|...3....6
|
|
384 |
|.......7.
|
|
385 |
|.3.5...8.
|
|
386 |
|9724...5.""".stripMargin.replaceAll("\\n", "")
|
|
387 |
|
158
|
388 |
searchT(List(game3), Nil).map(pretty)
|
155
|
389 |
search1T(List(game3)).map(pretty)
|
67
|
390 |
|
77
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
391 |
// Moral: Whenever a recursive function is resource-critical
|
158
|
392 |
// (i.e. works with large recursion depth), then you need to
|
77
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
393 |
// write it in tail-recursive fashion.
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
394 |
//
|
155
|
395 |
// Unfortuantely, Scala because of current limitations in
|
|
396 |
// the JVM is not as clever as other functional languages. It can
|
77
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
397 |
// only optimise "self-tail calls". This excludes the cases of
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
398 |
// multiple functions making tail calls to each other. Well,
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
399 |
// nothing is perfect.
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
400 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
401 |
|
67
|
402 |
|
|
403 |
|
71
|
404 |
|
67
|
405 |
|