19 time_needed(10, for (n <- list.par) yield n + 42) |
19 time_needed(10, for (n <- list.par) yield n + 42) |
20 |
20 |
21 // (needs a library and 'magic' option -Yrepl-class-based) |
21 // (needs a library and 'magic' option -Yrepl-class-based) |
22 |
22 |
23 |
23 |
|
24 |
|
25 |
24 // Just for Fun: Mutable vs Immutable |
26 // Just for Fun: Mutable vs Immutable |
25 //==================================== |
27 //==================================== |
26 // |
28 // |
27 // - no vars, no ++i, no += |
29 // - no vars, no ++i, no += |
28 // - no mutable data-structures (no Arrays, no ListBuffers) |
30 // - no mutable data-structures (no Arrays, no ListBuffers) |
66 // the first produces a result, while the second does not |
68 // the first produces a result, while the second does not |
67 for (n <- List(1, 2, 3, 4, 5)) yield n * n |
69 for (n <- List(1, 2, 3, 4, 5)) yield n * n |
68 |
70 |
69 |
71 |
70 for (n <- List(1, 2, 3, 4, 5)) println(n) |
72 for (n <- List(1, 2, 3, 4, 5)) println(n) |
|
73 |
|
74 |
|
75 // String Interpolations |
|
76 //======================= |
|
77 |
|
78 val n = 3 |
|
79 println("The square of " + n + " is " + square(n) + ".") |
|
80 |
|
81 println(s"The square of ${n} is ${square(n)}.") |
|
82 |
|
83 |
|
84 // helpful for debugging purposes |
|
85 // |
|
86 // "The most effective debugging tool is still careful thought, |
|
87 // coupled with judiciously placed print statements." |
|
88 // — Brian W. Kernighan, in Unix for Beginners (1979) |
|
89 |
|
90 |
|
91 def gcd_db(a: Int, b: Int) : Int = { |
|
92 println(s"Function called with ${a} and ${b}.") |
|
93 if (b == 0) a else gcd_db(b, a % b) |
|
94 } |
|
95 |
|
96 gcd_db(48, 18) |
|
97 |
|
98 |
|
99 // Asserts/Testing |
|
100 //================= |
|
101 |
|
102 assert(gcd(48, 18) == 6) |
|
103 |
|
104 assert(gcd(48, 18) == 5, "The gcd test failed") |
71 |
105 |
72 |
106 |
73 |
107 |
74 // Higher-Order Functions |
108 // Higher-Order Functions |
75 //======================== |
109 //======================== |