124   | 
   124   | 
   125 /* in Java  | 
   125 /* in Java  | 
   126 val lyrics = "Sun dips down, the day has gone. \n" +  | 
   126 val lyrics = "Sun dips down, the day has gone. \n" +  | 
   127              "Witches, wolves and giants yawn. \n" +  | 
   127              "Witches, wolves and giants yawn. \n" +  | 
   128              "Queen and dragon, troll and gnome: \n" +  | 
   128              "Queen and dragon, troll and gnome: \n" +  | 
   129              "tired buddies head for home"  | 
   129              "tired buddies head for home."  | 
   130 */   | 
   130 */   | 
   131   | 
   131   | 
   132 val lyrics = """Sun dips down, the day has gone.  | 
   132 val lyrics = """Sun dips down, the day has gone.  | 
   133                 |Witches, wolves and giants yawn.  | 
   133                 |Witches, wolves and giants yawn.  | 
   134                 |Queen and dragon, troll and gnome:  | 
   134                 |Queen and dragon, troll and gnome:  | 
   135                 |tired buddies head for home""".stripMargin  | 
   135                 |tired buddies head for home.""".stripMargin  | 
   136   | 
   136   | 
   137 println(lyrics)  | 
   137 println(lyrics)  | 
   138   | 
   138   | 
   139   | 
   139   | 
   140 // Pairs/Tuples  | 
   140 // Pairs/Tuples  | 
   250   for (n <- (1 to 10).toList;   | 
   253   for (n <- (1 to 10).toList;   | 
   251        m <- (1 to 10).toList) yield m * n  | 
   254        m <- (1 to 10).toList) yield m * n  | 
   252   | 
   255   | 
   253 mult_table.sliding(10,10).mkString("\n") | 
   256 mult_table.sliding(10,10).mkString("\n") | 
   254   | 
   257   | 
         | 
   258 // the list can also be constructed in any other way  | 
         | 
   259 for (n <- List(10,12,4,5,7,8,10)) yield n * n  | 
         | 
   260   | 
   255   | 
   261   | 
   256 // with if-predicates  | 
   262 // with if-predicates  | 
   257   | 
   263   | 
   258 for (n <- (1 to 3).toList;   | 
   264 for (n <- (1 to 3).toList;   | 
   259      m <- (1 to 3).toList;  | 
   265      m <- (1 to 3).toList;  | 
   268 for ((m, n) <- List((1, 4), (2, 3), (3, 2), (4, 1))) yield m + n   | 
   274 for ((m, n) <- List((1, 4), (2, 3), (3, 2), (4, 1))) yield m + n   | 
   269   | 
   275   | 
   270 for (p <- List((1, 4), (2, 3), (3, 2), (4, 1))) yield p._1 + p._2   | 
   276 for (p <- List((1, 4), (2, 3), (3, 2), (4, 1))) yield p._1 + p._2   | 
   271   | 
   277   | 
   272   | 
   278   | 
         | 
   279 // general pattern  | 
         | 
   280   | 
         | 
   281 for (x <- ...) yield { | 
         | 
   282   // potentially complicated  | 
         | 
   283   // calculation of a result  | 
         | 
   284 }  | 
         | 
   285   | 
         | 
   286   | 
   273   | 
   287   | 
   274 // with only a side-effect (no list is produced),  | 
   288 // with only a side-effect (no list is produced),  | 
   275 // has no "yield"  | 
   289 // has no "yield"  | 
   276   | 
   290   | 
   277 for (n <- (1 to 10)) println(n)  | 
   291 for (n <- (1 to 10)) println(n)  | 
   278   | 
         | 
   279   | 
         | 
   280   | 
         | 
   281   | 
   292   | 
   282   | 
   293   | 
   283   | 
   294   | 
   284 // concurrency (ONLY WORKS IN SCALA 2.11.8, not in SCALA 2.12)  | 
   295 // concurrency (ONLY WORKS IN SCALA 2.11.8, not in SCALA 2.12)  | 
   285 //             (would need to have this wrapped into a function, or  | 
   296 //             (would need to have this wrapped into a function, or  | 
   301 time_needed(10, for (n <- list) yield n + 42)  | 
   312 time_needed(10, for (n <- list) yield n + 42)  | 
   302 time_needed(10, for (n <- list.par) yield n + 42)  | 
   313 time_needed(10, for (n <- list.par) yield n + 42)  | 
   303   | 
   314   | 
   304   | 
   315   | 
   305 // mutable vs immutable factorial  | 
   316 // mutable vs immutable factorial  | 
   306 def fact_mu(n: Long): Long = { | 
   317 def fact_mu(n: Long) : Long = { | 
   307   var result : Long = 1  | 
   318   var result : Long = 1  | 
   308   var i = 1  | 
   319   var i = 1  | 
   309   while (i <= n) { | 
   320   while (i <= n) { | 
   310     result = result * i  | 
   321     result = result * i  | 
   311     i = i + 1  | 
   322     i = i + 1  | 
   531   plan.par.map(i(_)).reduce(_ + _)  | 
   542   plan.par.map(i(_)).reduce(_ + _)  | 
   532   | 
   543   | 
   533 santa_state("(((((()))))".toList) | 
   544 santa_state("(((((()))))".toList) | 
   534 santa_imutable("(((((()))))".toList) | 
   545 santa_imutable("(((((()))))".toList) | 
   535   | 
   546   | 
   536 def randomString(length: Int) =   | 
   547 def randomString(length: Int) : String =   | 
   537   List.fill(length)((40 + scala.util.Random.nextInt(2)).toChar)  | 
   548   List.fill(length)((40 + scala.util.Random.nextInt(2)).toChar)  | 
   538   | 
   549   | 
   539   | 
   550   | 
   540 santa_state(randomString(100))  | 
   551 santa_state(randomString(100))  | 
   541   | 
   552   |