Scala Explained!

Scala tips and code snippets.

Scala library explained

Classes hierarchy

Scala is primarily a object language, all the values are objects. Different classes of objects are defined in Scala Standard Library, based on the following class hierarchy:

classes

Cast

Values of a given type can be transformed into an equivalent values of an other type, both implicitly or explicitly.

Implicit cast

Scala can cast a variable from a type to an other type using several hints, like the destination variable type.

// Converts implicitly a char to a Long
val codePoint: Long = '☻'
println(s"Smiley Unicode value is $codePoint")
// --> Smiley Unicode value is 9787

The following graph shows which number types can be implicitly cast to an other number type:

cast

Explicit cast

An explicit cast is done by calling a cast method to produce an equivalent value with the desired type. By convention the cast methods starts with to... followed by the name of the new type. Example: toLong, toChar etc…

val longValue = 12L
val intValue = longValue.toInt // convert a `Long` value to a `Int` value

All Scala values can be cast to a String using toString method.

Primary value types

In most programming languages the primary values are numbers (boolean, integer, rational), characters and strings.

These value types are defined in Scala standard library and here is how you can use them:

True / False values: Boolean

Boolean values are either true or false. They are the result of comparison operations, for example a > b.

Boolean values supports the following operations:

// Example operations on Boolean values
val isFrontEngineRunning = true
val isRearEngineRunning = true
val isThereAPiloteInThePlane = false

val thePlaneWillLand =
  isThereAPiloteInThePlane || (isFrontEngineRunning && isRearEngineRunning)

println(s"the plane will ${ if (thePlaneWillLand) "land" else "crash" }!")
// --> the plane will land!

Numbers: Byte, Char, Short, Int, Long, Float, Double

Different number types are defined for different usages:

Number representations are explained in depth in the literals section.

Number values supports the following operations:

// Operation on numbers
val sucessRate = 100 * (4294967296L - 1048576) / 4294967296D
println(f"sucess rate = $sucessRate%.2f percent")
// --> sucess rate = 99.98 percent

Integer types Byte, Char, Short, Int & Long also support:

// Bitwise operations
// here is a filtering statement
val number = 19
val lowBits = number & ((1<<4) - 1)
println(s"number filtered to only keep the 4 lowest bits = $lowBits")
// --> number filtered to only keep the 4 lowest bits = 3

Text values: String

Scala String are based on Java String.

String values supports the following operations:

Useful String methods

String Interpolation and formatting

Values can be added to a String template using formatting and interpolation:

Convenient collection types

Scala collections types are containers that hold values: List, Map, Set, etc… There are two families of collections:

By default all available collections are immutable, to use a mutable version of a collection, it is required to import it first:

// Seq type is immutable by default
val immutableCollection = Seq(1, 2, 3)

// the mutable Seq type is imported and used here
import collection.mutable.Seq

val mutableCollection = Seq(1, 2, 3)
mutableCollection(0) = 4 // updates the element at the position 0

There are many different collections types. The concepts behind each collection type are described in Scala documentation.

Here are some convenient collection types that can be used in Scala programs:

String type is equivalent to a sequential collection.

Sequence of elements: List

Scala List is an immutable sequence of elements.

Combine List elements

Access List elements

Filter/search List items

Map-reduce List items

Key-value association: Map

Scala Map are collections that hold pairs of key-value.

Combine Map elements

Access Map elements

Remove Map elements

Filter/search Map items

Map-reduce Map items

You can leave a comment or request changes here.

This project is maintained by Y. Somda (yoeo)
Logo by Icon Island — theme by orderedlist — remixed by yoeo