progs/lecture2.scala
changeset 481 e03a0100ec46
parent 471 135bf034ac30
equal deleted inserted replaced
480:a623dd1f2898 481:e03a0100ec46
    31 // Option[...]: None, Some(_)
    31 // Option[...]: None, Some(_)
    32 
    32 
    33 def safe_div(x: Int, y: Int) : Option[Int] = 
    33 def safe_div(x: Int, y: Int) : Option[Int] = 
    34   if (y == 0) None else Some(x / y)
    34   if (y == 0) None else Some(x / y)
    35 
    35 
    36 safe_div(10 + 5, 4 - 1)  
    36 safe_div(10 + 5, 0)  
    37 
    37 
    38 List(1,2,3,4,5,6).indexOf(7)
    38 List(1,2,3,4,5,6).indexOf(7)
    39 List[Int]().min
    39 List[Int]().min
    40 List[Int]().minOption
    40 List[Int](3,4,5).minOption
    41 
    41 
    42 def my_min(ls: List[Int]) : Option[Int] = 
       
    43   ls.minOption
       
    44 
       
    45 my_min(List(1,2,3,4))
       
    46 
    42 
    47 
    43 
    48 // better error handling with Options (no exceptions)
    44 // better error handling with Options (no exceptions)
    49 //
    45 //
    50 //  Try(something).getOrElse(what_to_do_in_case_of_an_exception)
    46 //  Try(something).getOrElse(what_to_do_in_case_of_an_exception)
    51 //
    47 //
    52 
    48 
    53 import scala.util._      // Try,...
    49 import scala.util._      // Try,...
    54 import io.Source         // fromURL
    50 import io.Source         // fromURL
    55 
    51 
    56 val my_url = "https://nms.kcl.ac.uk/christian.urban/"
    52 val my_url = "https://nms.kcl.ac.uk/christian.urban2/"
    57 
    53 
    58 Source.fromURL(my_url)("ISO-8859-1").mkString
    54 Source.fromURL(my_url)("ISO-8859-1").mkString
    59 Source.fromURL(my_url)("ISO-8859-1").getLines().toList
    55 Source.fromURL(my_url)("ISO-8859-1").getLines().toList
    60 
    56 
    61 Try(Source.fromURL(my_url)("ISO-8859-1").mkString).getOrElse("")
    57 Try(Source.fromURL(my_url)("ISO-8859-1").mkString).getOrElse("")
   172 // and produce functions as result
   168 // and produce functions as result
   173 
   169 
   174 def even(x: Int) : Boolean = x % 2 == 0
   170 def even(x: Int) : Boolean = x % 2 == 0
   175 def odd(x: Int) : Boolean = x % 2 == 1
   171 def odd(x: Int) : Boolean = x % 2 == 1
   176 
   172 
       
   173 def inc(x: Int) : Int = x + 1
   177 val lst = (1 to 10).toList
   174 val lst = (1 to 10).toList
   178 
   175 
   179 lst.filter(even)
   176 lst.filter(_ % 2 == 0)
   180 lst.count(odd)
   177 lst.count(odd)
   181 lst.find(even)
   178 lst.find(even)
   182 lst.exists(even)
   179 lst.exists(even)
   183 
   180 
   184 lst.find(_ < 4)
   181 lst.find(_ < 4)
   185 lst.filter(_ < 4) 
   182 lst.filter(_ < 4) 
   186 
   183 
       
   184 val x = 3 < 4
       
   185 
   187 def less4(x: Int) : Boolean = x < 4
   186 def less4(x: Int) : Boolean = x < 4
   188 lst.find(less4)
   187 lst.find(less4)
   189 lst.find(_ < 4)
   188 lst.find(x => !(x < 4))
   190 
   189 
   191 lst.filter(x => x % 2 == 0)
   190 lst.filter(x => x % 2 == 0)
   192 lst.filter(_ % 2 == 0)
   191 lst.filter(_ % 2 == 0)
   193 
   192 
   194 
   193 
   228 lst.map(x => (double(x), square(x)))
   227 lst.map(x => (double(x), square(x)))
   229 
   228 
   230 // works also for strings
   229 // works also for strings
   231 def tweet(c: Char) = c.toUpper
   230 def tweet(c: Char) = c.toUpper
   232 
   231 
   233 "Hello World".map(tweet)
   232 "Hello\nWorld".map(tweet)
   234 
   233 
   235 
   234 
   236 // this can be iterated
   235 // this can be iterated
   237 
   236 
   238 lst.map(square).filter(_ > 4)
   237 lst.map(square).filter(_ > 4)
   349 lst.groupBy(_.length).get(3)
   348 lst.groupBy(_.length).get(3)
   350 
   349 
   351 val grps = lst.groupBy(_.length)
   350 val grps = lst.groupBy(_.length)
   352 grps.keySet
   351 grps.keySet
   353 
   352 
       
   353 // naive quicksort with "On" function
       
   354 
       
   355 def sortOn(f: Int => Int, xs: List[Int]) : List[Int] = {
       
   356   if (xs.size < 2) xs
       
   357   else {
       
   358    val pivot = xs.head
       
   359    val (left, right) = xs.partition(f(_) < f(pivot))
       
   360    sortOn(f, left) ::: pivot :: sortOn(f, right.tail)
       
   361   }
       
   362 } 
       
   363 
       
   364 sortOn(identity, List(99,99,99,98,10,-3,2)) 
       
   365 sortOn(n => - n, List(99,99,99,98,10,-3,2))
   354 
   366 
   355 
   367 
   356 
   368 
   357 // Pattern Matching
   369 // Pattern Matching
   358 //==================
   370 //==================