8. Syntax
object Sounds extends App {
Animal dog = new Dog("Beike")
println(s"Dog: ${dog.name}")
println(dog.makeSound)
}
trait Animal {
def makeSound: String
}
class Dog(val name: String) extends Animal {
override def makeSound: String =
s"$name says WOOF!"
}
public class Sounds {
public static void main(String[] args) {
Animal dog = new Dog("Beike");
System.out.println("Dog: " + dog.getName());
System.out.println(dog.makeSound());
}
}
interface Animal {
String makeSound();
}
class Dog extends Animal {
private String name;
public Dog(String name) {
this.name = name;
}
@Override public String makeSound() {
return getName() + " says WOOF!";
}
public String getName() {
return name;
}
}
9. Basis
• var
• val
• def
• if
• for
• while
val fordTColor = "Black"
var peugeotColor = "Blue"
peugeotColor = "Gray"
val ferrariColor =
if (ferrari.isRed) "Red"
else "Yellow"
val colors = Array(
"Blue",
"Black",
"Gray",
"White",
)
def printAllColors: Unit = {
for (color <- colors) {
println(color)
}
}
10. Waarden en
variabelen
• variable
• value
val one = 1
one = 2 // Compileert niet
val oneToFive = 1 to 5
val twoToSix = oneToFive map { _ + 1 }
var two = 2
two = 1 // Ok, maar raar
23. Hogere orde functies
• Functies uit functies
// makeAdder :: Int => (Int => Int)
val makeAdder = {
x: Int => {
y: Int => x + y
}
}
// add5 :: Int => Int
val add5 = makeAdder(5)
// eleven :: Int
val eleven = add5(6)
// 11
24. Hogere orde functies
• Functies uit functies
• Functies in argumenten
def foreach(fn: Int => Unit, xs: List[Int]) =
for (x <- xs) fn(x)
foreach(println, List(1, 2, 3))
27. Pattern
matching
• Switch on steroids
1 match {
case 1 => "one"
case 2 => "two"
}
// "one"
(1, "one") match {
case (2, string) => "first"
case (1, string) => "second"
case (_, "one") => "third"
case (1, "one") => "fourth"
case _ => "no match"
}
28. Pattern matching
sealed trait Job
case object Sales extends Job
case object Boss extends Job
case class Programmer(lang: String) extends Job
case class Employee(name: String, job: Job)
val henry = Employee("Henry", Programmer("Scala"))
val james = Employee("James", Boss)
val peter = Employee("Peter", Sales)
henry.job match {
case Boss => "bossing around"
case Sales => "selling stuff"
case Programmer(lang) => s"programming $lang"
}
30. null
RoutePlan plan = null;
Car car = new Garage().getCar("Ferrari");
if (car != null) {
NavSystem nav = ferrari.navSystem();
if (nav != null) {
Route route = nav.routeTo("Berlin");
if (route != null) {
plan = route.plan();
}
}
}
31. Option
• Geeft aan dat iets optioneel is
• Trait
• Case classes: Some(x: Any), None
32. null vs Option
val garage = new Garage()
val routePlan = for {
//Car <- Option[Car]
car <- garage.getCar("Ferrari")
//NavSystem <- Option[NavSystem]
nav <- car.navigationSystem
//Route <- Option[Route]
route <- nav.routeTo("Berlin")
//RoutePlan <- Option[RoutePlan]
plan <- route.plan
//RoutePlan -> Option[RoutePlan]
} yield plan
33. null vs Option
val garage = new Garage()
val routePlan = for {
car <- garage.getCar("Ferrari")
nav <- car.navigationSystem
route <- nav.routeTo("Berlin")
plan <- route.plan
} yield plan
35. QuickSort
• Neem een lijst met getallen
• Neem een getal ‘x’ uit deze lijst
• Uit de rest van de lijst:
• Zet de getallen lager dan ‘x’ voor ‘x’ en sorteer
• Zet de getallen hoger dan ‘x’ na ‘x’ en sorteer
36. QuickSort
• Recursieve functie
• Base case: Lege lijst
• Andere cases: Niet-lege lijst
• Manier om lijst op te delen o.b.v. predikaat
• Hogere orde functies!