scala presentatie

54
Scala Introductie voor Java (7) kenners

Upload: gedejong

Post on 05-Jul-2015

47 views

Category:

Engineering


0 download

DESCRIPTION

Short (1hr) introduction to Scala Programming given to Topicus at 23rd of september, 2014. Accompanied by github projects: https://github.com/gedejong/overpascala https://github.com/gedejong/pollardRho

TRANSCRIPT

Page 1: Scala presentatie

ScalaIntroductie voor Java (7) kenners

Page 3: Scala presentatie

Martin Oderski

Page 4: Scala presentatie
Page 5: Scala presentatie
Page 6: Scala presentatie

• Statically typed

Page 7: Scala presentatie

• Statically typed

• Type inference

Page 8: Scala presentatie

• Statically typed

• Type inference

• Functional / OOP

Page 9: Scala presentatie

• Statically typed

• Type inference

• Functional / OOP

• Compiled naar JVM bytecode

Page 10: Scala presentatie

• Statically typed

• Type inference

• Functional / OOP

• Compiled naar JVM bytecode

• én .NET

Page 11: Scala presentatie

• Statically typed

• Type inference

• Functional / OOP

• Compiled naar JVM bytecode

• én .NET

• én Javascript (scalajs)

Page 12: Scala presentatie

Static vs. Dynamic

Page 13: Scala presentatie

Type chaos

Page 14: Scala presentatie

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;

}

Page 15: Scala presentatie

Implicit types

Page 16: Scala presentatie

Implicit types

def keysAsList[K, V](m: Map[K, V]) =m.keySet.toList

Page 17: Scala presentatie

Alles is een object

Page 18: Scala presentatie

Overpass Query Model

Page 19: Scala presentatie

Overpass Query Model

Node

Page 20: Scala presentatie

Overpass Query Model

Node

Way

Page 21: Scala presentatie

Overpass Query Model

Node

Relation

Way

Page 22: Scala presentatie

node["addr:city"="Deventer"]["addr:housenumber"="25"]["addr:street"="Singel"];out

Page 23: Scala presentatie

node["addr:city"="Deventer"]["addr:housenumber"="25"]["addr:street"="Singel"];out

Node( "addr:city" === "Deventer" and "addr:housenumber" === "25" and "addr:street" === "Singel") | OutStatement

Page 24: Scala presentatie

node(2678759904);way(around: 1000);(rel(bw)["route"="bicycle"]["type"="route"];way(r);node(w););out

Page 25: Scala presentatie

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)

Page 26: Scala presentatie

sealed trait Statement

Page 27: Scala presentatie

sealed trait Statement!case object OutStatement extends Statement

Page 28: Scala presentatie

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) }

Page 29: Scala presentatie

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

Page 30: Scala presentatie

Pimp my types

Page 31: Scala presentatie

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) }

Page 32: Scala presentatie

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”)

Page 33: Scala presentatie

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”)

Page 34: Scala presentatie

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”)

Page 35: Scala presentatie

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”)

Page 36: Scala presentatie

Pattern matchingdef 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"]"""

… }

Page 37: Scala presentatie

Map, flatmap en filter

Page 38: Scala presentatie

Type-safe DSLs

Page 39: Scala presentatie

Type-safe DSLs

val avg: Option[Float] = from(grades)(g => where(g.subjectId === mathId) compute(avg(g.scoreInPercentage)) )

Page 40: Scala presentatie

Running Demo: OpenStreetMaps en

Buienradar

Page 41: Scala presentatie

Buienradar APIhttp://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

Page 42: Scala presentatie
Page 43: Scala presentatie
Page 44: Scala presentatie

• Actors

Page 45: Scala presentatie

• Actors

• Scalaz

Page 46: Scala presentatie

• Actors

• Scalaz

• Higher-order types

Page 47: Scala presentatie

• Actors

• Scalaz

• Higher-order types

• Parser Combinators

Page 48: Scala presentatie

• Actors

• Scalaz

• Higher-order types

• Parser Combinators

• ScalaTest / ScalaCheck

Page 49: Scala presentatie

Ik wil meer!

Page 50: Scala presentatie
Page 51: Scala presentatie
Page 52: Scala presentatie
Page 53: Scala presentatie
Page 54: Scala presentatie