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