progs/lecture2.scala
changeset 363 e5c1d69cffa4
parent 362 1bde878ba6c9
child 364 f1a6fa599d26
equal deleted inserted replaced
362:1bde878ba6c9 363:e5c1d69cffa4
     1 // Scala Lecture 2
     1 // Scala Lecture 2
     2 //=================
     2 //=================
     3 
     3  
     4 
     4 
     5 // String Interpolations
     5 // String Interpolations
     6 //=======================
     6 //=======================
     7 
     7 
     8 
     8 
   270 
   270 
   271 // this can be iterated
   271 // this can be iterated
   272 
   272 
   273 lst.map(square).filter(_ > 4)
   273 lst.map(square).filter(_ > 4)
   274 
   274 
   275 (lst.map(square)
   275 lst.map(square).find(_ > 4)
       
   276 lst.map(square).find(_ > 4).map(double)
       
   277 
       
   278 lst.map(square)
   276    .find(_ > 4)
   279    .find(_ > 4)
   277    .map(square))
   280    .map(double)
   278 
   281 
   279 lst.map(square).find(_ > 4)
   282 
       
   283 // Option Type and maps
       
   284 //======================
       
   285 
       
   286 // a function that turns strings into numbers (similar to .toInt)
       
   287 Integer.parseInt("12u34")
       
   288 
       
   289 // maps on Options
       
   290 
       
   291 import scala.util._
       
   292 
       
   293 def get_me_an_int(s: String) : Option[Int] = 
       
   294  Try(Some(Integer.parseInt(s))).getOrElse(None)
       
   295 
       
   296 get_me_an_int("12345").map(_ % 2 == 0)
       
   297 get_me_an_int("12u34").map(_ % 2 == 0)
       
   298 
       
   299 
       
   300 
       
   301 val lst = List("12345", "foo", "5432", "bar", "x21", "456")
       
   302 for (x <- lst) yield get_me_an_int(x)
       
   303 
       
   304 // summing up all the numbers
       
   305 
       
   306 lst.map(get_me_an_int).flatten.sum
       
   307 
       
   308 
       
   309 
   280 
   310 
   281 // this is actually how for-comprehensions are
   311 // this is actually how for-comprehensions are
   282 // defined in Scala
   312 // defined in Scala
   283 
   313 
   284 lst.map(n => square(n))
   314 lst.map(n => square(n))
   286 
   316 
   287 // lets define our own higher-order functions
   317 // lets define our own higher-order functions
   288 // type of functions is for example Int => Int
   318 // type of functions is for example Int => Int
   289 
   319 
   290 
   320 
   291 def my_map_int(lst: List[Int], f: Int => Int) : List[Int] = {
   321 def my_map_int(lst: List[Int], f: Int => Int) : List[Int] = 
       
   322 {
   292   if (lst == Nil) Nil
   323   if (lst == Nil) Nil
   293   else f(lst.head) :: my_map_int(lst.tail, f)
   324   else f(lst.head) :: my_map_int(lst.tail, f)
   294 }
   325 }
   295 
   326 
   296 my_map_int(lst, square)
   327 my_map_int(lst, square)
   297 
       
   298 
   328 
   299 // same function using pattern matching: a kind
   329 // same function using pattern matching: a kind
   300 // of switch statement on steroids (see more later on)
   330 // of switch statement on steroids (see more later on)
   301 
   331 
   302 def my_map_int(lst: List[Int], f: Int => Int) : List[Int] = 
   332 def my_map_int(lst: List[Int], f: Int => Int) : List[Int] = 
   304     case Nil => Nil
   334     case Nil => Nil
   305     case x::xs => f(x)::my_map_int(xs, f)
   335     case x::xs => f(x)::my_map_int(xs, f)
   306   }
   336   }
   307 
   337 
   308 
   338 
       
   339 
       
   340 val biglst = (1 to 10000).toList
       
   341 my_map_int(biglst, double)
       
   342 
       
   343 (1 to 10000000).toList.map(double)
       
   344 
   309 // other function types
   345 // other function types
   310 //
   346 //
   311 // f1: (Int, Int) => Int
   347 // f1: (Int, Int) => Int
   312 // f2: List[String] => Option[Int]
   348 // f2: List[String] => Option[Int]
   313 // ... 
   349 // ... 
   314 val lst = (1 to 10).toList
   350 
   315 
   351 
   316 lst.sum
       
   317 
       
   318 val lst = List(1,2,3,4)
       
   319 
       
   320 lst.head
       
   321 lst.tail
       
   322 
       
   323 def sumOf(f: Int => Int, lst: List[Int]): Int = 
       
   324 lst match {
       
   325   case Nil => 0
       
   326   case x::foo => f(x) + sumOf(f, foo)
       
   327 }
       
   328 
       
   329 def sum_squares(lst: List[Int]) = sumOf(square, lst)
       
   330 def sum_cubes(lst: List[Int])   = sumOf(x => x * x * x, lst)
       
   331 
       
   332 sum_squares(lst)
       
   333 sum_cubes(lst)
       
   334 
       
   335 // lets try a factorial
       
   336 def fact(n: Int) : Int = 
       
   337   if (n == 0) 1 else n * fact(n - 1)
       
   338 
       
   339 def sum_fact(lst: List[Int]) = sumOf(fact, lst)
       
   340 sum_fact(lst)
       
   341 
       
   342 
       
   343 
       
   344 // sometimes it is needed that you specify the type. 
       
   345 (1 to 100).filter((x: Int) => x % 2 == 0).sum 
       
   346 
       
   347 // in this case it is clear that x must be an Int
       
   348 (1 to 100).filter(x => x % 2 == 0).sum
       
   349 
       
   350 // When each parameter (only x in this case) is used only once
       
   351 // you can use the wizardy placeholder syntax
       
   352 (1 to 100).filter(_ % 2 == 0).sum
       
   353 
       
   354 
       
   355 
       
   356 // Option Type and maps
       
   357 //======================
       
   358 
       
   359 // a function that turns strings into numbers (similar to .toInt)
       
   360 Integer.parseInt("12u34")
       
   361 
       
   362 import scala.util._
       
   363 
       
   364 def get_me_an_int(s: String) : Option[Int] = 
       
   365  Try(Some(Integer.parseInt(s))).getOrElse(None)
       
   366 
       
   367 val lst = List("12345", "foo", "5432", "bar", "x21", "456")
       
   368 for (x <- lst) yield get_me_an_int(x)
       
   369 
       
   370 // summing up all the numbers
       
   371 
       
   372 lst.map(get_me_an_int).flatten.sum
       
   373 lst.map(get_me_an_int).flatten.sum
       
   374 
       
   375 lst.flatMap(get_me_an_int).sum
       
   376 
       
   377 // maps on Options
       
   378 
       
   379 get_me_an_int("12345").map(even)
       
   380 get_me_an_int("12u34").map(even)
       
   381 
       
   382 def my_map_option(o: Option[Int], f : Int => Int) : Option[Int] = {
       
   383 o match {
       
   384    case None => None
       
   385    case Some(foo) => Some(f(foo))
       
   386 }}
       
   387 
       
   388 my_map_option(Some(4), square)
       
   389 my_map_option(None, square)
       
   390 
   352 
   391 
   353 
   392 
   354 
   393 // Map type (upper-case)
   355 // Map type (upper-case)
   394 //=======================
   356 //=======================