Lesson 3: Scala Programming Language Tutorial

Lesson 3: Scala Programming Language Tutorial


Example 1: Working with Functions

def greet(name: String): String = {
  s"Hello, $name!"
}

val message = greet("Scala")
println(message)

Explanation:

  1. def greet(name: String): String = { – This defines a function named greet that takes a String parameter name and returns a String. Functions in Scala can return values directly.

  2. s"Hello, $name!" – This is an interpolated string. The $name inside the string dynamically inserts the value of name, creating a greeting message like "Hello, Scala!".

  3. val message = greet("Scala") – Calls the greet function with "Scala" as an argument. The return value "Hello, Scala!" is assigned to the variable message.

  4. println(message) – Prints the message to the console. Since message contains "Hello, Scala!", the output displayed is "Hello, Scala!".


Example 2: Using Collections (List, Set, Map)

val numbers = List(1, 2, 3, 4, 5)
val uniqueNumbers = Set(1, 2, 2, 3)
val capitalCities = Map("France" -> "Paris", "Italy" -> "Rome")

println(numbers)
println(uniqueNumbers)
println(capitalCities)

Explanation:

  1. val numbers = List(1, 2, 3, 4, 5) – Creates a List containing ordered elements. Lists maintain sequence and can hold duplicate values, useful for ordered data structures like queues and arrays.

  2. val uniqueNumbers = Set(1, 2, 2, 3) – Defines a Set, which removes duplicate values. Even though 2 is repeated, the Set only stores {1, 2, 3} as unique elements.

  3. val capitalCities = Map("France" -> "Paris", "Italy" -> "Rome") – Defines a Map, which holds key-value pairs. The keys "France" and "Italy" map to "Paris" and "Rome" respectively.

  4. println(capitalCities) – Prints the Map, showing each country’s capital. The output will be Map(France -> Paris, Italy -> Rome), demonstrating how Scala maps store key-value pairs.


Example 3: Class and Object-Oriented Programming

class Car(val brand: String, val model: String) {
  def details(): String = s"$brand $model"
}

val myCar = new Car("Tesla", "Model S")
println(myCar.details())

Explanation:

  1. class Car(val brand: String, val model: String) { – Declares a Car class with two properties, brand and model, both defined as val, meaning they are immutable once assigned.

  2. def details(): String = s"$brand $model" – Defines a method details that returns a formatted string with brand and model. The string interpolation helps in dynamic value insertion.

  3. val myCar = new Car("Tesla", "Model S") – Creates an instance of Car with "Tesla" as brand and "Model S" as model. The new keyword is used to instantiate objects.

  4. println(myCar.details()) – Calls the details method on myCar, printing "Tesla Model S". This demonstrates object-oriented programming principles like encapsulation and class usage.


Example 4: Working with Higher-Order Functions (map, filter)

val numbers = List(1, 2, 3, 4, 5)

val doubled = numbers.map(x => x * 2)
val evens = numbers.filter(x => x % 2 == 0)

println(doubled)
println(evens)

Explanation:

  1. val numbers = List(1, 2, 3, 4, 5) – Creates a List of numbers from 1 to 5. Lists are commonly used in Scala for processing sequences of data.

  2. val doubled = numbers.map(x => x * 2) – Uses map, a higher-order function, to apply x * 2 to each element in numbers, resulting in a new list: List(2, 4, 6, 8, 10).

  3. val evens = numbers.filter(x => x % 2 == 0) – Uses filter to keep only even numbers from numbers. The result is List(2, 4), as they are the only even numbers.

  4. println(doubled) and println(evens) – Prints both transformed lists. doubled outputs List(2, 4, 6, 8, 10), while evens outputs List(2, 4), demonstrating functional programming.