Scala
Scala is a powerful and statically-typed programming language that blends object-oriented and functional programming paradigms. It runs on the Java Virtual Machine (JVM) and offers seamless interoperability with existing Java libraries and frameworks. Scala's expressive syntax, type inference, and functional programming capabilities enable concise and highly expressive code. It supports features like pattern matching, immutability, higher-order functions, and parallel processing, making it well-suited for building scalable and concurrent applications.
Module 1: Introduction to Scala
Introduction to Scala
Scala is a general-purpose, high-level, multi-paradigm programming language. It is a pure object-oriented language, but also incorporates functional programming features.
object HelloWorld { def main(args: Array[String]) { println("Hello, world!") } }
Variables and Basic Data Types
In Scala, you have mutable (var) and immutable (val) variables. Scala supports all the numeric data types, boolean type, character type, string type, etc.
val x: Int = 10 var y: Double = 20.0 val isScalaFun: Boolean = true val character: Char = 'a' val message: String = "Hello, Scala!"
Control Flow
Scala offers several control flow constructs like if, else, for, while, and match-case (similar to switch-case in other languages).
val a: Int = 10 if( a < 20 ) { println("a is less than 20") } else { println("a is not less than 20") }
Module 2: Functions and Collections in Scala
Functions
Functions in Scala are objects. Scala allows defining anonymous functions (functions that have no name).
def add(x: Int, y: Int): Int = { return x + y; } println(add(10, 20))
Collections
Scala collections systematically distinguish between mutable and immutable collections. It provides a rich set of collection library.
val numbers: Seq[Int] = Seq(1, 2, 3, 4) val map: Map[String, Int] = Map("apple" -> 3, "banana" -> 4) val set: Set[Int] = Set(1, 1, 2)
Module 3: Object-Oriented and Functional Programming in Scala
Object-Oriented Programming
Scala is a pure object-oriented language in the sense that every value is an object.
class Point(val xc: Int, val yc: Int) { var x: Int = xc var y: Int = yc def move(dx: Int, dy: Int) { x = x + dx y = y + dy println ("Point x location : " + x) println ("Point y location : " + y) } }
Functional Programming
Scala also has full support for functional programming, including features such as lambda expressions, higher-order functions, and immutability.
val list = List(1, 2, 3, 4, 5) val doubledList = list.map(x => x * 2) println(doubledList)