1 // Scala Lecture 1 |
1 // Scala Lecture 1 |
2 //================= |
2 //================= |
|
3 |
|
4 println("Hello World") |
|
5 |
3 |
6 |
4 // - List, Sets, Ints, Strings, ... |
7 // - List, Sets, Ints, Strings, ... |
5 // - Value assignments (val vs var) |
8 // - Value assignments (val vs var) |
6 // - How to define functions? (What is returned?) |
9 // - How to define functions? (What is returned?) |
7 // - If-Conditions |
10 // - If-Conditions |
9 // - String-Interpolations |
12 // - String-Interpolations |
10 // |
13 // |
11 // |
14 // |
12 |
15 |
13 |
16 |
|
17 |
14 // Value assignments |
18 // Value assignments |
15 // (their names should be lower case) |
19 // (their names should be lower case) |
16 //==================================== |
20 //==================================== |
17 |
21 |
18 |
22 |
19 val x = 42 |
23 val x = 42 |
20 val y = 3 + 4 |
24 val y = 3 + 4 |
21 val z = x / y |
25 val z = x / y |
22 val x = 0 |
26 val x = 17 |
23 println(z) |
27 println(z) |
24 |
28 |
25 |
29 |
26 // (you cannot reassign values: z = 9 will give an error) |
30 // (you cannot reassign values: z = 9 will give an error) |
27 var z = 9 |
|
28 z = 10 |
|
29 |
31 |
30 |
32 |
31 |
33 |
32 |
34 |
33 // Collections |
35 // Collections |
34 //============= |
36 //============= |
35 |
37 |
36 List(1,2,3,1) |
38 val ls = List("1","2","3","1") |
37 Set(1,2,3,1) |
39 |
|
40 val ls2 = ls ::: ls |
|
41 |
|
42 val s1 = Set(1,2,3,1) |
38 |
43 |
39 // picking an element in a list |
44 // picking an element in a list |
40 val lst = List(1, 2, 3, 1) |
45 val lst = List(1, 2, 3, 1) |
41 |
46 |
|
47 val lst = List(1, 2, 3, 1) |
|
48 |
|
49 val lst1 = 0 :: lst |
|
50 |
|
51 println(lst) |
|
52 println(lst1) |
42 |
53 |
43 lst(0) |
54 lst(0) |
44 lst(2) |
55 lst(2) |
45 |
56 |
46 // head and tail |
57 // head and tail |
53 |
64 |
54 1 :: 2 :: 3 :: Nil |
65 1 :: 2 :: 3 :: Nil |
55 List(1, 2, 3) ::: List(4, 5, 6) |
66 List(1, 2, 3) ::: List(4, 5, 6) |
56 |
67 |
57 // also |
68 // also |
58 List(1, 2, 3) ++ List(3, 6, 5) |
69 List(1, 2, 3) ::: List(3, 6, 5) |
59 Set(1, 2, 3) ++ Set(3, 6, 5) |
70 Set(1, 2, 3) ++ Set(3, 6, 5) |
60 |
71 |
61 // ranges |
72 // ranges |
62 1 to 10 |
73 1 to 10 |
63 (1 to 10).toList |
74 (1 to 10).toList |
200 |
211 |
201 |
212 |
202 // Function Definitions |
213 // Function Definitions |
203 //====================== |
214 //====================== |
204 |
215 |
205 |
216 // The general scheme for a function: you have to give a |
|
217 // type to each argument and a return type of the function |
|
218 // |
|
219 // def fname(arg1: ty1, arg2: ty2,..., argn: tyn): rty = { |
|
220 // .... |
|
221 // } |
|
222 |
|
223 def average(ls: List[Int]) : Int = { |
|
224 println(s"$ls") |
|
225 val s = ls.sum |
|
226 val l = ls.length |
|
227 if (l == 0) 0 |
|
228 else s / l |
|
229 } |
|
230 |
|
231 average(List(1,2,3,4,56)) |
206 def incr(x: Int) : Int = x + 1 |
232 def incr(x: Int) : Int = x + 1 |
207 def double(x: Int) : Int = x + x |
233 def double(x: Int) : Int = x + x |
208 def square(x: Int) : Int = x * x |
234 def square(x: Int) : Int = x * x |
209 |
235 |
210 def str(x: Int) : String = x.toString |
236 def str(x: Int) : String = x.toString |
211 |
|
212 |
237 |
213 incr(3) |
238 incr(3) |
214 double(4) |
239 double(4) |
215 square(6) |
240 square(6) |
216 str(3) |
241 str(3) |
217 |
242 |
218 |
243 |
219 // The general scheme for a function: you have to give a |
244 |
220 // type to each argument and a return type of the function |
245 |
221 // |
246 def len(xs: List[Int]) : Int = { |
222 // def fname(arg1: ty1, arg2: ty2,..., argn: tyn): rty = { |
247 if (xs == Nil) 0 |
223 // .... |
248 else 1 + len(xs.tail) |
224 // } |
249 } |
225 |
|
226 |
|
227 // > LENGTH OF LIST EXAMPLE |
|
228 def len(xs: List[Int], acc: Int) : Int = { |
|
229 if (xs == Nil) acc |
|
230 else foo(xs.tail, acc + 1) |
|
231 } |
|
232 |
|
233 def len(xs: List[Int]) : Int = foo(xs, 0) |
|
234 |
250 |
235 len(List(1,2,3,4,1)) |
251 len(List(1,2,3,4,1)) |
236 |
252 |
237 |
|
238 def len(xs: List[Int]) : Int = { |
|
239 if (xs == Nil) 0 |
|
240 else (1 + len(xs.tail)) |
|
241 } |
|
242 |
|
243 |
|
244 |
|
245 len(List(1,2,3,4,1)) |
|
246 |
253 |
247 |
254 |
248 |
255 |
249 def len(xs: List[Int]) : Int = xs match { |
256 def len(xs: List[Int]) : Int = xs match { |
250 case Nil => 0 |
257 case Nil => 0 |
320 |
327 |
321 |
328 |
322 // For-Comprehensions (not For-Loops) |
329 // For-Comprehensions (not For-Loops) |
323 //==================================== |
330 //==================================== |
324 |
331 |
325 val lst = (1 to 10).toList |
332 val lst = (1 to 10).toSet |
326 for (n <- lst) yield n * n |
333 val result = for (n <- lst) yield { |
327 |
334 n * n |
|
335 } |
|
336 |
|
337 println(result) |
328 |
338 |
329 for (n <- lst) yield { |
339 for (n <- lst) yield { |
330 square(n) + double(n) |
340 square(n) + double(n) |
331 } |
341 } |
332 |
342 |
339 val mult_table = |
349 val mult_table = |
340 for (n <- (1 to 10).toList; |
350 for (n <- (1 to 10).toList; |
341 m <- (1 to 10).toList) yield n * m |
351 m <- (1 to 10).toList) yield n * m |
342 |
352 |
343 println(mult_table.mkString(",")) |
353 println(mult_table.mkString(",")) |
344 mult_table.sliding(10,10).toList |
354 mult_table.sliding(10,10).toList.mkString(",") |
345 |
355 |
346 |
356 |
347 .mkString("\n") |
357 .mkString("\n") |
348 |
358 |
349 // for-comprehensions also work for other |
359 // for-comprehensions also work for other |
359 // with if-predicates / filters |
369 // with if-predicates / filters |
360 |
370 |
361 val xs = for (n <- (1 to 3).toList; |
371 val xs = for (n <- (1 to 3).toList; |
362 m <- (1 to 3).toList) yield (n,m) |
372 m <- (1 to 3).toList) yield (n,m) |
363 |
373 |
364 xs.filter{case (m, n) => (n + m) % 2 == 0} |
|
365 |
374 |
366 for (n <- (1 to 3).toList; |
375 for (n <- (1 to 3).toList; |
367 m <- (1 to 3).toList; |
376 m <- (1 to 3).toList; |
368 if (n + m) % 2 == 0) yield (n, m) |
377 if (n + m) % 2 == 0) yield (n, m) |
369 |
378 |
395 |
404 |
396 xs.tail |
405 xs.tail |
397 |
406 |
398 val foo = for (n <- (1 to 10).toList) yield n * n |
407 val foo = for (n <- (1 to 10).toList) yield n * n |
399 |
408 |
|
409 println(for (n <- (1 to 10).toList) print(n)) |
400 |
410 |
401 // BTW: a roundabout way of printing out a list, say |
411 // BTW: a roundabout way of printing out a list, say |
402 val lst = ('a' to 'm').toList |
412 val lst = ('a' to 'm').toList |
403 |
413 |
404 for (i <- (0 until lst.length)) println(lst(i)) |
414 for (i <- (0 until lst.length)) println(lst(i)) |
420 |
430 |
421 // .maxBy, sortBy with pairs |
431 // .maxBy, sortBy with pairs |
422 def get_length(s: String) : (String, Int) = |
432 def get_length(s: String) : (String, Int) = |
423 (s, s.length) |
433 (s, s.length) |
424 |
434 |
|
435 def len(s: String) : Int = s.length |
|
436 |
|
437 |
425 val lst = List("zero", "one", "two", "three", "four", "ten") |
438 val lst = List("zero", "one", "two", "three", "four", "ten") |
|
439 lst.sorted |
|
440 lst.sortBy(len(_)) |
426 val strs = for (s <- lst) yield get_length(s) |
441 val strs = for (s <- lst) yield get_length(s) |
427 |
442 |
428 strs.sortBy(_._2) |
443 strs.sortBy(_._2) |
429 strs.sortBy(_._1) |
444 strs.sortBy(_._1) |
430 |
445 |
474 // - no mutable data-structures (no Arrays, no ListBuffers) |
489 // - no mutable data-structures (no Arrays, no ListBuffers) |
475 |
490 |
476 // But what the heck....lets try to count to 1 Mio in parallel |
491 // But what the heck....lets try to count to 1 Mio in parallel |
477 // |
492 // |
478 // requires |
493 // requires |
479 // scala-cli --extra-jars scala- parallel-collections_3-1.0.4.jar |
494 // scala --extra-jars scala- parallel-collections_3-1.0.4.jar |
480 |
495 |
481 import scala.collection.parallel.CollectionConverters._ |
496 import scala.collection.parallel.CollectionConverters._ |
482 |
497 |
483 def test() = { |
498 def test() = { |
484 var cnt = 0 |
499 var cnt = 0 |
560 l"some_fresh_name" |
575 l"some_fresh_name" |
561 |
576 |
562 // Further Information |
577 // Further Information |
563 //===================== |
578 //===================== |
564 |
579 |
565 // We are going to use Scala 3 and the scala-cli repl (easier to use) |
580 // We are going to use Scala 3. The Scala homepage |
566 // |
581 // and general information is at |
567 // https://scala-cli.virtuslab.org |
|
568 // |
|
569 // |
|
570 // The Scala homepage and general information is at |
|
571 // |
582 // |
572 // http://www.scala-lang.org |
583 // http://www.scala-lang.org |
573 // http://docs.scala-lang.org |
584 // http://docs.scala-lang.org |
574 // |
585 // |
575 // |
586 // |
576 // It should be fairly easy to install the scala-cli binary and |
587 // It should be fairly easy to install the scala binary and |
577 // run Scala on the commandline. People also use Scala with |
588 // run Scala on the commandline. People also use Scala with |
578 // Vim and Jedit. I currently settled on Codium |
589 // Vim and Jedit. I currently settled on Codium / VSCode |
579 // |
590 // |
|
591 // https://vscodium.com |
580 // https://code.visualstudio.com |
592 // https://code.visualstudio.com |
581 // |
593 // |
582 // There are also plugins for Eclipse and IntelliJ - YMMV. |
594 // There are also plugins for Eclipse and IntelliJ - YMMV. |
583 // Finally there are online editors specifically designed for |
595 // Finally there are online editors specifically designed for |
584 // running Scala applications (but do not blame me if you lose |
596 // running Scala applications (but do not blame me if you lose |
585 // all what you typed in): |
597 // all what you typed in): |
586 // |
598 // |
587 // https://scalafiddle.io |
|
588 // https://scastie.scala-lang.org |
599 // https://scastie.scala-lang.org |
589 // |
600 // |
590 // |
601 // |
591 // |
602 // |
592 // Scala Library Docs |
603 // Scala Library Docs |