"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(" ") == factorial(30)) } 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."> "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(" ") == factorial(30)) } 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.">

Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

1 Programming in Scala Chapters 2 & 3

2 Two “Hello World” programs package hello object HelloWorld1 extends Application { println("Hello, world 1!") } package hello object HelloWorld2 { def main(args: Array[String]) { println("Hello, world 2! " + args.toList) } The argument of the scala command has to be a top- level object. If that object is followed by: extends Application, 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.

3 Chapter 1 code 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)) } 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.

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 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) Chapter 2 more code

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 Note that methods without arguments are written without parentheses: s.length

13 List operations

14

15 Homework These are all list exercises. 1.def hasNEvens(list: List[Int], n: Int): Boolean = 2.def containsElement(list: List[Any], element: Any): Boolean = 3.def allOdd(list: List[Int]): Boolean = 4.def square(list: List[Int]): List[Int] = 5.def sumList(list: List[Int]): Int = 6.def isSorted(list: List[Int]): Boolean = 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.


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

Similar presentations


Ads by Google