1 / 15

Programming in Scala

Programming in Scala. Chapters 2 & 3. Two “Hello World” programs. package hello object HelloWorld1 extends Application { println("Hello, world 1!") }.

Download Presentation

Programming in Scala

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Programming in Scala Chapters 2 & 3

  2. Two “Hello World” programs package hello object HelloWorld1 extends Application { println("Hello, world 1!") } The argument of the scala command has to be a top-level object. If that object is followed by:extendsApplication, then all statements contained in that object will be executed; otherwise you have to add a method main which will act as the entry point of your program—as in Java. package hello object HelloWorld2 { def main(args: Array[String]) { println("Hello, world 2! " + args.toList) } }

  3. Chapter 1 code Terminating semi-colons are not required. No “new” needed in new Map() The function return type comes after the function signature. That is followed by = and then by the body. Better to use assert() than println(). assert() is a function, not a keyword. Put constant on left. BigInt() takes a String. package hello object Chapter_01 extends Application { var capital = Map("US" -> "Washington", "France" -> "Paris") capital += ("Japan" -> "Tokyo") capital += ("Russia" -> "Moscow") println(capital("Russia")) println(capital("Japan")) println(capital("France ")) def factorial(x: BigInt): BigInt = { if (x == 0) 1 else x * factorial(x - 1) } println(factorial(30)) assert(BigInt("265252859812191058636308480000000") == factorial(30)) }

  4. Chapter 2 code package hello object Chapter_02 extends Application { val msg = "Hello, world! " println(msg) val multiLine = "This is the next line. " val two = 2 +1 val three = 2 + 1 println(msg + "; " + multiline + "; " + two + "; " + three) var twoThree = 2 println(twoThree) twoThree += 1 println(twoThree) } val for value. Values are immutable, like final variables. var for variable is mutable—but discouraged. See if you can write your code without using var. Since there are no line terminators, how does the compiler know when a line is finished? What is it doing with the “+1”?

  5. Chapter 2 more code package hello object Chapter_02 extends Application { def max(x: Int, y: Int): Int = { if (x > y) x else y } println(max(3, 4)) def min(x: Int, y: Int): Int = if (x < y) x else y println(min(3, 4)) } Types are declared: variable: type as in x: Int The = after the function signature is like an assignment statement, assigning the body of the function to the signature. If it’s one statement, can leave out the braces.

  6. Chapter 2 more code Chapter 2 more code package hello object Chapter_02 extends Application { val list = List(1, 2, 3, 4) println(list) list.foreach(x => println(x*x)) for(x <- list) println(x*x*x) } Iterators: foreach(arg => function(arg))for(arg <- elements)function(arg)

  7. Chapter 3: all operations are method calls package hello object Chapter_03 extends Application { assert((1+2).==((1).+(2))) println("OK") } Scala has no operators, but the usual operator symbols can be used as method names. But doesn’t seem to work for all cases.

  8. Chapter 3: Arrays package hello object Chapter_02 extends Application { val numNames = Array("zero", "one", "two") val numNames1 = Array.apply("zero", "one", "two") } Arrays must be of one type. Arrays are mutable. apply() is like an Array factory method. Lists are immutable, but apparently they can be heterogeneous.

  9. Chapter 3: Lists package hello object Chapter_02 extends Application { // Lists are immutable like Java Strings. val oneTwoThreeInt = List(1, 2, 3) // OK. List[Int] // reduceLeft() takes a function of two arguments. val max = oneTwoThreeInt.reduceLeft((a, b) => a.max(b)) assert(oneTwoThreeInt(2) == max) oneTwoThreeInt = List(1, 2, "3") // Error. List[Any] val oneTwoThreeHetero = List(1, 2, "3") // OK. List[Any] val max = oneTwoThreeHetero .reduceLeft((a, b) => a.max(b)) // Error. val oneTwoThreeString = List("1", "2", "3") val cat = oneTwoStringThree.reduceLeft((a, b) => a.concat(b)) // OK assert(“123" == cat) }

  10. Chapter 3: Lists package hello object Chapter_02 extends Application { // Lists are immutable like Java Strings. val oneTwoThreeInt = List(1, 2, 3) // OK. List[Int] // :: adds to the front of a list. Any operator that ends in “:” applies // to the right. :: is the cons (construct) method. assert(oneTwoThreeInt == 1 :: 2 :: 3 :: Nil) // So 0 :: oneTwoThree // is really oneTwoThree.cons(0) }

  11. List operations

  12. List operations Note that methods without arguments are written without parentheses: s.length

  13. List operations

  14. List operations

  15. HomeworkThese are all list exercises. • def hasNEvens(list: List[Int], n: Int): Boolean = <returns true/false if list has/doesn’t have n even elements> • def containsElement(list: List[Any], element: Any): Boolean = <returns true/false if list contains/doesn’t contain element> • def allOdd(list: List[Int]): Boolean = <returns true/false if list elements are/are not all odd> • def square(list: List[Int]): List[Int] = <returns a list of the squares of the original list> • def sumList(list: List[Int]): Int = <returns the sum of the original list> • def isSorted(list: List[Int]): Boolean = <returns true/false depending on whether the list is sorted from low to high> For each exercise find three different ways to accomplish the result, at least one of which uses a list method that takes a function as an argument.

More Related