progs/lecture1.scala
changeset 504 7653dd662db3
parent 497 ef37fb04a343
equal deleted inserted replaced
503:e0ee3aa6334b 504:7653dd662db3
     1 // Scala Lecture 1
     1 // Scala Lecture 1
     2 //=================
     2 //=================
     3 
     3 
       
     4 print("Hello")
     4 
     5 
     5 // Topics
     6 // Topics
     6 //--------
     7 //--------
     7 // - List, Sets, Ints, Strings, ... 
     8 // - List, Sets, Ints, Strings, ... 
     8 // - Value assignments (val vs var)
     9 // - Value assignments (val vs var)
    67 List(1, 2, 3) ::: List(3, 6, 5)
    68 List(1, 2, 3) ::: List(3, 6, 5)
    68 Set(1, 2, 3) ++ Set(3, 6, 5)
    69 Set(1, 2, 3) ++ Set(3, 6, 5)
    69 
    70 
    70 // ranges
    71 // ranges
    71 1 to 10
    72 1 to 10
    72 (1 to 10).toList
    73 (1 to 100).toString
    73 (1 to 10).toList.toString
    74 (1 to 10).toList.toString
    74 
    75 
    75 
    76 
    76 
    77 
    77 // Equality in Scala is structural
    78 // Equality in Scala is structural
   181 val name = "bob"
   182 val name = "bob"
   182 
   183 
   183 val name : Int = "bob"
   184 val name : Int = "bob"
   184 
   185 
   185 // type errors
   186 // type errors
   186 math.sqrt("64")
   187 math.sqrt(64)
   187 
   188 
   188 // produces
   189 // produces
   189 //
   190 //
   190 // Type Mismatch Error:
   191 // Type Mismatch Error:
   191 //   Found   : ("64" : String)
   192 //   Found   : ("64" : String)
   215 //
   216 //
   216 //  def fname(arg1: ty1, arg2: ty2,..., argn: tyn): rty = {
   217 //  def fname(arg1: ty1, arg2: ty2,..., argn: tyn): rty = {
   217 //     ....
   218 //     ....
   218 //  }
   219 //  }
   219 
   220 
       
   221 def incr(x: Int) : Int = x + 1
       
   222 incr(42)
       
   223 
       
   224 def add(x: Int, y: Int) : Int = x + y
       
   225 
       
   226 
   220 def average(ls: List[Int]) : Int = {
   227 def average(ls: List[Int]) : Int = {
   221   println(s"$ls")
       
   222   val s = ls.sum
   228   val s = ls.sum
   223   val l = ls.length
   229   val l = ls.length
   224   if (l == 0) 0 
   230   s / l
   225   else s / l
   231 }
   226 }
   232 
   227 
   233 average(List(1,2,3,4))
   228 average(List(1,2,3,4,56))
   234 
   229 
   235 
   230 
   236 
   231 def incr(x: Int) : Int = x + 1
       
   232 def double(x: Int) : Int = x + x
   237 def double(x: Int) : Int = x + x
   233 def square(x: Int) : Int = x * x
   238 def square(x: Int) : Int = x * x
   234 
   239 
   235 def str(x: Int) : String = x.toString
   240 def str(x: Int) : String = x.toString
   236 
   241 
   318 
   323 
   319 
   324 
   320 // For-Comprehensions (not For-Loops)
   325 // For-Comprehensions (not For-Loops)
   321 //====================================
   326 //====================================
   322 
   327 
   323 val lst = (1 to 10).toSet
   328 val lst = (1 to 10).toList
   324 val result = for (n <- lst) yield {
   329 for (n <- lst) yield {
   325   n * n 
   330   n * n 
   326 }
   331 }
   327 
   332 
   328 println(result)
   333 println(result)
       
   334 
       
   335 def square(n: Int) = n * n
       
   336 def double(n: Int) = n + n
   329 
   337 
   330 for (n <- lst) yield { 
   338 for (n <- lst) yield { 
   331   square(n) + double(n)
   339   square(n) + double(n)
   332 }
   340 }
   333 
   341 
   334 for (n <- (1 to 10).toList; 
   342 val foo = for (n <- (1 to 10).toList; 
   335      m <- (1 to 5).toList) yield (n, m)
   343      m <- (1 to 5).toList;
   336 
   344      if (n + m) % 2 == 0) yield (n, m)
       
   345 
       
   346 foo.head
   337 
   347 
   338 // you can assign the result of a for-comprehension
   348 // you can assign the result of a for-comprehension
   339 // to a value
   349 // to a value
   340 val mult_table = 
   350 val mult_table = 
   341   for (n <- (1 to 10).toList; 
   351   for (n <- (1 to 10).toList; 
   374 
   384 
   375 
   385 
   376 // general pattern of for-yield 
   386 // general pattern of for-yield 
   377 // (yield can be several lines)
   387 // (yield can be several lines)
   378 
   388 
       
   389 /*
   379 for (pat <- ...) yield {
   390 for (pat <- ...) yield {
   380   // potentially complicated
   391   // potentially complicated
   381   // calculation of a result
   392   // calculation of a result
   382 }
   393 }
       
   394 */
   383 
   395 
   384 // For without yield
   396 // For without yield
   385 //===================
   397 //===================
   386 
   398 
   387 // with only a side-effect (no list is produced),
   399 // with only a side-effect (no list is produced),
   388 // has no "yield"
   400 // has no "yield"
       
   401 
       
   402 for (n <- (1 to 10).toList) yield println(n * n)
   389 
   403 
   390 val xs = for (n <- (1 to 10).toList) println(n * n)
   404 val xs = for (n <- (1 to 10).toList) println(n * n)
   391 
   405 
   392 xs.tail  // error
   406 xs.tail  // error
   393 
   407 
   476 // - no mutable data-structures (no Arrays, no ListBuffers)
   490 // - no mutable data-structures (no Arrays, no ListBuffers)
   477 
   491 
   478 // But what the heck....lets try to count to 1 Mio in parallel
   492 // But what the heck....lets try to count to 1 Mio in parallel
   479 // 
   493 // 
   480 // requires
   494 // requires
   481 // scala --extra-jars scala- parallel-collections_3-1.0.4.jar
   495 // scala --extra-jars scala-parallel-collections_3-1.2.0.jar
   482 
   496 
   483 import scala.collection.parallel.CollectionConverters._
   497 import scala.collection.parallel.CollectionConverters._
       
   498 
       
   499 for (n <- (1 to 10).par) println(n)
   484 
   500 
   485 def test() = {
   501 def test() = {
   486   var cnt = 0
   502   var cnt = 0
   487 
   503 
   488   for(i <- (1 to 100_000).par) cnt += 1
   504   for(i <- (1 to 100_000).par) cnt += 1
   550 gcd_db(48, 18)
   566 gcd_db(48, 18)
   551 
   567 
   552 // you can also implement your own string interpolations
   568 // you can also implement your own string interpolations
   553 
   569 
   554 extension (sc: StringContext) {
   570 extension (sc: StringContext) {
   555     def i(args: Any*): String = s"\t${sc.s(args:_*)}\n"
   571     def i(args: Any*): String = s"\t${sc.s(args*)}\n"
   556     def l(args: Any*): String = s"${sc.s(args:_*)}:\n"
   572     def l(args: Any*): String = s"${sc.s(args*)}:\n"
   557 }
   573 }
   558 
   574 
   559 // this allows you to write things like
   575 // this allows you to write things like
   560 
   576 
   561 i"add ${3+2}" 
   577 i"add ${3+2}"