Skip to content

Instantly share code, notes, and snippets.

View awelm's full-sized avatar
🎯
Focusing

Akila Welihinda awelm

🎯
Focusing
View GitHub Profile
// Note all variables declared with `val` are immutable
val opt = Some(5) // type: Option[Int], value: Some(5)
/*
* map lets you run a transformation function on the value possibly stored
* inside the Option. If map is called on an empty Option, then map will skip
* the transformation function and return None. The first map call below prints
* the stored value and then adds 2 to it. The second map call wraps the stored
* value inside another Option, which is why the return value has an extra
* Option layer. The third call always replaces the stored value with None.
// A case class in Scala is just a typed tuple
case class Person(name: String, title: String)
val person = Person("Joe Smith", "Mr")
val professors = Set("Joe Smith")
val greeting = person match {
case Person(name, "Dr") =>
// This block executes if person has the title "Dr"
s"Welcome Doctor $name"