65 if (a == b) println("Equal") else println("Unequal") |
66 if (a == b) println("Equal") else println("Unequal") |
66 |
67 |
67 Set(1,2,3) == Set(3,1,2) |
68 Set(1,2,3) == Set(3,1,2) |
68 List(1,2,3) == List(3,1,2) |
69 List(1,2,3) == List(3,1,2) |
69 |
70 |
70 val n1 = 3 + 7 |
|
71 val n2 = 5 + 5 |
|
72 |
|
73 n1 == n2 |
|
74 |
71 |
75 // this applies to "concrete" values...pretty much everything; |
72 // this applies to "concrete" values...pretty much everything; |
76 // but for example you cannot compare functions (later) |
73 // but for example you cannot compare functions (later) |
77 |
74 |
78 |
75 |
107 //==================== |
105 //==================== |
108 |
106 |
109 List(1,2,3,1).toString |
107 List(1,2,3,1).toString |
110 List(1,2,3,1).toSet |
108 List(1,2,3,1).toSet |
111 |
109 |
|
110 "hello".toList |
112 "hello".toList.tail |
111 "hello".toList.tail |
|
112 |
|
113 |
113 1.toDouble |
114 1.toDouble |
114 |
115 |
115 1L |
116 1L // a long |
116 // a long |
117 1F // a float |
117 |
118 |
118 // useful list methods |
119 // useful list methods |
119 |
120 |
120 List(1,2,3,4).length |
121 List(1,2,3,4).length |
121 List(1,2,3,4).reverse |
122 List(1,2,3,4).reverse |
183 t._4 |
184 t._4 |
184 |
185 |
185 |
186 |
186 List(("one", 1), ("two", 2), ("three", 3)) |
187 List(("one", 1), ("two", 2), ("three", 3)) |
187 |
188 |
|
189 |
188 // Function Definitions |
190 // Function Definitions |
189 //====================== |
191 //====================== |
190 |
192 |
191 def incr(x: Int) : Int = x + 1 |
193 def incr(x: Int) : Int = x + 1 |
192 def double(x: Int) : Int = x + x |
194 def double(x: Int) : Int = x + x |
264 if (n == 0) 1 else x * power(x, n - 1) |
266 if (n == 0) 1 else x * power(x, n - 1) |
265 |
267 |
266 power(5, 5) |
268 power(5, 5) |
267 |
269 |
268 |
270 |
269 // Option type |
|
270 //============= |
|
271 |
|
272 //in Java if something unusually happens, you return null or something |
|
273 // |
|
274 //in Scala you use Options instead |
|
275 // - if the value is present, you use Some(value) |
|
276 // - if no value is present, you use None |
|
277 |
|
278 |
|
279 List(7,2,3,4,5,6).find(_ < 4) |
|
280 List(5,6,7,8,9).find(_ < 4) |
|
281 |
|
282 |
|
283 |
|
284 // error handling with Options (no exceptions) |
|
285 // |
|
286 // Try(something).getOrElse(what_to_do_in_case_of_an_exception) |
|
287 // |
|
288 import scala.util._ |
|
289 import io.Source |
|
290 |
|
291 val my_url = "https://nms.kcl.ac.uk/christian.urban/" |
|
292 |
|
293 Source.fromURL(my_url).mkString |
|
294 |
|
295 Try(Source.fromURL(my_url).mkString).getOrElse("") |
|
296 |
|
297 Try(Some(Source.fromURL(my_url).mkString)).getOrElse(None) |
|
298 |
|
299 |
|
300 // the same for files |
|
301 Try(Some(Source.fromFile("text.txt").mkString)).getOrElse(None) |
|
302 |
|
303 |
|
304 |
|
305 // how to implement a function for reading something from files... |
|
306 |
|
307 def get_contents(name: String) : List[String] = |
|
308 Source.fromFile(name).getLines.toList |
|
309 |
|
310 get_contents("test.txt") |
|
311 |
|
312 // slightly better - return Nil |
|
313 def get_contents(name: String) : List[String] = |
|
314 Try(Source.fromFile(name).getLines.toList).getOrElse(List()) |
|
315 |
|
316 get_contents("text.txt") |
|
317 |
|
318 // much better - you record in the type that things can go wrong |
|
319 def get_contents(name: String) : Option[List[String]] = |
|
320 Try(Some(Source.fromFile(name).getLines.toList)).getOrElse(None) |
|
321 |
|
322 get_contents("text.txt") |
|
323 get_contents("test.txt") |
|
324 |
|
325 |
|
326 // String Interpolations |
|
327 //======================= |
|
328 |
|
329 val n = 3 |
|
330 println("The square of " + n + " is " + square(n) + ".") |
|
331 |
|
332 println(s"The square of ${n} is ${square(n)}.") |
|
333 |
|
334 |
|
335 // helpful for debugging purposes |
|
336 // |
|
337 // "The most effective debugging tool is still careful thought, |
|
338 // coupled with judiciously placed print statements." |
|
339 // — Brian W. Kernighan, in Unix for Beginners (1979) |
|
340 |
|
341 |
|
342 def gcd_db(a: Int, b: Int) : Int = { |
|
343 println(s"Function called with ${a} and ${b}.") |
|
344 if (b == 0) a else gcd_db(b, a % b) |
|
345 } |
|
346 |
|
347 gcd_db(48, 18) |
|
348 |
|
349 |
|
350 // Asserts/Testing |
|
351 //================= |
|
352 |
|
353 assert(gcd(48, 18) == 6) |
|
354 |
|
355 assert(gcd(48, 18) == 5, "The gcd test failed") |
|
356 |
|
357 |
271 |
358 // For-Comprehensions (not For-Loops) |
272 // For-Comprehensions (not For-Loops) |
359 //==================================== |
273 //==================================== |
360 |
274 |
361 |
275 |
442 // BTW: a roundabout way of printing out a list, say |
356 // BTW: a roundabout way of printing out a list, say |
443 val lst = ('a' to 'm').toList |
357 val lst = ('a' to 'm').toList |
444 |
358 |
445 for (i <- (0 until lst.length)) println(lst(i)) |
359 for (i <- (0 until lst.length)) println(lst(i)) |
446 |
360 |
447 |
|
448 // Why not just? Why making your life so complicated? |
361 // Why not just? Why making your life so complicated? |
449 for (c <- lst) println(c) |
362 for (c <- lst) println(c) |
|
363 |
|
364 |
450 |
365 |
451 // Aside: concurrency |
366 // Aside: concurrency |
452 // scala -Yrepl-class-based -cp scala-parallel-collections_2.13-0.2.0.jar |
367 // scala -Yrepl-class-based -cp scala-parallel-collections_2.13-0.2.0.jar |
453 |
368 |
454 for (n <- (1 to 10)) println(n) |
369 for (n <- (1 to 10)) println(n) |
518 println(cnt) |
433 println(cnt) |
519 } |
434 } |
520 |
435 |
521 test() |
436 test() |
522 |
437 |
|
438 // Option type |
|
439 //============= |
|
440 |
|
441 //in Java if something unusually happens, you return null or something |
|
442 // |
|
443 //in Scala you use Options instead |
|
444 // - if the value is present, you use Some(value) |
|
445 // - if no value is present, you use None |
|
446 |
|
447 |
|
448 List(7,2,3,4,5,6).find(_ < 4) |
|
449 List(5,6,7,8,9).find(_ < 4) |
|
450 |
|
451 |
|
452 |
|
453 // error handling with Options (no exceptions) |
|
454 // |
|
455 // Try(something).getOrElse(what_to_do_in_case_of_an_exception) |
|
456 // |
|
457 import scala.util._ |
|
458 import io.Source |
|
459 |
|
460 val my_url = "https://nms.kcl.ac.uk/christian.urban/" |
|
461 |
|
462 Source.fromURL(my_url).mkString |
|
463 |
|
464 Try(Source.fromURL(my_url).mkString).getOrElse("") |
|
465 |
|
466 Try(Some(Source.fromURL(my_url).mkString)).getOrElse(None) |
|
467 |
|
468 |
|
469 // the same for files |
|
470 Try(Some(Source.fromFile("text.txt").mkString)).getOrElse(None) |
|
471 |
|
472 |
|
473 |
|
474 // how to implement a function for reading something from files... |
|
475 |
|
476 def get_contents(name: String) : List[String] = |
|
477 Source.fromFile(name).getLines.toList |
|
478 |
|
479 get_contents("test.txt") |
|
480 |
|
481 // slightly better - return Nil |
|
482 def get_contents(name: String) : List[String] = |
|
483 Try(Source.fromFile(name).getLines.toList).getOrElse(List()) |
|
484 |
|
485 get_contents("text.txt") |
|
486 |
|
487 // much better - you record in the type that things can go wrong |
|
488 def get_contents(name: String) : Option[List[String]] = |
|
489 Try(Some(Source.fromFile(name).getLines.toList)).getOrElse(None) |
|
490 |
|
491 get_contents("text.txt") |
|
492 get_contents("test.txt") |
|
493 |
|
494 |
|
495 |
523 |
496 |
524 // Further Information |
497 // Further Information |
525 //===================== |
498 //===================== |
526 |
499 |
527 // The Scala homepage and general information is at |
500 // The Scala homepage and general information is at |