444
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
1 |
|
447
|
2 |
// Task 1
|
444
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
3 |
|
447
|
4 |
List(7,2,3,4,5,6).find(_ < 4) // => Some(3)
|
|
5 |
List(5,6,7,8,9).find(_ < 4) // => None
|
|
6 |
List(5,6,7,8,9).min // => 5
|
|
7 |
List(5,6,7,8,9).minOption // => Some(5)
|
|
8 |
List[Int]().minOption // => None
|
444
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
9 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
10 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
11 |
// Task 2
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
12 |
|
447
|
13 |
Try(Some(List(5,6,7,8,9).min)).getOrElse(None)
|
|
14 |
Try(Some(List[Int]().min)).getOrElse(None)
|
444
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
15 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
16 |
// Task 3
|
447
|
17 |
import scala.util._
|
|
18 |
import io.Source
|
444
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
19 |
|
447
|
20 |
val my_url = "https://nms.kcl.ac.uk/christian.urban/"
|
444
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
21 |
|
447
|
22 |
// fails if there is no file with that name
|
|
23 |
Source.fromFile("test.txt")("ISO-8859-1").mkString
|
|
24 |
Source.fromFile("test.txt")("ISO-8859-1").getLines().toList
|
444
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
25 |
|
447
|
26 |
// encapsulates the failure case as None
|
|
27 |
Try(Some(Source.fromFile("test.txt")("ISO-8859-1").mkString)).getOrElse(None)
|
|
28 |
Try(Source.fromFile("test.txt")("ISO-8859-1").mkString).toOption // same but shorter
|
444
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
29 |
|
447
|
30 |
// for files with propper closing of the file after reading
|
|
31 |
Using(Source.fromFile("test.txt")("ISO-8859-1"))(_.mkString).toOption
|
444
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
32 |
|
447
|
33 |
// Task 4 (Higher-Order Functions)
|
444
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
34 |
|
447
|
35 |
List(7,2,3,4,5,6).find(_ < 4)
|
|
36 |
List(7,2,3,4,5,6).count(_ % 2 == 0)
|
|
37 |
List(7,2,3,4,5,6).sortWith(_ > _)
|
|
38 |
List(7,2,3,4,5,6).filter(_ > 4)
|
444
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
39 |
|
447
|
40 |
// Task 5 (Maps)
|
444
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
41 |
|
447
|
42 |
List(7,2,3,4,5,6).map(n => n * n)
|
444
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
43 |
|
447
|
44 |
for (n <- List(7,2,3,4,5,6)) yield n * n
|
444
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
45 |
|
447
|
46 |
// The advantages of for-comprehensions is that they
|
|
47 |
// can be nested and also can contain guards. In such
|
|
48 |
// cases the translations to maps and filters is a bit
|
|
49 |
// involved.
|
444
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
50 |
|
447
|
51 |
// Task 6 (Pattern-Matching)
|
444
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
52 |
|
447
|
53 |
def my_map(lst: List[Int], f: Int => Int) : List[Int] = {
|
|
54 |
if (lst == Nil) Nil
|
|
55 |
else f(lst.head) :: my_map(lst.tail, f)
|
444
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
56 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
57 |
|
447
|
58 |
def my_map(lst: List[Int], f: Int => Int) : List[Int] = lst macth {
|
|
59 |
case Nil => Nil
|
|
60 |
case x::xs => f(x) :: my_map(xs, f)
|
444
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
61 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
62 |
|
447
|
63 |
// Task 7 (Web-Crawler, hard)
|
|
64 |
|
|
65 |
// see lecture2.scala
|
|
66 |
|
|
67 |
// requires an accumulator that records all pages that have
|
|
68 |
// already been visited, for example
|
444
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
69 |
|
447
|
70 |
def crawl(url: String, n: Int, acc : Set[String] = Set()) : Unit = {
|
|
71 |
if (n == 0) ()
|
|
72 |
else {
|
|
73 |
println(s" Visiting: $n $url")
|
|
74 |
val urls = get_all_URLs(get_page(url))
|
|
75 |
for (u <- urls) crawl(u, n - 1, acc | urls)
|
|
76 |
}
|
|
77 |
}
|