progs/lecture1.scala
changeset 497 ef37fb04a343
parent 491 2a30c7dfe3ed
equal deleted inserted replaced
496:b28e976dcd65 497:ef37fb04a343
     1 // Scala Lecture 1
     1 // Scala Lecture 1
     2 //=================
     2 //=================
     3 
     3 
     4 println("Hello World")
     4 
     5 
     5 // Topics
     6 
     6 //--------
     7 // - List, Sets, Ints, Strings, ... 
     7 // - List, Sets, Ints, Strings, ... 
     8 // - Value assignments (val vs var)
     8 // - Value assignments (val vs var)
     9 // - How to define functions? (What is returned?)
     9 // - How to define functions? (What is returned?)
    10 // - If-Conditions
    10 // - If-Conditions
    11 // - For-Comprehensions (guards, with/without yield)
    11 // - For-Comprehensions (guards, with/without yield)
    12 // - String-Interpolations
    12 // - String-Interpolations
    13 //
    13 //
    14 //
       
    15 
    14 
    16 
    15 
    17 
    16 
    18 // Value assignments
    17 // Value assignments
    19 // (their names should be lower case)
    18 // (their names should be lower case)
    42 val s1 = Set(1,2,3,1)
    41 val s1 = Set(1,2,3,1)
    43 
    42 
    44 // picking an element in a list
    43 // picking an element in a list
    45 val lst = List(1, 2, 3, 1)
    44 val lst = List(1, 2, 3, 1)
    46 
    45 
    47 val lst = List(1, 2, 3, 1)
       
    48 
    46 
    49 val lst1 = 0 :: lst
    47 val lst1 = 0 :: lst
    50 
    48 
    51 println(lst)
    49 println(lst)
    52 println(lst1)
    50 println(lst1)
    72 // ranges
    70 // ranges
    73 1 to 10
    71 1 to 10
    74 (1 to 10).toList
    72 (1 to 10).toList
    75 (1 to 10).toList.toString
    73 (1 to 10).toList.toString
    76 
    74 
    77 (1 until 10).toList
       
    78 
    75 
    79 
    76 
    80 // Equality in Scala is structural
    77 // Equality in Scala is structural
    81 //=================================
    78 //=================================
    82 val a = "Dave2"
    79 val a = "Dave2"
   227   if (l == 0) 0 
   224   if (l == 0) 0 
   228   else s / l
   225   else s / l
   229 }
   226 }
   230 
   227 
   231 average(List(1,2,3,4,56))
   228 average(List(1,2,3,4,56))
       
   229 
       
   230 
   232 def incr(x: Int) : Int = x + 1
   231 def incr(x: Int) : Int = x + 1
   233 def double(x: Int) : Int = x + x
   232 def double(x: Int) : Int = x + x
   234 def square(x: Int) : Int = x * x
   233 def square(x: Int) : Int = x * x
   235 
   234 
   236 def str(x: Int) : String = x.toString
   235 def str(x: Int) : String = x.toString
   250 
   249 
   251 len(List(1,2,3,4,1))
   250 len(List(1,2,3,4,1))
   252 
   251 
   253 
   252 
   254 
   253 
   255 
       
   256 def len(xs: List[Int]) : Int = xs match {
       
   257    case Nil => 0
       
   258    case x :: xs => 1 + len(xs)
       
   259 }
       
   260 
       
   261 len(List(1,2,3,4,1))
       
   262 
       
   263 
       
   264 
       
   265 // If-Conditionals
   254 // If-Conditionals
   266 //=================
   255 //=================
   267 
   256 
   268 // - Scala used to not have a then-keyword
   257 // - Scala used to *not* have a then-keyword
   269 // - !!both if-else branches need to be present!!
   258 // - !!both if-else branches need to be present!!
   270 
   259 
   271 def fact(n: Int) : Int = 
   260 def fact(n: Int) : Int = 
   272   if (n == 0) 1 else n * fact(n - 1)
   261   if (n == 0) 1 else n * fact(n - 1)
       
   262 
       
   263 fact(5)
   273 
   264 
   274 
   265 
   275 // Scala 3 introduced if-then-else - maybe people 
   266 // Scala 3 introduced if-then-else - maybe people 
   276 // desperately needed it 
   267 // desperately needed it 
   277 def fact(n: Int) : Int = 
   268 def fact(n: Int) : Int = 
   278   if n == 0 then 1 else n * fact(n - 1)
   269   if n == 0 then 1 else n * fact(n - 1)
   279 
   270 
   280 fact(5)
   271 
   281 fact(150)
   272 fact(150)
   282 
   273 
   283 /* boolean operators
   274 /* boolean operators
   284  
   275  
   285    ==     equals
   276    ==     equals
   349 val mult_table = 
   340 val mult_table = 
   350   for (n <- (1 to 10).toList; 
   341   for (n <- (1 to 10).toList; 
   351        m <- (1 to 10).toList) yield n * m
   342        m <- (1 to 10).toList) yield n * m
   352 
   343 
   353 println(mult_table.mkString(","))
   344 println(mult_table.mkString(","))
   354 mult_table.sliding(10,10).toList.mkString(",")
   345 mult_table.sliding(10,10).toList.mkString("\n")
   355 
       
   356 
       
   357 .mkString("\n")
       
   358 
   346 
   359 // for-comprehensions also work for other
   347 // for-comprehensions also work for other
   360 // collections
   348 // collections
   361 
   349 
   362 for (n <- Set(10,12,4,5,7,8,10)) yield n * n
   350 for (n <- Set(10,12,4,5,7,8,10)) yield n * n
   363 
   351 
   364 for (n <- (1 to 10)) yield {
   352 for (n <- (1 to 10)) yield {
   365 
       
   366   n * n  
   353   n * n  
   367 }
   354 }
   368 
   355 
   369 // with if-predicates / filters
   356 // with if-predicates / filters
   370 
   357 
   379 
   366 
   380 // with patterns
   367 // with patterns
   381 
   368 
   382 val lst = List((1, 4), (2, 3), (3, 2), (4, 1))
   369 val lst = List((1, 4), (2, 3), (3, 2), (4, 1))
   383 
   370 
   384 ` yield m + n 
       
   385 
       
   386 for (p <- lst) yield p._1 + p._2 
   371 for (p <- lst) yield p._1 + p._2 
       
   372 
       
   373 for ((m, n) <- lst) yield m + n 
   387 
   374 
   388 
   375 
   389 // general pattern of for-yield 
   376 // general pattern of for-yield 
   390 // (yield can be several lines)
   377 // (yield can be several lines)
   391 
   378 
   398 //===================
   385 //===================
   399 
   386 
   400 // with only a side-effect (no list is produced),
   387 // with only a side-effect (no list is produced),
   401 // has no "yield"
   388 // has no "yield"
   402 
   389 
   403 val xs = for (n <- (1 to 10).toList) yield println(n * n)
   390 val xs = for (n <- (1 to 10).toList) println(n * n)
   404 
   391 
   405 xs.tail
   392 xs.tail  // error
   406 
   393 
   407 val foo = for (n <- (1 to 10).toList) yield n * n
   394 val foo = for (n <- (1 to 10).toList) yield n * n
   408 
   395 
   409 println(for (n <- (1 to 10).toList)  print(n))
   396 println(for (n <- (1 to 10).toList)  print(n))
   410 
   397 
   449 
   436 
   450 
   437 
   451 
   438 
   452 
   439 
   453 // Aside: concurrency 
   440 // Aside: concurrency 
   454 // scala-cli --extra-jars scala-parallel-collections_3-1.0.4.jar 
   441 // scala --extra-jars scala-parallel-collections_3-1.2.0.jar 
   455 
   442 
   456 for (n <- (1 to 10)) println(n)
   443 for (n <- (1 to 10)) println(n)
   457 
   444 
   458 import scala.collection.parallel.CollectionConverters._
   445 import scala.collection.parallel.CollectionConverters._
   459 
   446 
   496 import scala.collection.parallel.CollectionConverters._
   483 import scala.collection.parallel.CollectionConverters._
   497 
   484 
   498 def test() = {
   485 def test() = {
   499   var cnt = 0
   486   var cnt = 0
   500 
   487 
   501   for(i <- (1 to 100_000)) cnt += 1
   488   for(i <- (1 to 100_000).par) cnt += 1
   502 
   489 
   503   println(s"Should be 100000: $cnt")
   490   println(s"Should be 100_000: $cnt")
   504 }
   491 }
   505 
   492 
   506 test()
   493 test()
   507 
   494 
   508 // Or
   495 // Or