General Scala

Values, Variables and Control Structures

val moritz = 1865
val s = {
  "Max"
  moritz
}
val result = if (true) "blue" else 2

Collections

val xs = Set( ... )
scala> langs("Lisp")
res0: String = John McCarthy

scala> langs("Java")
res1: String = James Gosling

scala> langs("Pascal")
res2: String = Niklaus Wirth

scala> langs("Scala")
res3: String = Martin Odersky

Functions

Classes and Objects

Inheritance

Given the Person class below, write the minimal non-final subclass of Person named Employee that adds a mutable salary: Double field and initializes the fields defined in Person.

class Person(val name: String)

Case Classes and Pattern Matching

sealed trait AmpelMann
case object Grün extends AmpelMann
case object Rot extends AmpelMann

Algebraic Data Types

def hauptstadt(land: Bundesland) = land match {
  case land: Berlin => "Berlin"
  case land: Brandenburg => "Potsdam"
  case land: MecklenburgVolpommern => "Schwerin"
  case land: SachsenAnhalt => "Magdeburg"
  case land: Sachsen => "Dresden"
  case land: SchleswigHolstein => "Kiel"
  case land: FreieHansestadtHamburg => "Hamburg"
  case land: HansestadtBremen => "Bremen"
  case land: Niedersachsen => "Hannover"
  case land: NordrheinWestfalen => "Düsseldorf"
  case land: FreistaatThüringen => "Erfurt"
  case land: Hessen => "Wiesbaden"
  case land: RheinlandPfalz => "Mainz"
  case land: Saarland => "Saarbrücken"
  case land: BadenWürttemberg => "Stuttgart"
  case land: FreistaatBayern => "München"
}