var numberOfKittens = 6 val kittensPerHouse = 101 val alphabet = "abcdefghijklmnopqrstuvwxyz" var done = false numberOfKittens += 1 // kittensPerHouse = kittensPerHouse * 2 // This would not compile; kittensPerHouse is not updatable println(alphabet) done = true // A simple conditional; by the way, this is a comment if (numberOfKittens > kittensPerHouse) { println("Too many kittens!!!") } // The braces are not required when all branches are one liners. However, the // Scala Style Guide prefers brace omission only if an "else" clause is included. // (Preferably not this, even though it compiles...) if (numberOfKittens > kittensPerHouse) println("Too many kittens!!!") // ifs have else clauses, of course // This is where you can omit braces! if (done) println("we are done") else numberOfKittens += 1 // And else ifs // For style, keep braces because not all branches are one liners. if (done) { println("we are done") } else if (numberOfKittens < kittensPerHouse) { println("more kittens!") numberOfKittens += 1 } else { done = true } val likelyCharactersSet = if (alphabet.length == 26) "english" else "not english" println(likelyCharactersSet) // Simple scaling function with an input argument, e.g., times2(3) returns 6 // Curly braces can be omitted for short one-line functions. def times2(x: Int): Int = 2 * x // More complicated function def distance(x: Int, y: Int, returnPositive: Boolean): Int = { val xy = x * y if (returnPositive) xy.abs else -xy.abs } // Overloaded function def times2(x: Int): Int = 2 * x def times2(x: String): Int = 2 * x.toInt times2(5) times2("7") /** Prints a triangle made of "X"s * This is another style of comment */ def asciiTriangle(rows: Int) { // This is cute, multiplying "X" makes a string with many copies of "X" def printRow(columns: Int): Unit = println("X" * columns) if(rows > 0) { printRow(rows) asciiTriangle(rows - 1) // Here is the recursive call } } // printRow(1) // This would not work, since we're calling printRow outside its scope asciiTriangle(6) val x = 7 val y = 14 val list1 = List(1, 2, 3) val list2 = x :: y :: y :: Nil // An alternate notation for assembling a list val list3 = list1 ++ list2 // Appends the second list to the first list val m = list2.length val s = list2.size val headOfList = list1.head // Gets the first element of the list val restOfList = list1.tail // Get a new list with first element removed val third = list1(2) // Gets the third element of a list (0-indexed) for (i <- 0 to 7) { print(i + " ") } println() for (i <- 0 until 7) { print(i + " ") } println() for(i <- 0 to 10 by 2) { print(i + " ") } println() val randomList = List(util.Random.nextInt(), util.Random.nextInt(), util.Random.nextInt(), util.Random.nextInt()) var listSum = 0 for (value <- randomList) { listSum += value } println("sum is " + listSum) // WrapCounter counts up to a max value based on a bit size class WrapCounter(counterBits: Int) { val max: Long = (1 << counterBits) - 1 var counter = 0L def inc(): Long = { counter = counter + 1 if (counter > max) { counter = 0 } counter } println(s"counter created with max value $max") } val x = new WrapCounter(2) x.inc() // Increments the counter // Member variables of the instance x are visible to the outside, unless they are declared private if(x.counter == x.max) { println("counter is about to wrap") } x inc() // Scala allows the dots to be omitted; this can be useful for making embedded DSL's look more natural // A one-line code block doesn't need to be enclosed in {} def add1(c: Int): Int = c + 1 class RepeatString(s: String) { val repeatedString = s + s } val intList = List(1, 2, 3) val stringList = intList.map { i => i.toString }