|    290  |    293  | 
|    291  |    294  | 
|    292 // For-Comprehensions (not For-Loops) |    295 // For-Comprehensions (not For-Loops) | 
|    293 //==================================== |    296 //==================================== | 
|    294  |    297  | 
|    295 (1 to 10).toList |    298 val lst = (1 to 10).toList | 
|    296 for (n <- (1 to 10).toList) yield {  |    299 for (n <- lst) yield n * n  | 
|    297   square(n) + 1 |    300  | 
|         |    301  | 
|         |    302 for (n <- lst) yield {  | 
|         |    303   square(n) + double(n) | 
|    298 } |    304 } | 
|    299  |    305  | 
|    300 for (n <- (1 to 10).toList;  |    306 for (n <- (1 to 10).toList;  | 
|    301      m <- (1 to 10).toList) yield (m, n) |    307      m <- (1 to 5).toList) yield (n, m, n * m) | 
|    302  |    308  | 
|    303  |    309  | 
|    304 // you can assign the result of a for-comprehension |    310 // you can assign the result of a for-comprehension | 
|    305 // to a value |    311 // to a value | 
|    306 val mult_table =  |    312 val mult_table =  | 
|    307   for (n <- (1 to 10).toList;  |    313   for (n <- (1 to 10).toList;  | 
|    308        m <- (1 to 10).toList) yield m * n |    314        m <- (1 to 10).toList) yield n * m | 
|    309  |    315  | 
|    310 println(mult_table.mkString) |    316 println(mult_table.mkString) | 
|    311 mult_table.sliding(10,10).mkString("\n") |    317 mult_table.sliding(10,10).mkString("\n") | 
|    312  |    318  | 
|    313 // the list/set/... can also be constructed in any  |    319 // for-comprehensions also work for other | 
|    314 // other way |    320 // collections | 
|    315  |    321  | 
|    316 for (n <- Set(10,12,4,5,7,8,10)) yield n * n |    322 for (n <- Set(10,12,4,5,7,8,10)) yield n * n | 
|    317  |    323  | 
|    318 for (n <- (1 to 10)) yield { |    324 for (n <- (1 to 10)) yield { | 
|    319   n * n   |    325   n * n   | 
|    320 } |    326 } | 
|    321  |    327  | 
|         |    328 // with if-predicates / filters | 
|         |    329  | 
|    322 if (1 == 2) "a" else "b" |    330 if (1 == 2) "a" else "b" | 
|    323  |         | 
|    324 // with if-predicates / filters |         | 
|    325  |    331  | 
|    326 for (n <- (1 to 3).toList;  |    332 for (n <- (1 to 3).toList;  | 
|    327      m <- (1 to 3).toList; |    333      m <- (1 to 3).toList; | 
|    328      if (n + m) % 2 == 0) yield (n, m) |    334      if (n + m) % 2 == 0) yield (n, m) | 
|    329  |    335  | 
|    330 for (n <- (1 to 3).toList;  |         | 
|    331      m <- (1 to 3).toList; |         | 
|    332      if ((((n + m) % 2 == 0)))) yield (n, m) |         | 
|    333  |    336  | 
|    334 // with patterns |    337 // with patterns | 
|    335  |    338  | 
|    336 val lst = List((1, 4), (2, 3), (3, 2), (4, 1)) |    339 val lst = List((1, 4), (2, 3), (3, 2), (4, 1)) | 
|    337  |    340  | 
|    341  |    344  | 
|    342  |    345  | 
|    343 // general pattern of for-yield  |    346 // general pattern of for-yield  | 
|    344 // (yield can be several lines) |    347 // (yield can be several lines) | 
|    345  |    348  | 
|    346 for (p <- ...) yield { |    349 for (pat <- ...) yield { | 
|    347   // potentially complicated |    350   // potentially complicated | 
|    348   // calculation of a result |    351   // calculation of a result | 
|    349 } |    352 } | 
|         |    353  | 
|         |    354 // For without yield | 
|         |    355 //=================== | 
|         |    356  | 
|         |    357 // with only a side-effect (no list is produced), | 
|         |    358 // has no "yield" | 
|         |    359  | 
|         |    360 for (n <- (1 to 10).toList) println(n * n) | 
|         |    361  | 
|         |    362 for (n <- (1 to 10).toList) yield n * n | 
|         |    363  | 
|         |    364 // BTW: a roundabout way of printing out a list, say | 
|         |    365 val lst = ('a' to 'm').toList | 
|         |    366  | 
|         |    367 for (i <- (0 until lst.length)) println(lst(i)) | 
|         |    368  | 
|         |    369 // Why not just? Why making your life so complicated? | 
|         |    370 for (c <- lst) println(c) | 
|         |    371  | 
|         |    372  | 
|    350  |    373  | 
|    351 // Functions producing multiple outputs |    374 // Functions producing multiple outputs | 
|    352 //====================================== |    375 //====================================== | 
|    353  |    376  | 
|    354 def get_ascii(c: Char) : (Char, Int) =  |    377 def get_ascii(c: Char) : (Char, Int) =  | 
|    369  |    392  | 
|    370 strs.maxBy(_._2) |    393 strs.maxBy(_._2) | 
|    371 strs.maxBy(_._1) |    394 strs.maxBy(_._1) | 
|    372  |    395  | 
|    373  |    396  | 
|    374 // For without yield |         | 
|    375 //=================== |         | 
|    376  |         | 
|    377 // with only a side-effect (no list is produced), |         | 
|    378 // has no "yield" |         | 
|    379  |         | 
|    380 for (n <- (1 to 10)) println(n) |         | 
|    381  |         | 
|    382 (1 to 10).toList |         | 
|    383 (1 until 10).toList |         | 
|    384 // BTW: a roundabout way of printing out a list, say |         | 
|    385 val lst = ('a' to 'm').toList |         | 
|    386  |         | 
|    387 for (i <- (0 until lst.length)) println(lst(i)) |         | 
|    388  |         | 
|    389 // Why not just? Why making your life so complicated? |         | 
|    390 for (c <- lst) println(c) |         | 
|    391  |    397  | 
|    392  |    398  | 
|    393  |    399  | 
|    394 // Aside: concurrency  |    400 // Aside: concurrency  | 
|    395 // scala -Yrepl-class-based -cp scala-parallel-collections_2.13-0.2.0.jar  |    401 // scala -Yrepl-class-based -cp scala-parallel-collections_2.13-0.2.0.jar  |