1 2 3 4 5 6 7 8 9 10 | ;; Scala REPL % scala Welcome to Scala 3.2.2 (19.0.2, Java Java HotSpot(TM) 64-Bit Server VM). Type in expressions for evaluation. Or try :help. scala> println("Hello, World!"); Hello, World! scala> |
1 2 3 4 5 6 7 8 9 10 | ;; Everything starts with a Hello, World! object HelloWorld { /* This is my first java program. * This will print 'Hello World' as the output */ def main(args: Array[String]) { println("Hello, World!") // prints Hello World } } |
1 2 3 4 5 6 7 8 9 10 | ;; Comments // Single line comments print("This") // Comment at end of line /* Multi line comments Formatting is how you want it */ |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | ;; Scala Basics Object // Objects have states and behaviors. An object is an instance of a class. // Example − A dog has states - color, name, breed as well as behaviors - wagging, barking, and eating. Class // A class can be defined as a template/blueprint that describes the behaviors/states that are related to the class. Methods // A method is basically a behavior. A class can contain many methods. It is in methods where the logics are // written, data is manipulated and all the actions are executed. Fields // Each object has its unique set of instance variables, which are called fields. // An object's state is created by the values assigned to these fields. Closure // A closure is a function, whose return value depends on the value of one or more variables // declared outside this function. Traits // A trait encapsulates method and field definitions, which can then be reused by mixing them into classes. // Traits are used to define object types by specifying the signature of the supported methods. |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | ;; Scala keywords abstract. case catch class def do else extends false final finally for forSome if implicit import lazy match new Null object override package private protected return sealed super this throw trait Try true type val Var while with yield - : = => <- <: <% >: # @ |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | ;; Data Types var x: Int = 3 // Mutable val y: Double = 42.0 // Immutable var m = "Hello" // Inferred mutable type val n = 3.33 // Inferred immutable type val b: Byte = 1 val i: Int = 1 val l: Long = 1 val s: Short = 1 val d: Double = 2.0 val f: Float = 3.0 val c: Char = 'c' var a = BigInt(1_234_567_890_987_654_321L) var b = BigDecimal(123_456.789) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | // If val x: Int = 0 if x > 0 then println("X is > 0") // If else val x: Int = 1 if x == 0 println("X is == 0") else println("X is > 0") // Nested If if x == 0 println("X is == 0") else if x < 0 println("X is < 0") else println("X is > 0") // If as an expression val x = if a < b then a else b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | // Loops // Basic for count for i <- 1 to 3 do println(i) // for until ( increments ) for i <- 0 until 10 by 2 do println(i) // for loop sequence val ints = List(1, 2, 3, 4, 5) for i <- ints do doSomething(i) // Guards val ints = List(1, 2, 3, 4, 5) for i <- ints // For each i in ints if i > 2 // Guard: Only if i is gt 2 do println(i) // Yields val doubles = for i <- ints yield i * 2 val doubles: List[Int] = List(2, 4, 6, 8, 10) val names = List("chris", "ed", "maurice") val capNames = for name <- names yield name.capitalize val fruits = List("apple", "banana", "lime", "orange") val fruitLengths = for f <- fruits if f.length > 4 yield // you can use multiple lines // of code here f.length val fruitLengths: List[Int] = List(5, 6, 6) |
1 2 3 4 5 6 | // while var x = 10 while x >= 0 do doSomething(x) x -= 1 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | // match // match value val i = 1 i match case 1 => println("one") // i == 1 case 2 => println("two") // i == 2 case _ => println("other") // // match expression val result = i match case 1 => "one" case 2 => "two" case _ => "other" // match type val p = Person("Fred") p match case Person(name) if name == "Fred" => println(s"$name says, Yubba dubba doo") case Person(name) if name == "Bam Bam" => println(s"$name says, Bam bam!") case _ => println("Watch the Flintstones!") def getClassAsString(x: Matchable): String = x match case s: String => s"'$s' is a String" case i: Int => "Int" case d: Double => "Double" case l: List[?] => "List" case _ => "Unknown" // examples getClassAsString(1) // Int getClassAsString("hello") // 'hello' is a String getClassAsString(List(1, 2, 3)) // List |
1 | ;; Methods |
1 | ;; Functions |
1 | ;; Closures |
1 | ;; Options |
1 | ;; Iterators |
1 | // Classes |
1 | // Singleton Objects |
1 | // Access Modifiers |
1 | ;; Traits |
1 | ;; Pattern Matching |
1 | ;; Regular Expressions |
1 | ;; Extractors |
1 | ;; File I/O |
1 2 3 4 5 6 7 8 9 | // Exceptions try writeTextToFile(text) catch case ioe: IOException => println("Got an IOException.") case nfe: NumberFormatException => println("Got a NumberFormatException.") finally println("Clean up your resources here.") |