| author | Christian Urban <christian.urban@kcl.ac.uk> | 
| Fri, 26 Apr 2024 17:37:56 +0100 | |
| changeset 485 | 02d2d49c9feb | 
| parent 478 | 0e6ca70496c1 | 
| child 490 | f70d74fea67f | 
| permissions | -rw-r--r-- | 
| 67 | 1  | 
// Scala Lecture 3  | 
2  | 
//=================  | 
|
3  | 
||
| 478 | 4  | 
// last week:  | 
5  | 
// higher-order functions  | 
|
6  | 
// maps  | 
|
| 446 | 7  | 
|
| 478 | 8  | 
// - recursion  | 
9  | 
// - Sudoku  | 
|
10  | 
// - string interpolations  | 
|
| 445 | 11  | 
// - Pattern-Matching  | 
12  | 
||
| 478 | 13  | 
// A Recursive Web Crawler / Email Harvester  | 
14  | 
//===========================================  | 
|
15  | 
//  | 
|
16  | 
// the idea is to look for links using the  | 
|
17  | 
// regular expression "https?://[^"]*" and for  | 
|
18  | 
// email addresses using another regex.  | 
|
19  | 
||
20  | 
import io.Source  | 
|
21  | 
import scala.util._  | 
|
22  | 
||
23  | 
// gets the first 10K of a web-page  | 
|
24  | 
def get_page(url: String) : String = {
 | 
|
25  | 
  Try(Source.fromURL(url)("ISO-8859-1").take(10000).mkString).
 | 
|
26  | 
    getOrElse { println(s"  Problem with: $url"); ""}
 | 
|
27  | 
}  | 
|
28  | 
||
29  | 
// regex for URLs and emails  | 
|
30  | 
val http_pattern = """"https?://[^"]*"""".r  | 
|
31  | 
val email_pattern = """([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})""".r
 | 
|
32  | 
||
33  | 
//test case:  | 
|
34  | 
//email_pattern.findAllIn  | 
|
35  | 
//  ("foo bla christian@kcl.ac.uk 1234567").toList
 | 
|
36  | 
||
37  | 
||
38  | 
// drops the first and last character from a string  | 
|
39  | 
def unquote(s: String) = s.drop(1).dropRight(1)  | 
|
40  | 
||
41  | 
def get_all_URLs(page: String): Set[String] =  | 
|
42  | 
http_pattern.findAllIn(page).map(unquote).toSet  | 
|
43  | 
||
44  | 
// naive version of crawl - searches until a given depth,  | 
|
45  | 
// visits pages potentially more than once  | 
|
46  | 
def crawl(url: String, n: Int) : Unit = {
 | 
|
47  | 
if (n == 0) ()  | 
|
48  | 
  else {
 | 
|
49  | 
println(s" Visiting: $n $url")  | 
|
50  | 
for (u <- get_all_URLs(get_page(url))) crawl(u, n - 1)  | 
|
51  | 
}  | 
|
52  | 
}  | 
|
53  | 
||
54  | 
// some starting URLs for the crawler  | 
|
55  | 
val startURL = """https://nms.kcl.ac.uk/christian.urban/"""  | 
|
56  | 
||
57  | 
crawl(startURL, 2)  | 
|
58  | 
||
59  | 
||
60  | 
// a primitive email harvester  | 
|
61  | 
def emails(url: String, n: Int) : Set[String] = {
 | 
|
62  | 
if (n == 0) Set()  | 
|
63  | 
  else {
 | 
|
64  | 
println(s" Visiting: $n $url")  | 
|
65  | 
val page = get_page(url)  | 
|
66  | 
val new_emails = email_pattern.findAllIn(page).toSet  | 
|
67  | 
new_emails ++ (for (u <- get_all_URLs(page)) yield emails(u, n - 1)).flatten  | 
|
68  | 
}  | 
|
69  | 
}  | 
|
70  | 
||
71  | 
emails(startURL, 2)  | 
|
72  | 
||
73  | 
||
74  | 
||
75  | 
// Sudoku  | 
|
76  | 
//========  | 
|
77  | 
||
78  | 
// THE POINT OF THIS CODE IS NOT TO BE SUPER  | 
|
79  | 
// EFFICIENT AND FAST, just explaining exhaustive  | 
|
80  | 
// depth-first search  | 
|
81  | 
||
82  | 
||
83  | 
val game0 = """.14.6.3..  | 
|
84  | 
|62...4..9  | 
|
85  | 
|.8..5.6..  | 
|
86  | 
|.6.2....3  | 
|
87  | 
|.7..1..5.  | 
|
88  | 
|5....9.6.  | 
|
89  | 
|..6.2..3.  | 
|
90  | 
|1..5...92  | 
|
91  | 
              |..7.9.41.""".stripMargin.replaceAll("\\n", "")
 | 
|
92  | 
||
93  | 
type Pos = (Int, Int)  | 
|
94  | 
val EmptyValue = '.'  | 
|
95  | 
val MaxValue = 9  | 
|
96  | 
||
97  | 
def pretty(game: String): String =  | 
|
98  | 
  "\n" + (game.grouped(MaxValue).mkString("\n"))
 | 
|
99  | 
||
100  | 
pretty(game0)  | 
|
101  | 
||
102  | 
||
103  | 
val allValues = "123456789".toList  | 
|
104  | 
val indexes = (0 to 8).toList  | 
|
105  | 
||
106  | 
def empty(game: String) = game.indexOf(EmptyValue)  | 
|
107  | 
def isDone(game: String) = empty(game) == -1  | 
|
108  | 
def emptyPosition(game: String) : Pos = {
 | 
|
109  | 
val e = empty(game)  | 
|
110  | 
(e % MaxValue, e / MaxValue)  | 
|
111  | 
}  | 
|
112  | 
||
113  | 
def get_row(game: String, y: Int) =  | 
|
114  | 
indexes.map(col => game(y * MaxValue + col))  | 
|
115  | 
def get_col(game: String, x: Int) =  | 
|
116  | 
indexes.map(row => game(x + row * MaxValue))  | 
|
117  | 
||
118  | 
//get_row(game0, 0)  | 
|
119  | 
//get_row(game0, 1)  | 
|
120  | 
//get_col(game0, 0)  | 
|
121  | 
||
122  | 
def get_box(game: String, pos: Pos): List[Char] = {
 | 
|
123  | 
def base(p: Int): Int = (p / 3) * 3  | 
|
124  | 
val x0 = base(pos._1)  | 
|
125  | 
val y0 = base(pos._2)  | 
|
126  | 
val ys = (y0 until y0 + 3).toList  | 
|
127  | 
(x0 until x0 + 3).toList  | 
|
128  | 
.flatMap(x => ys.map(y => game(x + y * MaxValue)))  | 
|
| 446 | 129  | 
}  | 
130  | 
||
131  | 
||
| 478 | 132  | 
//get_box(game0, (3, 1))  | 
133  | 
||
134  | 
||
135  | 
// this is not mutable!!  | 
|
136  | 
def update(game: String, pos: Int, value: Char): String =  | 
|
137  | 
game.updated(pos, value)  | 
|
138  | 
||
139  | 
def toAvoid(game: String, pos: Pos): List[Char] =  | 
|
140  | 
(get_col(game, pos._1) ++  | 
|
141  | 
get_row(game, pos._2) ++  | 
|
142  | 
get_box(game, pos))  | 
|
| 445 | 143  | 
|
| 478 | 144  | 
def candidates(game: String, pos: Pos): List[Char] =  | 
145  | 
allValues.diff(toAvoid(game, pos))  | 
|
146  | 
||
147  | 
//candidates(game0, (0,0))  | 
|
148  | 
||
149  | 
||
150  | 
def search(game: String): List[String] = {
 | 
|
151  | 
if (isDone(game)) List(game)  | 
|
152  | 
  else {
 | 
|
153  | 
val cs = candidates(game, emptyPosition(game))  | 
|
154  | 
cs.par.map(c => search(update(game, empty(game), c))).flatten.toList  | 
|
155  | 
}  | 
|
| 445 | 156  | 
}  | 
157  | 
||
| 478 | 158  | 
pretty(game0)  | 
159  | 
search(game0).map(pretty)  | 
|
| 445 | 160  | 
|
| 478 | 161  | 
val game1 = """23.915...  | 
162  | 
|...2..54.  | 
|
163  | 
|6.7......  | 
|
164  | 
|..1.....9  | 
|
165  | 
|89.5.3.17  | 
|
166  | 
|5.....6..  | 
|
167  | 
|......9.5  | 
|
168  | 
|.16..7...  | 
|
169  | 
              |...329..1""".stripMargin.replaceAll("\\n", "")
 | 
|
170  | 
||
171  | 
search(game1).map(pretty)  | 
|
172  | 
||
173  | 
// a game that is in the hard category  | 
|
174  | 
val game2 = """8........  | 
|
175  | 
|..36.....  | 
|
176  | 
|.7..9.2..  | 
|
177  | 
|.5...7...  | 
|
178  | 
|....457..  | 
|
179  | 
|...1...3.  | 
|
180  | 
|..1....68  | 
|
181  | 
|..85...1.  | 
|
182  | 
              |.9....4..""".stripMargin.replaceAll("\\n", "")
 | 
|
183  | 
||
184  | 
search(game2).map(pretty)  | 
|
185  | 
||
186  | 
// game with multiple solutions  | 
|
187  | 
val game3 = """.8...9743  | 
|
188  | 
|.5...8.1.  | 
|
189  | 
|.1.......  | 
|
190  | 
|8....5...  | 
|
191  | 
|...8.4...  | 
|
192  | 
|...3....6  | 
|
193  | 
|.......7.  | 
|
194  | 
|.3.5...8.  | 
|
195  | 
              |9724...5.""".stripMargin.replaceAll("\\n", "")
 | 
|
196  | 
||
197  | 
search(game3).map(pretty).foreach(println)  | 
|
198  | 
||
199  | 
// for measuring time  | 
|
200  | 
def time_needed[T](i: Int, code: => T) = {
 | 
|
201  | 
val start = System.nanoTime()  | 
|
202  | 
for (j <- 1 to i) code  | 
|
203  | 
val end = System.nanoTime()  | 
|
204  | 
  s"${(end - start) / 1.0e9} secs"
 | 
|
205  | 
}  | 
|
206  | 
||
207  | 
time_needed(2, search(game2))  | 
|
208  | 
||
209  | 
||
210  | 
// concurrency  | 
|
211  | 
// scala-cli --extra-jars scala-parallel-collections_3-1.0.4.jar  | 
|
212  | 
// import scala.collection.parallel.CollectionConverters._  | 
|
213  | 
||
214  | 
||
215  | 
||
| 445 | 216  | 
|
217  | 
// String Interpolations  | 
|
218  | 
//=======================  | 
|
219  | 
||
220  | 
def cube(n: Int) : Int = n * n * n  | 
|
221  | 
||
222  | 
val n = 3  | 
|
223  | 
println("The cube of " + n + " is " + cube(n) + ".")
 | 
|
224  | 
||
225  | 
println(s"The cube of $n is ${cube(n)}.")
 | 
|
226  | 
||
227  | 
// or even  | 
|
228  | 
||
229  | 
println(s"The cube of $n is ${n * n * n}.")
 | 
|
230  | 
||
231  | 
// helpful for debugging purposes  | 
|
232  | 
//  | 
|
233  | 
// "The most effective debugging tool is still careful  | 
|
234  | 
// thought, coupled with judiciously placed print  | 
|
235  | 
// statements."  | 
|
236  | 
// — Brian W. Kernighan, in Unix for Beginners (1979)  | 
|
237  | 
||
238  | 
||
239  | 
def gcd_db(a: Int, b: Int) : Int = {
 | 
|
240  | 
println(s"Function called with $a and $b.")  | 
|
241  | 
if (b == 0) a else gcd_db(b, a % b)  | 
|
242  | 
}  | 
|
243  | 
||
244  | 
gcd_db(48, 18)  | 
|
| 320 | 245  | 
|
| 415 | 246  | 
|
| 343 | 247  | 
|
248  | 
||
| 320 | 249  | 
// Recursion Again ;o)  | 
250  | 
//====================  | 
|
251  | 
||
| 217 | 252  | 
|
| 366 | 253  | 
// another well-known example: Towers of Hanoi  | 
254  | 
//=============================================  | 
|
| 178 | 255  | 
|
| 320 | 256  | 
def move(from: Char, to: Char) =  | 
257  | 
println(s"Move disc from $from to $to!")  | 
|
| 67 | 258  | 
|
| 320 | 259  | 
def hanoi(n: Int, from: Char, via: Char, to: Char) : Unit = {
 | 
260  | 
if (n == 0) ()  | 
|
261  | 
  else {
 | 
|
262  | 
hanoi(n - 1, from, to, via)  | 
|
263  | 
move(from, to)  | 
|
264  | 
hanoi(n - 1, via, from, to)  | 
|
265  | 
}  | 
|
266  | 
}  | 
|
| 67 | 267  | 
|
| 320 | 268  | 
hanoi(4, 'A', 'B', 'C')  | 
| 67 | 269  | 
|
| 155 | 270  | 
|
271  | 
||
| 478 | 272  | 
// Pattern Matching  | 
273  | 
//==================  | 
|
274  | 
||
275  | 
// A powerful tool which has even landed in Java during  | 
|
276  | 
// the last few years (https://inside.java/2021/06/13/podcast-017/).  | 
|
277  | 
// ...Scala already has it for many years and the concept is  | 
|
278  | 
// older than your friendly lecturer, that is stone old ;o)  | 
|
279  | 
||
280  | 
// The general schema:  | 
|
281  | 
//  | 
|
282  | 
//    expression match {
 | 
|
283  | 
// case pattern1 => expression1  | 
|
284  | 
// case pattern2 => expression2  | 
|
285  | 
// ...  | 
|
286  | 
// case patternN => expressionN  | 
|
287  | 
// }  | 
|
288  | 
||
289  | 
||
290  | 
// recall  | 
|
291  | 
def len(xs: List[Int]) : Int = {
 | 
|
292  | 
if (xs == Nil) 0  | 
|
293  | 
else 1 + len(xs.tail)  | 
|
294  | 
}  | 
|
295  | 
||
296  | 
def len(xs: List[Int]) : Int = xs match {
 | 
|
297  | 
case Nil => 0  | 
|
298  | 
case hd::tail => 1 + len(tail)  | 
|
299  | 
}  | 
|
300  | 
||
301  | 
||
302  | 
def my_map_int(lst: List[Int], f: Int => Int) : List[Int] =  | 
|
303  | 
  lst match {
 | 
|
304  | 
case Nil => Nil  | 
|
305  | 
case x::xs => f(x)::my_map_int(xs, f)  | 
|
306  | 
}  | 
|
307  | 
||
308  | 
def my_map_option(opt: Option[Int], f: Int => Int) : Option[Int] =  | 
|
309  | 
  opt match {
 | 
|
310  | 
case None => None  | 
|
311  | 
case Some(x) => Some(f(x))  | 
|
312  | 
}  | 
|
313  | 
||
314  | 
my_map_option(None, x => x * x)  | 
|
315  | 
my_map_option(Some(8), x => x * x)  | 
|
316  | 
||
317  | 
||
318  | 
// you can also have cases combined  | 
|
319  | 
def season(month: String) : String = month match {
 | 
|
320  | 
case "March" | "April" | "May" => "It's spring"  | 
|
321  | 
case "June" | "July" | "August" => "It's summer"  | 
|
322  | 
case "September" | "October" | "November" => "It's autumn"  | 
|
323  | 
case "December" => "It's winter"  | 
|
324  | 
case "January" | "February" => "It's unfortunately winter"  | 
|
325  | 
case _ => "Wrong month"  | 
|
326  | 
}  | 
|
327  | 
||
328  | 
// pattern-match on integers  | 
|
329  | 
||
330  | 
def fib(n: Int) : Int = n match { 
 | 
|
331  | 
case 0 | 1 => 1  | 
|
332  | 
case n => fib(n - 1) + fib(n - 2)  | 
|
333  | 
}  | 
|
334  | 
||
335  | 
fib(10)  | 
|
336  | 
||
337  | 
// pattern-match on results  | 
|
338  | 
||
339  | 
// Silly: fizz buzz  | 
|
340  | 
def fizz_buzz(n: Int) : String = (n % 3, n % 5) match {
 | 
|
341  | 
case (0, 0) => "fizz buzz"  | 
|
342  | 
case (0, _) => "fizz"  | 
|
343  | 
case (_, 0) => "buzz"  | 
|
344  | 
case _ => n.toString  | 
|
345  | 
}  | 
|
346  | 
||
347  | 
for (n <- 1 to 20)  | 
|
348  | 
println(fizz_buzz(n))  | 
|
349  | 
||
350  | 
// guards in pattern-matching  | 
|
351  | 
||
352  | 
def foo(xs: List[Int]) : String = xs match {
 | 
|
353  | 
case Nil => s"this list is empty"  | 
|
354  | 
case x :: xs if x % 2 == 0  | 
|
355  | 
=> s"the first elemnt is even"  | 
|
356  | 
case x :: y :: rest if x == y  | 
|
357  | 
=> s"this has two elemnts that are the same"  | 
|
358  | 
case hd :: tl => s"this list is standard $hd::$tl"  | 
|
359  | 
}  | 
|
360  | 
||
361  | 
foo(Nil)  | 
|
362  | 
foo(List(1,2,3))  | 
|
363  | 
foo(List(1,2))  | 
|
364  | 
foo(List(1,1,2,3))  | 
|
365  | 
foo(List(2,2,2,3))  | 
|
366  | 
||
367  | 
||
368  | 
// Trees  | 
|
| 320 | 369  | 
|
| 323 | 370  | 
abstract class Tree  | 
371  | 
case class Leaf(x: Int) extends Tree  | 
|
372  | 
case class Node(s: String, left: Tree, right: Tree) extends Tree  | 
|
373  | 
||
| 366 | 374  | 
val lf = Leaf(20)  | 
375  | 
val tr = Node("foo", Leaf(10), Leaf(23))
 | 
|
| 320 | 376  | 
|
| 366 | 377  | 
val lst : List[Tree] = List(lf, tr)  | 
378  | 
||
379  | 
||
380  | 
abstract class Colour  | 
|
| 320 | 381  | 
case object Red extends Colour  | 
382  | 
case object Green extends Colour  | 
|
383  | 
case object Blue extends Colour  | 
|
| 323 | 384  | 
case object Yellow extends Colour  | 
| 320 | 385  | 
|
386  | 
||
387  | 
def fav_colour(c: Colour) : Boolean = c match {
 | 
|
388  | 
case Green => true  | 
|
| 323 | 389  | 
case _ => false  | 
| 320 | 390  | 
}  | 
391  | 
||
| 366 | 392  | 
fav_colour(Blue)  | 
393  | 
||
| 320 | 394  | 
|
395  | 
// ... a tiny bit more useful: Roman Numerals  | 
|
396  | 
||
| 321 | 397  | 
sealed abstract class RomanDigit  | 
| 320 | 398  | 
case object I extends RomanDigit  | 
399  | 
case object V extends RomanDigit  | 
|
400  | 
case object X extends RomanDigit  | 
|
401  | 
case object L extends RomanDigit  | 
|
402  | 
case object C extends RomanDigit  | 
|
403  | 
case object D extends RomanDigit  | 
|
404  | 
case object M extends RomanDigit  | 
|
405  | 
||
406  | 
type RomanNumeral = List[RomanDigit]  | 
|
407  | 
||
| 366 | 408  | 
List(X,I,M,A)  | 
| 320 | 409  | 
|
410  | 
/*  | 
|
411  | 
I -> 1  | 
|
412  | 
II -> 2  | 
|
413  | 
III -> 3  | 
|
414  | 
IV -> 4  | 
|
415  | 
V -> 5  | 
|
416  | 
VI -> 6  | 
|
417  | 
VII -> 7  | 
|
418  | 
VIII -> 8  | 
|
419  | 
IX -> 9  | 
|
420  | 
X -> 10  | 
|
421  | 
*/  | 
|
422  | 
||
423  | 
def RomanNumeral2Int(rs: RomanNumeral): Int = rs match { 
 | 
|
424  | 
case Nil => 0  | 
|
425  | 
case M::r => 1000 + RomanNumeral2Int(r)  | 
|
426  | 
case C::M::r => 900 + RomanNumeral2Int(r)  | 
|
427  | 
case D::r => 500 + RomanNumeral2Int(r)  | 
|
428  | 
case C::D::r => 400 + RomanNumeral2Int(r)  | 
|
429  | 
case C::r => 100 + RomanNumeral2Int(r)  | 
|
430  | 
case X::C::r => 90 + RomanNumeral2Int(r)  | 
|
431  | 
case L::r => 50 + RomanNumeral2Int(r)  | 
|
432  | 
case X::L::r => 40 + RomanNumeral2Int(r)  | 
|
433  | 
case X::r => 10 + RomanNumeral2Int(r)  | 
|
434  | 
case I::X::r => 9 + RomanNumeral2Int(r)  | 
|
435  | 
case V::r => 5 + RomanNumeral2Int(r)  | 
|
436  | 
case I::V::r => 4 + RomanNumeral2Int(r)  | 
|
437  | 
case I::r => 1 + RomanNumeral2Int(r)  | 
|
438  | 
}  | 
|
439  | 
||
440  | 
RomanNumeral2Int(List(I,V)) // 4  | 
|
441  | 
RomanNumeral2Int(List(I,I,I,I)) // 4 (invalid Roman number)  | 
|
442  | 
RomanNumeral2Int(List(V,I)) // 6  | 
|
443  | 
RomanNumeral2Int(List(I,X)) // 9  | 
|
444  | 
RomanNumeral2Int(List(M,C,M,L,X,X,I,X)) // 1979  | 
|
445  | 
RomanNumeral2Int(List(M,M,X,V,I,I)) // 2017  | 
|
446  | 
||
447  | 
||
| 478 | 448  | 
abstract class Rexp  | 
449  | 
case object ZERO extends Rexp // matches nothing  | 
|
450  | 
case object ONE extends Rexp // matches the empty string  | 
|
451  | 
case class CHAR(c: Char) extends Rexp // matches a character c  | 
|
452  | 
case class ALT(r1: Rexp, r2: Rexp) extends Rexp // alternative  | 
|
453  | 
case class SEQ(r1: Rexp, r2: Rexp) extends Rexp // sequence  | 
|
454  | 
case class STAR(r: Rexp) extends Rexp // star  | 
|
455  | 
||
456  | 
def depth(r: Rexp) : Int = r match {
 | 
|
457  | 
case ZERO => 1  | 
|
458  | 
case ONE => 1  | 
|
459  | 
case CHAR(_) => 1  | 
|
460  | 
case ALT(r1, r2) => 1 + List(depth(r1), depth(r2)).max  | 
|
461  | 
case SEQ(r1, r2) => 1 + List(depth(r1), depth(r2)).max  | 
|
462  | 
case STAR(r1) => 1 + depth(r1)  | 
|
463  | 
}  | 
|
464  | 
||
465  | 
||
466  | 
||
467  | 
||
468  | 
||
| 366 | 469  | 
// expressions (essentially trees)  | 
470  | 
||
471  | 
abstract class Exp  | 
|
472  | 
case class N(n: Int) extends Exp // for numbers  | 
|
473  | 
case class Plus(e1: Exp, e2: Exp) extends Exp  | 
|
474  | 
case class Times(e1: Exp, e2: Exp) extends Exp  | 
|
475  | 
||
476  | 
def string(e: Exp) : String = e match {
 | 
|
477  | 
case N(n) => s"$n"  | 
|
478  | 
  case Plus(e1, e2) => s"(${string(e1)} + ${string(e2)})" 
 | 
|
479  | 
  case Times(e1, e2) => s"(${string(e1)} * ${string(e2)})"
 | 
|
480  | 
}  | 
|
481  | 
||
482  | 
val e = Plus(N(9), Times(N(3), N(4)))  | 
|
483  | 
e.toString  | 
|
484  | 
println(string(e))  | 
|
485  | 
||
486  | 
def eval(e: Exp) : Int = e match {
 | 
|
487  | 
case N(n) => n  | 
|
488  | 
case Plus(e1, e2) => eval(e1) + eval(e2)  | 
|
489  | 
case Times(e1, e2) => eval(e1) * eval(e2)  | 
|
490  | 
}  | 
|
491  | 
||
492  | 
println(eval(e))  | 
|
493  | 
||
494  | 
// simplification rules:  | 
|
495  | 
// e + 0, 0 + e => e  | 
|
496  | 
// e * 0, 0 * e => 0  | 
|
497  | 
// e * 1, 1 * e => e  | 
|
498  | 
//  | 
|
499  | 
// (....9 ....)  | 
|
500  | 
||
501  | 
def simp(e: Exp) : Exp = e match {
 | 
|
502  | 
case N(n) => N(n)  | 
|
503  | 
  case Plus(e1, e2) => (simp(e1), simp(e2)) match {
 | 
|
504  | 
case (N(0), e2s) => e2s  | 
|
505  | 
case (e1s, N(0)) => e1s  | 
|
506  | 
case (e1s, e2s) => Plus(e1s, e2s)  | 
|
507  | 
}  | 
|
508  | 
  case Times(e1, e2) => (simp(e1), simp(e2)) match {
 | 
|
509  | 
case (N(0), _) => N(0)  | 
|
510  | 
case (_, N(0)) => N(0)  | 
|
511  | 
case (N(1), e2s) => e2s  | 
|
512  | 
case (e1s, N(1)) => e1s  | 
|
513  | 
case (e1s, e2s) => Times(e1s, e2s)  | 
|
514  | 
}  | 
|
515  | 
}  | 
|
516  | 
||
517  | 
||
518  | 
val e2 = Times(Plus(N(0), N(1)), Plus(N(0), N(9)))  | 
|
519  | 
println(string(e2))  | 
|
520  | 
println(string(simp(e2)))  | 
|
521  | 
||
522  | 
||
523  | 
||
| 320 | 524  | 
// String interpolations as patterns  | 
525  | 
||
526  | 
val date = "2019-11-26"  | 
|
527  | 
val s"$year-$month-$day" = date  | 
|
528  | 
||
529  | 
def parse_date(date: String) : Option[(Int, Int, Int)]= date match {
 | 
|
530  | 
case s"$year-$month-$day" => Some((day.toInt, month.toInt, year.toInt))  | 
|
531  | 
case s"$day/$month/$year" => Some((day.toInt, month.toInt, year.toInt))  | 
|
532  | 
case s"$day.$month.$year" => Some((day.toInt, month.toInt, year.toInt))  | 
|
533  | 
case _ => None  | 
|
534  | 
}  | 
|
| 318 | 535  | 
|
| 320 | 536  | 
parse_date("2019-11-26")
 | 
537  | 
parse_date("26/11/2019")
 | 
|
538  | 
parse_date("26.11.2019")
 | 
|
539  | 
||
540  | 
||
| 478 | 541  | 
|
542  | 
||
543  | 
// Map type (upper-case)  | 
|
544  | 
//=======================  | 
|
545  | 
||
546  | 
// Note the difference between map and Map  | 
|
547  | 
||
548  | 
val m = Map(1 -> "one", 2 -> "two", 10 -> "many")  | 
|
549  | 
||
550  | 
List((1, "one"), (2, "two"), (10, "many")).toMap  | 
|
551  | 
||
552  | 
m.get(1)  | 
|
553  | 
m.get(4)  | 
|
554  | 
||
555  | 
m.getOrElse(1, "")  | 
|
556  | 
m.getOrElse(4, "")  | 
|
557  | 
||
558  | 
val new_m = m + (10 -> "ten")  | 
|
| 320 | 559  | 
|
| 478 | 560  | 
new_m.get(10)  | 
561  | 
||
562  | 
val m2 = for ((k, v) <- m) yield (k, v.toUpperCase)  | 
|
563  | 
||
564  | 
||
565  | 
||
566  | 
// groupBy function on Maps  | 
|
567  | 
val lst = List("one", "two", "three", "four", "five")
 | 
|
568  | 
lst.groupBy(_.head)  | 
|
| 320 | 569  | 
|
| 478 | 570  | 
lst.groupBy(_.length)  | 
571  | 
||
572  | 
lst.groupBy(_.length).get(3)  | 
|
573  | 
||
574  | 
val grps = lst.groupBy(_.length)  | 
|
575  | 
grps.keySet  | 
|
576  | 
||
577  | 
||
578  | 
||
| 320 | 579  | 
|
580  | 
// Tail recursion  | 
|
581  | 
//================  | 
|
582  | 
||
| 375 | 583  | 
def fact(n: BigInt): BigInt =  | 
| 320 | 584  | 
if (n == 0) 1 else n * fact(n - 1)  | 
585  | 
||
586  | 
fact(10) //ok  | 
|
587  | 
fact(10000) // produces a stackoverflow  | 
|
588  | 
||
| 375 | 589  | 
|
| 320 | 590  | 
def factT(n: BigInt, acc: BigInt): BigInt =  | 
591  | 
if (n == 0) acc else factT(n - 1, n * acc)  | 
|
592  | 
||
593  | 
factT(10, 1)  | 
|
594  | 
println(factT(100000, 1))  | 
|
595  | 
||
596  | 
// there is a flag for ensuring a function is tail recursive  | 
|
597  | 
import scala.annotation.tailrec  | 
|
598  | 
||
599  | 
@tailrec  | 
|
600  | 
def factT(n: BigInt, acc: BigInt): BigInt =  | 
|
601  | 
if (n == 0) acc else factT(n - 1, n * acc)  | 
|
602  | 
||
603  | 
||
604  | 
||
605  | 
// for tail-recursive functions the Scala compiler  | 
|
606  | 
// generates loop-like code, which does not need  | 
|
607  | 
// to allocate stack-space in each recursive  | 
|
608  | 
// call; Scala can do this only for tail-recursive  | 
|
609  | 
// functions  | 
|
610  | 
||
| 375 | 611  | 
def length(xs: List[Int]) : Int = xs match {
 | 
612  | 
case Nil => 0  | 
|
613  | 
case _ :: tail => 1 + length(tail)  | 
|
614  | 
}  | 
|
| 366 | 615  | 
|
| 375 | 616  | 
@tailrec  | 
617  | 
def lengthT(xs: List[Int], acc : Int) : Int = xs match {
 | 
|
618  | 
case Nil => acc  | 
|
619  | 
case _ :: tail => lengthT(tail, 1 + acc)  | 
|
620  | 
}  | 
|
621  | 
||
622  | 
lengthT(List.fill(10000000)(1), 0)  | 
|
| 366 | 623  | 
|
624  | 
||
625  | 
||
626  | 
||
| 478 | 627  | 
|
| 366 | 628  | 
|
629  | 
||
| 478 | 630  | 
// Aside: concurrency  | 
631  | 
// scala-cli --extra-jars scala-parallel-collections_3-1.0.4.jar  | 
|
| 366 | 632  | 
|
| 478 | 633  | 
for (n <- (1 to 10)) println(n)  | 
634  | 
||
635  | 
import scala.collection.parallel.CollectionConverters._  | 
|
636  | 
||
637  | 
for (n <- (1 to 10).par) println(n)  | 
|
| 366 | 638  | 
|
639  | 
||
| 478 | 640  | 
// for measuring time  | 
641  | 
def time_needed[T](n: Int, code: => T) = {
 | 
|
642  | 
val start = System.nanoTime()  | 
|
643  | 
for (i <- (0 to n)) code  | 
|
644  | 
val end = System.nanoTime()  | 
|
645  | 
(end - start) / 1.0e9  | 
|
| 366 | 646  | 
}  | 
647  | 
||
| 478 | 648  | 
val list = (1L to 10_000_000L).toList  | 
649  | 
time_needed(10, for (n <- list) yield n + 42)  | 
|
650  | 
time_needed(10, for (n <- list.par) yield n + 42)  | 
|
| 366 | 651  | 
|
| 478 | 652  | 
// ...but par does not make everything faster  | 
| 158 | 653  | 
|
| 478 | 654  | 
list.sum  | 
655  | 
list.par.sum  | 
|
| 67 | 656  | 
|
| 478 | 657  | 
time_needed(10, list.sum)  | 
658  | 
time_needed(10, list.par.sum)  | 
|
| 158 | 659  | 
|
660  | 
||
| 478 | 661  | 
// Mutable vs Immutable  | 
662  | 
//======================  | 
|
663  | 
//  | 
|
664  | 
// Remember:  | 
|
665  | 
// - no vars, no ++i, no +=  | 
|
666  | 
// - no mutable data-structures (no Arrays, no ListBuffers)  | 
|
| 158 | 667  | 
|
| 478 | 668  | 
// But what the heck....lets try to count to 1 Mio in parallel  | 
669  | 
//  | 
|
670  | 
// requires  | 
|
671  | 
// scala-cli --extra-jars scala- parallel-collections_3-1.0.4.jar  | 
|
672  | 
||
673  | 
import scala.collection.parallel.CollectionConverters._  | 
|
674  | 
||
675  | 
def test() = {
 | 
|
676  | 
var cnt = 0  | 
|
677  | 
||
678  | 
for(i <- (1 to 100_000).par) cnt += 1  | 
|
679  | 
||
680  | 
println(s"Should be 100000: $cnt")  | 
|
| 67 | 681  | 
}  | 
682  | 
||
| 478 | 683  | 
test()  | 
684  | 
||
685  | 
// Or  | 
|
686  | 
// Q: Count how many elements are in the intersections of  | 
|
687  | 
// two sets?  | 
|
688  | 
// A; IMPROPER WAY (mutable counter)  | 
|
689  | 
||
690  | 
def count_intersection(A: Set[Int], B: Set[Int]) : Int = {
 | 
|
691  | 
var count = 0  | 
|
692  | 
for (x <- A.par; if B contains x) count += 1  | 
|
693  | 
count  | 
|
694  | 
}  | 
|
695  | 
||
696  | 
val A = (0 to 999).toSet  | 
|
697  | 
val B = (0 to 999 by 4).toSet  | 
|
698  | 
||
699  | 
count_intersection(A, B)  | 
|
700  | 
||
701  | 
// but do not try to add .par to the for-loop above  | 
|
| 217 | 702  | 
|
| 158 | 703  | 
|
| 478 | 704  | 
//propper parallel version  | 
705  | 
def count_intersection2(A: Set[Int], B: Set[Int]) : Int =  | 
|
706  | 
A.par.count(x => B contains x)  | 
|
| 155 | 707  | 
|
| 478 | 708  | 
count_intersection2(A, B)  | 
| 67 | 709  | 
|
| 
77
 
3cbe3d90b77f
updated
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
73 
diff
changeset
 | 
710  |