This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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" |
NewerOlder