Scala 
Introductie voor Java (7) kenners
Edwin de Jong 
edwin.de.jong@topicus.nl
Martin Oderski
• Statically typed
• Statically typed 
• Type inference
• Statically typed 
• Type inference 
• Functional / OOP
• Statically typed 
• Type inference 
• Functional / OOP 
• Compiled naar JVM bytecode
• Statically typed 
• Type inference 
• Functional / OOP 
• Compiled naar JVM bytecode 
• én .NET
• Statically typed 
• Type inference 
• Functional / OOP 
• Compiled naar JVM bytecode 
• én .NET 
• én Javascript (scalajs)
Static vs. Dynamic
Type chaos
Type chaos 
<K, V> List<K> keysAsList(HashMap<K, V> aMap) { 
List<K> result = new LinkedList<K>(); 
for (K key : aMap.keySet()) { 
result.add(key); 
} 
return result; 
}
Implicit types
Implicit types 
def keysAsList[K, V](m: Map[K, V]) = 
m.keySet.toList
Alles is een object
Overpass Query Model
Overpass Query Model 
Node
Overpass Query Model 
Node 
Way
Overpass Query Model 
Node 
Way 
Relation
node["addr:city"="Deventer"]["addr:housenumber"="25"] 
["addr:street"="Singel"];out
node["addr:city"="Deventer"]["addr:housenumber"="25"] 
["addr:street"="Singel"];out 
Node( 
"addr:city" === "Deventer" and 
"addr:housenumber" === "25" and 
"addr:street" === "Singel") | OutStatement
node(2678759904);way(around: 1000);(rel(bw) 
["route"="bicycle"]["type"="route"];way(r);node(w););out
node(2678759904);way(around: 1000);(rel(bw) 
["route"="bicycle"]["type"="route"];way(r);node(w););out 
Node(Id(2678759904)) | 
Way(Around(1.kilo[metre])) | 
Union( 
Relation(isPartOf(Ways) and 
"route" === "bicycle" and 
"type" === "route") | 
Way(containedIn(Relations)) | 
Node(containedIn(Ways)) 
) | OutStatement)
sealed trait Statement
sealed trait Statement 
! 
case object OutStatement extends Statement
sealed trait Statement 
! 
case object OutStatement extends Statement 
! 
sealed trait PipeableStatement extends Statement { 
def |(other: Statement) = PipedStatement(this, other) 
def into(variable: String) = IntoStatement(this, variable) 
}
sealed trait Statement 
! 
case object OutStatement extends Statement 
! 
sealed trait PipeableStatement extends Statement { 
def |(other: Statement) = PipedStatement(this, other) 
def into(variable: String) = IntoStatement(this, variable) 
} 
! 
case class PipedStatement(left: Statement, right: Statement) 
extends PipeableStatement 
! 
case class IntoStatement(s: Statement, variable: String) 
extends PipeableStatement
Pimp my types
Pimp my types 
implicit class PimpedStringClause(str: String) { 
def ===(other: String) = Eq(str, other) 
def !==(other: String) = Neq(str, other) 
def ==~(other: String) = Regex(str, other) 
def !=~(other: String) = Regex(str, other) 
}
Pimp my types 
implicit class PimpedStringClause(str: String) { 
def ===(other: String) = Eq(str, other) 
def !==(other: String) = Neq(str, other) 
def ==~(other: String) = Regex(str, other) 
def !=~(other: String) = Regex(str, other) 
} 
val str = “addr:street” 
new PimpedStringClause(str).===(“Singel 25”)
Pimp my types 
implicit class PimpedStringClause(str: String) { 
def ===(other: String) = Eq(str, other) 
def !==(other: String) = Neq(str, other) 
def ==~(other: String) = Regex(str, other) 
def !=~(other: String) = Regex(str, other) 
} 
val str = “addr:street” 
new PimpedStringClause(str).===(“Singel 25”) 
Eq( 
“addr:street”, 
“Singel 25”)
Pimp my types 
implicit class PimpedStringClause(str: String) { 
def ===(other: String) = Eq(str, other) 
def !==(other: String) = Neq(str, other) 
def ==~(other: String) = Regex(str, other) 
def !=~(other: String) = Regex(str, other) 
} 
val str = “addr:street” 
new PimpedStringClause(str).===(“Singel 25”) 
str.===(“Singel 25”) 
Eq( 
“addr:street”, 
“Singel 25”)
Pimp my types 
implicit class PimpedStringClause(str: String) { 
def ===(other: String) = Eq(str, other) 
def !==(other: String) = Neq(str, other) 
def ==~(other: String) = Regex(str, other) 
def !=~(other: String) = Regex(str, other) 
} 
val str = “addr:street” 
new PimpedStringClause(str).===(“Singel 25”) 
str.===(“Singel 25”) 
str === “Singel 25” 
Eq( 
“addr:street”, 
“Singel 25”)
Pattern matching 
def ClauseShows[S <: Clause]: Show[S] = shows { 
case Id(id) ⇒ s"($id)" 
case Eq(tag, value) ⇒ s"""["$tag"="$value"]""" 
case HasKey(tag) ⇒ """["$tag"]""" 
case Regex(tag, value) ⇒ s"""["$tag"~"$value"]""" 
… 
}
Map, flatmap en filter
Type-safe DSLs
Type-safe DSLs 
val avg: Option[Float] = 
from(grades)(g => 
where(g.subjectId === mathId) 
compute(avg(g.scoreInPercentage)) 
)
Running Demo: 
OpenStreetMaps en 
Buienradar
Buienradar API 
http://gps.buienradar.nl/getrr.php? 
lat=52.2560148&lon=6.160242 
000|17:35 040|17:40 000|17:45 000|17:50 000| 
17:55 000|18:00 000|18:05 063|18:10 000|18:15 
000|18:20 000|18:25 000|18:30 000|18:35 000| 
18:40 000|18:45 000|18:50 000|18:55 000|19:00 
000|19:05 000|19:10 000|19:15 000|19:20 000| 
19:25 000|19:30 000|19:35 
GET
• Actors
• Actors 
• Scalaz
• Actors 
• Scalaz 
• Higher-order types
• Actors 
• Scalaz 
• Higher-order types 
• Parser Combinators
• Actors 
• Scalaz 
• Higher-order types 
• Parser Combinators 
• ScalaTest / ScalaCheck
Ik wil meer!
Scala presentatie
Scala presentatie
Scala presentatie
Scala presentatie
Scala presentatie

Scala presentatie