Download - SDC - Einführung in Scala
![Page 1: SDC - Einführung in Scala](https://reader033.vdocuments.site/reader033/viewer/2022061218/54b796a24a795912598b45a5/html5/thumbnails/1.jpg)
Einführung in ScalaSDC 2011 - Seitenbau Konstanz
Christian BaranowskiDennis Braunsdorf
![Page 2: SDC - Einführung in Scala](https://reader033.vdocuments.site/reader033/viewer/2022061218/54b796a24a795912598b45a5/html5/thumbnails/2.jpg)
Was ist Scala?
![Page 3: SDC - Einführung in Scala](https://reader033.vdocuments.site/reader033/viewer/2022061218/54b796a24a795912598b45a5/html5/thumbnails/3.jpg)
Scala a Scalable Language
![Page 4: SDC - Einführung in Scala](https://reader033.vdocuments.site/reader033/viewer/2022061218/54b796a24a795912598b45a5/html5/thumbnails/4.jpg)
Scala Bazaar
Scala = Bazaar
Java = Cathedral
“The Cathedral and the Bazaar” - Eric S. Raymond
![Page 5: SDC - Einführung in Scala](https://reader033.vdocuments.site/reader033/viewer/2022061218/54b796a24a795912598b45a5/html5/thumbnails/5.jpg)
Warum Scala?
![Page 6: SDC - Einführung in Scala](https://reader033.vdocuments.site/reader033/viewer/2022061218/54b796a24a795912598b45a5/html5/thumbnails/6.jpg)
Scala ist objekt-orientiert
![Page 7: SDC - Einführung in Scala](https://reader033.vdocuments.site/reader033/viewer/2022061218/54b796a24a795912598b45a5/html5/thumbnails/7.jpg)
Everything is a Object
![Page 8: SDC - Einführung in Scala](https://reader033.vdocuments.site/reader033/viewer/2022061218/54b796a24a795912598b45a5/html5/thumbnails/8.jpg)
Scala ist funktional
![Page 9: SDC - Einführung in Scala](https://reader033.vdocuments.site/reader033/viewer/2022061218/54b796a24a795912598b45a5/html5/thumbnails/9.jpg)
Scala ist statisch typisiert
![Page 10: SDC - Einführung in Scala](https://reader033.vdocuments.site/reader033/viewer/2022061218/54b796a24a795912598b45a5/html5/thumbnails/10.jpg)
Scala Basics
![Page 11: SDC - Einführung in Scala](https://reader033.vdocuments.site/reader033/viewer/2022061218/54b796a24a795912598b45a5/html5/thumbnails/11.jpg)
Variablen
var msg = "Hello SDC!"// Java CodeString msg = "Hello World";
val msg = "Hello SDC!“
// Java Codefinal String msg = "Hello World";
var msg = "Hello SDC!"// Java CodeString msg = "Hello World";
val msg = "Hello SDC!“
// Java Codefinal String msg = "Hello World";
![Page 12: SDC - Einführung in Scala](https://reader033.vdocuments.site/reader033/viewer/2022061218/54b796a24a795912598b45a5/html5/thumbnails/12.jpg)
Funktionen
def max(x : Int, y : Int) : Int = { if(x > y) { return x; } else { return y; }}
def max(x : Int, y : Int) : Int = { if(x > y) { return x; } else { return y; }}
![Page 13: SDC - Einführung in Scala](https://reader033.vdocuments.site/reader033/viewer/2022061218/54b796a24a795912598b45a5/html5/thumbnails/13.jpg)
Funktionen
def max(x : Int, y : Int) : Int = { if(x > y) { x } else { y }}
def max(x : Int, y : Int) : Int = { if(x > y) { x } else { y }}
![Page 14: SDC - Einführung in Scala](https://reader033.vdocuments.site/reader033/viewer/2022061218/54b796a24a795912598b45a5/html5/thumbnails/14.jpg)
Funktionen
def max(x : Int, y : Int) : Int = { if(x > y) x else y}
def max(x : Int, y : Int) : Int = { if(x > y) x else y}
![Page 15: SDC - Einführung in Scala](https://reader033.vdocuments.site/reader033/viewer/2022061218/54b796a24a795912598b45a5/html5/thumbnails/15.jpg)
Funktionen
def max(x : Int, y : Int) = { if(x > y) x else y}
def max(x : Int, y : Int) = { if(x > y) x else y}
![Page 16: SDC - Einführung in Scala](https://reader033.vdocuments.site/reader033/viewer/2022061218/54b796a24a795912598b45a5/html5/thumbnails/16.jpg)
Procedure
def printer(msg : String) { ...}
def printer(msg : String) { ...}
![Page 17: SDC - Einführung in Scala](https://reader033.vdocuments.site/reader033/viewer/2022061218/54b796a24a795912598b45a5/html5/thumbnails/17.jpg)
Funktionen Composition and Scoping
def max(x : Int, y : Int) = { def xGreater() = { x > y } if(xGreater()) x else y}
def max(x : Int, y : Int) = { def xGreater() = { x > y } if(xGreater()) x else y}
![Page 18: SDC - Einführung in Scala](https://reader033.vdocuments.site/reader033/viewer/2022061218/54b796a24a795912598b45a5/html5/thumbnails/18.jpg)
Funktionen Composition and Scoping
def max(x : Int, y : Int) = { def xGreater() = { x > y } if(xGreater) x else y}
def max(x : Int, y : Int) = { def xGreater() = { x > y } if(xGreater) x else y}
![Page 19: SDC - Einführung in Scala](https://reader033.vdocuments.site/reader033/viewer/2022061218/54b796a24a795912598b45a5/html5/thumbnails/19.jpg)
Functions are objects
def oncePerSecond(callback: () => Unit) {while (true) { callback(); Thread sleep 1000 }
}
def timeFlies() {println("time flies like an arrow...")
}
oncePerSecond(timeFlies)
def oncePerSecond(callback: () => Unit) {while (true) { callback(); Thread sleep 1000 }
}
def timeFlies() {println("time flies like an arrow...")
}
oncePerSecond(timeFlies)
![Page 20: SDC - Einführung in Scala](https://reader033.vdocuments.site/reader033/viewer/2022061218/54b796a24a795912598b45a5/html5/thumbnails/20.jpg)
Anonymous Functions
def oncePerSecond(callback: () => Unit) {while (true) { callback(); Thread sleep 1000 }
}
oncePerSecond(() => println("time flies like an arrow..."))
def oncePerSecond(callback: () => Unit) {while (true) { callback(); Thread sleep 1000 }
}
oncePerSecond(() => println("time flies like an arrow..."))
![Page 21: SDC - Einführung in Scala](https://reader033.vdocuments.site/reader033/viewer/2022061218/54b796a24a795912598b45a5/html5/thumbnails/21.jpg)
Arrays
val names = new Array[String](2)names(0) = "Dennis"names(1) = "Christian"
val names = new Array[String](2)names(0) = "Dennis"names(1) = "Christian"
![Page 22: SDC - Einführung in Scala](https://reader033.vdocuments.site/reader033/viewer/2022061218/54b796a24a795912598b45a5/html5/thumbnails/22.jpg)
Arrays
val names = Array("Dennis", "Christian")val names = Array("Dennis", "Christian")
![Page 23: SDC - Einführung in Scala](https://reader033.vdocuments.site/reader033/viewer/2022061218/54b796a24a795912598b45a5/html5/thumbnails/23.jpg)
Arrays
val names = Array("Dennis", "Christian", true, 0, 1.5)
val names : Array[Any] = Array("Dennis", "Christian", true, 0, 1.5)
val names = Array("Dennis", "Christian", true, 0, 1.5)
val names : Array[Any] = Array("Dennis", "Christian", true, 0, 1.5)
![Page 24: SDC - Einführung in Scala](https://reader033.vdocuments.site/reader033/viewer/2022061218/54b796a24a795912598b45a5/html5/thumbnails/24.jpg)
Listen
val oneTwo = List(1, 2)
val threeFour = List(3, 4)
val oneTwoThreeFour = oneTwo ::: threeFour
val oneTwoThree = 1 :: 2 :: 3 :: Nil
val oneTwo = List(1, 2)
val threeFour = List(3, 4)
val oneTwoThreeFour = oneTwo ::: threeFour
val oneTwoThree = 1 :: 2 :: 3 :: Nil
![Page 25: SDC - Einführung in Scala](https://reader033.vdocuments.site/reader033/viewer/2022061218/54b796a24a795912598b45a5/html5/thumbnails/25.jpg)
Tuples
val dennis = (1, "Dennis")
val christian = (2, "Christian")
val dennis = ("Dennis", "Braunsdorf")
println(dennis._1)println(dennis._2)
val dennis = (1, "Dennis")
val christian = (2, "Christian")
val dennis = ("Dennis", "Braunsdorf")
println(dennis._1)println(dennis._2)
![Page 26: SDC - Einführung in Scala](https://reader033.vdocuments.site/reader033/viewer/2022061218/54b796a24a795912598b45a5/html5/thumbnails/26.jpg)
Sets
![Page 27: SDC - Einführung in Scala](https://reader033.vdocuments.site/reader033/viewer/2022061218/54b796a24a795912598b45a5/html5/thumbnails/27.jpg)
Sets
import scala.collection.mutable.HashSet
val jetSet = new HashSet[String]jetSet += "Lear"jetSet += ("Boeing", "Airbus")println(jetSet.contains("Cessna"))
import scala.collection.mutable.HashSet
val jetSet = new HashSet[String]jetSet += "Lear"jetSet += ("Boeing", "Airbus")println(jetSet.contains("Cessna"))
![Page 28: SDC - Einführung in Scala](https://reader033.vdocuments.site/reader033/viewer/2022061218/54b796a24a795912598b45a5/html5/thumbnails/28.jpg)
Sets
val jetSet = Set("AirBus")
jetSet: scala.collection.immutable.Set[java.lang.String]
val jetSet = Set("AirBus")
jetSet: scala.collection.immutable.Set[java.lang.String]
![Page 29: SDC - Einführung in Scala](https://reader033.vdocuments.site/reader033/viewer/2022061218/54b796a24a795912598b45a5/html5/thumbnails/29.jpg)
Maps
![Page 30: SDC - Einführung in Scala](https://reader033.vdocuments.site/reader033/viewer/2022061218/54b796a24a795912598b45a5/html5/thumbnails/30.jpg)
Maps
import scala.collection.mutable.HashMap
val treasureMap = new HashMap[Int, String]treasureMap += 1 -> "Go to island."treasureMap += 2 -> "Find big X on ground."treasureMap += 3 -> "Dig."println(treasureMap(2))
import scala.collection.mutable.HashMap
val treasureMap = new HashMap[Int, String]treasureMap += 1 -> "Go to island."treasureMap += 2 -> "Find big X on ground."treasureMap += 3 -> "Dig."println(treasureMap(2))
![Page 31: SDC - Einführung in Scala](https://reader033.vdocuments.site/reader033/viewer/2022061218/54b796a24a795912598b45a5/html5/thumbnails/31.jpg)
Maps
val treasureMap = Map( (1, "Go to island."), (2, "Find big X on ground."), (3, "Dig."))
val treasureMap = Map( (1, "Go to island."), (2, "Find big X on ground."), (3, "Dig."))
![Page 32: SDC - Einführung in Scala](https://reader033.vdocuments.site/reader033/viewer/2022061218/54b796a24a795912598b45a5/html5/thumbnails/32.jpg)
While Schleife
val names = Array("Dennis", "Christian")var i = 0while(i < names.length) { println(names(i)) i = i + 1}
val names = Array("Dennis", "Christian")var i = 0while(i < names.length) { println(names(i)) i = i + 1}
![Page 33: SDC - Einführung in Scala](https://reader033.vdocuments.site/reader033/viewer/2022061218/54b796a24a795912598b45a5/html5/thumbnails/33.jpg)
For Schleife
val names = Array("Dennis", "Christian")for(name <- names) println(name)
val names = Array("Dennis", "Christian")for(name <- names) println(name)
![Page 34: SDC - Einführung in Scala](https://reader033.vdocuments.site/reader033/viewer/2022061218/54b796a24a795912598b45a5/html5/thumbnails/34.jpg)
Foreach Collection
val names = Array("Dennis", "Christian")names.foreach(name => println(name))val names = Array("Dennis", "Christian")names.foreach(name => println(name))
![Page 35: SDC - Einführung in Scala](https://reader033.vdocuments.site/reader033/viewer/2022061218/54b796a24a795912598b45a5/html5/thumbnails/35.jpg)
Throw Exceptions
def max(x : Int, y : Int) : Int = { if(x < 0) throw new Exception("Negativ Int!") ...}
def max(x : Int, y : Int) : Int = { if(x < 0) throw new Exception("Negativ Int!") ...}
![Page 36: SDC - Einführung in Scala](https://reader033.vdocuments.site/reader033/viewer/2022061218/54b796a24a795912598b45a5/html5/thumbnails/36.jpg)
Catch Exceptions
try { ... } catch { case ioe: IOException
=> println("Fehler beim lesen auf dem Filesystem")
case e: Exception => println("Unbekannter Fehler")
}
try { ... } catch { case ioe: IOException
=> println("Fehler beim lesen auf dem Filesystem")
case e: Exception => println("Unbekannter Fehler")
}
![Page 37: SDC - Einführung in Scala](https://reader033.vdocuments.site/reader033/viewer/2022061218/54b796a24a795912598b45a5/html5/thumbnails/37.jpg)
Scala OOP Basics
![Page 38: SDC - Einführung in Scala](https://reader033.vdocuments.site/reader033/viewer/2022061218/54b796a24a795912598b45a5/html5/thumbnails/38.jpg)
Everything is a Object
![Page 39: SDC - Einführung in Scala](https://reader033.vdocuments.site/reader033/viewer/2022061218/54b796a24a795912598b45a5/html5/thumbnails/39.jpg)
Numbers are Objects
1 + 2 * 3 / x = (1).+(((2).*(3))./(x))1 + 2 * 3 / x = (1).+(((2).*(3))./(x))
![Page 40: SDC - Einführung in Scala](https://reader033.vdocuments.site/reader033/viewer/2022061218/54b796a24a795912598b45a5/html5/thumbnails/40.jpg)
Objekte
object DemoService { def print(msg : String) { println(msg) } }
DemoService.print("Hello World")
object DemoService { def print(msg : String) { println(msg) } }
DemoService.print("Hello World")
![Page 41: SDC - Einführung in Scala](https://reader033.vdocuments.site/reader033/viewer/2022061218/54b796a24a795912598b45a5/html5/thumbnails/41.jpg)
Klassen
class Complex(real: Double, imaginary: Double) {def re() = realdef im() = imaginary
}
class Complex(real: Double, imaginary: Double) {def re() = realdef im() = imaginary
}
![Page 42: SDC - Einführung in Scala](https://reader033.vdocuments.site/reader033/viewer/2022061218/54b796a24a795912598b45a5/html5/thumbnails/42.jpg)
Klassen
class Complex(real: Double, imaginary: Double) {def re = realdef im = imaginary
}
class Complex(real: Double, imaginary: Double) {def re = realdef im = imaginary
}
![Page 43: SDC - Einführung in Scala](https://reader033.vdocuments.site/reader033/viewer/2022061218/54b796a24a795912598b45a5/html5/thumbnails/43.jpg)
Abstrakte Klassen
abstract class Complex(real: Double, imaginary: Double) {
abstract def re()def im = imaginary
}
abstract class Complex(real: Double, imaginary: Double) {
abstract def re()def im = imaginary
}
![Page 44: SDC - Einführung in Scala](https://reader033.vdocuments.site/reader033/viewer/2022061218/54b796a24a795912598b45a5/html5/thumbnails/44.jpg)
Klassen
protected class Complex(real: Double, imaginary: Double) {
private def re() = realprotected def im() = imaginary
}
protected class Complex(real: Double, imaginary: Double) {
private def re() = realprotected def im() = imaginary
}
![Page 45: SDC - Einführung in Scala](https://reader033.vdocuments.site/reader033/viewer/2022061218/54b796a24a795912598b45a5/html5/thumbnails/45.jpg)
Auxiliary Constructors
class Complex(real: Double, imaginary: Double) {
def this() = this(0,0)
def re() = realdef im() = imaginary
}
class Complex(real: Double, imaginary: Double) {
def this() = this(0,0)
def re() = realdef im() = imaginary
}
![Page 46: SDC - Einführung in Scala](https://reader033.vdocuments.site/reader033/viewer/2022061218/54b796a24a795912598b45a5/html5/thumbnails/46.jpg)
Packages und Imports
package demo
import javax._
import scala.collection.mutable.HashMap
package demo
import javax._
import scala.collection.mutable.HashMap
![Page 47: SDC - Einführung in Scala](https://reader033.vdocuments.site/reader033/viewer/2022061218/54b796a24a795912598b45a5/html5/thumbnails/47.jpg)
Scala Live DemoGetting Started ….
![Page 48: SDC - Einführung in Scala](https://reader033.vdocuments.site/reader033/viewer/2022061218/54b796a24a795912598b45a5/html5/thumbnails/48.jpg)
Scala OOP Teil 2
![Page 49: SDC - Einführung in Scala](https://reader033.vdocuments.site/reader033/viewer/2022061218/54b796a24a795912598b45a5/html5/thumbnails/49.jpg)
Traits
trait Ord {def < (that: Any): Booleandef <=(that: Any): Boolean
= (this < that) || (this == that)def > (that: Any): Boolean = !(this <= that)def >=(that: Any): Boolean = !(this < that)
}
trait Service
class MyOrd extends Ord with Service { def < (that: Any): Boolean = false }
trait Ord {def < (that: Any): Booleandef <=(that: Any): Boolean
= (this < that) || (this == that)def > (that: Any): Boolean = !(this <= that)def >=(that: Any): Boolean = !(this < that)
}
trait Service
class MyOrd extends Ord with Service { def < (that: Any): Boolean = false }
![Page 50: SDC - Einführung in Scala](https://reader033.vdocuments.site/reader033/viewer/2022061218/54b796a24a795912598b45a5/html5/thumbnails/50.jpg)
Operators + * - /
class Demo { def + (x : Int) = 1 + x}
var o1 = new MyOrdprintln(o1 + 1)
class Demo { def + (x : Int) = 1 + x}
var o1 = new MyOrdprintln(o1 + 1)
![Page 51: SDC - Einführung in Scala](https://reader033.vdocuments.site/reader033/viewer/2022061218/54b796a24a795912598b45a5/html5/thumbnails/51.jpg)
Methoden Overloading
class Demo { def + (x : Int) = 1 + x def + (x : Demo) = 1 + 1}
class Demo { def + (x : Int) = 1 + x def + (x : Demo) = 1 + 1}
![Page 52: SDC - Einführung in Scala](https://reader033.vdocuments.site/reader033/viewer/2022061218/54b796a24a795912598b45a5/html5/thumbnails/52.jpg)
Methoden Überschreiben
class Complex(real: Double, imaginary: Double) {
def re = realdef im = imaginaryoverride def toString() =
"" + re + (if (im < 0) "" else "+") + im + "i„
}
class Complex(real: Double, imaginary: Double) {
def re = realdef im = imaginaryoverride def toString() =
"" + re + (if (im < 0) "" else "+") + im + "i„
}
![Page 53: SDC - Einführung in Scala](https://reader033.vdocuments.site/reader033/viewer/2022061218/54b796a24a795912598b45a5/html5/thumbnails/53.jpg)
Case Klassen
abstract class Treecase class Sum(l: Tree, r: Tree) extends Treecase class Var(n: String) extends Treecase class Const(v: Int) extends Tree
abstract class Treecase class Sum(l: Tree, r: Tree) extends Treecase class Var(n: String) extends Treecase class Const(v: Int) extends Tree
![Page 54: SDC - Einführung in Scala](https://reader033.vdocuments.site/reader033/viewer/2022061218/54b796a24a795912598b45a5/html5/thumbnails/54.jpg)
Pattern Matching
def eval(t: Tree, env: Environment): Int = t match {case Sum(l, r) => eval(l, env) + eval(r, env)case Var(n) => env(n)case Const(v) => v
}
def derive(t: Tree, v: String): Tree = t match {case Sum(l, r) => Sum(derive(l, v), derive(r, v))case Var(n) if (v == n) => Const(1)case _ => Const(0)
}
val exp: Tree = Sum(Sum(Var("x"),Var("x")),Sum(Const(7),Var("y")))val env: Environment = { case "x" => 5 case "y" => 7 }println("Expression: " + exp)println("Evaluation with x=5, y=7: " + eval(exp, env))println("Derivative relative to x:\n " + derive(exp, "x"))println("Derivative relative to y:\n " + derive(exp, "y"))
def eval(t: Tree, env: Environment): Int = t match {case Sum(l, r) => eval(l, env) + eval(r, env)case Var(n) => env(n)case Const(v) => v
}
def derive(t: Tree, v: String): Tree = t match {case Sum(l, r) => Sum(derive(l, v), derive(r, v))case Var(n) if (v == n) => Const(1)case _ => Const(0)
}
val exp: Tree = Sum(Sum(Var("x"),Var("x")),Sum(Const(7),Var("y")))val env: Environment = { case "x" => 5 case "y" => 7 }println("Expression: " + exp)println("Evaluation with x=5, y=7: " + eval(exp, env))println("Derivative relative to x:\n " + derive(exp, "x"))println("Derivative relative to y:\n " + derive(exp, "y"))
![Page 55: SDC - Einführung in Scala](https://reader033.vdocuments.site/reader033/viewer/2022061218/54b796a24a795912598b45a5/html5/thumbnails/55.jpg)
Advanced Features
![Page 56: SDC - Einführung in Scala](https://reader033.vdocuments.site/reader033/viewer/2022061218/54b796a24a795912598b45a5/html5/thumbnails/56.jpg)
For-Comprehensions
for (p <- persons if p.age > 20) yield p.name
def queens(n: Int): List[List[Int]] = { def placeQueens(k: Int): List[List[Int]] = if (k == 0) List(List()) else for { queens <- placeQueens(k - 1) column <- List.range(1, n + 1) if isSafe(column, queens, 1) } yield column :: queens placeQueens(n)}
def isSafe(col: Int, queens: List[Int], delta: Int): Boolean
for (b <- books; a <- b.authors if a startsWith "Ullman") yield b.title
for (p <- persons if p.age > 20) yield p.name
def queens(n: Int): List[List[Int]] = { def placeQueens(k: Int): List[List[Int]] = if (k == 0) List(List()) else for { queens <- placeQueens(k - 1) column <- List.range(1, n + 1) if isSafe(column, queens, 1) } yield column :: queens placeQueens(n)}
def isSafe(col: Int, queens: List[Int], delta: Int): Boolean
for (b <- books; a <- b.authors if a startsWith "Ullman") yield b.title
![Page 57: SDC - Einführung in Scala](https://reader033.vdocuments.site/reader033/viewer/2022061218/54b796a24a795912598b45a5/html5/thumbnails/57.jpg)
Genericity
class Reference[T] { private var contents: T = _ def set(value: T) { contents = value } def get: T = contents}
object IntegerReference { def main(args: Array[String]) { val cell = new Reference[Int] cell.set(13) println("Reference contains the half of "
+ (cell.get * 2)) }}
class Reference[T] { private var contents: T = _ def set(value: T) { contents = value } def get: T = contents}
object IntegerReference { def main(args: Array[String]) { val cell = new Reference[Int] cell.set(13) println("Reference contains the half of "
+ (cell.get * 2)) }}
![Page 58: SDC - Einführung in Scala](https://reader033.vdocuments.site/reader033/viewer/2022061218/54b796a24a795912598b45a5/html5/thumbnails/58.jpg)
Generic Stack
abstract class Stack[A] { def push(x: A): Stack[A] = new NonEmptyStack[A](x, this) def isEmpty: Boolean def top: A def pop: Stack[A]}class EmptyStack[A] extends Stack[A] { def isEmpty = true def top = error("EmptyStack.top") def pop = error("EmptyStack.pop")}class NonEmptyStack[A](elem: A, rest: Stack[A]) extends Stack[A] { def isEmpty = false def top = elem def pop = rest}
abstract class Stack[A] { def push(x: A): Stack[A] = new NonEmptyStack[A](x, this) def isEmpty: Boolean def top: A def pop: Stack[A]}class EmptyStack[A] extends Stack[A] { def isEmpty = true def top = error("EmptyStack.top") def pop = error("EmptyStack.pop")}class NonEmptyStack[A](elem: A, rest: Stack[A]) extends Stack[A] { def isEmpty = false def top = elem def pop = rest}
![Page 59: SDC - Einführung in Scala](https://reader033.vdocuments.site/reader033/viewer/2022061218/54b796a24a795912598b45a5/html5/thumbnails/59.jpg)
Lazy Values
case class Employee(id: Int, name: String, managerId: Int) { lazy val manager: Employee = Db.get(managerId) lazy val team: List[Employee] = Db.team(id)}
case class Employee(id: Int, name: String, managerId: Int) { lazy val manager: Employee = Db.get(managerId) lazy val team: List[Employee] = Db.team(id)}
![Page 60: SDC - Einführung in Scala](https://reader033.vdocuments.site/reader033/viewer/2022061218/54b796a24a795912598b45a5/html5/thumbnails/60.jpg)
Implicit Parameters
implicit object stringMonoid extends Monoid[String] { def add(x: String, y: String): String = x.concat(y) def unit: String = ""}
implicit object intMonoid extends Monoid[Int] { def add(x: Int, y: Int): Int = x + y def unit: Int = 0}
implicit object stringMonoid extends Monoid[String] { def add(x: String, y: String): String = x.concat(y) def unit: String = ""}
implicit object intMonoid extends Monoid[Int] { def add(x: Int, y: Int): Int = x + y def unit: Int = 0}
![Page 61: SDC - Einführung in Scala](https://reader033.vdocuments.site/reader033/viewer/2022061218/54b796a24a795912598b45a5/html5/thumbnails/61.jpg)
Implicit Conversions
implicit def int2ordered(x: Int): Ordered[Int] = new Ordered[Int] { def compare(y: Int): Int = if (x < y) -1 else if (x > y) 1 else 0}
implicit def int2ordered(x: Int): Ordered[Int] = new Ordered[Int] { def compare(y: Int): Int = if (x < y) -1 else if (x > y) 1 else 0}
![Page 62: SDC - Einführung in Scala](https://reader033.vdocuments.site/reader033/viewer/2022061218/54b796a24a795912598b45a5/html5/thumbnails/62.jpg)
Annotations
@fieldclass BeanProperty extends annotation.StaticAnnotation@fieldclass BeanProperty extends annotation.StaticAnnotation
![Page 63: SDC - Einführung in Scala](https://reader033.vdocuments.site/reader033/viewer/2022061218/54b796a24a795912598b45a5/html5/thumbnails/63.jpg)
Scala Java Integration// Java public class MainJava { public static void main(String[] args) {
Point point = new Point();point.name();
}}
// Scalaclass Point(x : Int, y : Int) { var name = "Bin ein Point" def this() = this(0, 0) override def toString = name + " : x=" + x + " : y=" + y
}
![Page 64: SDC - Einführung in Scala](https://reader033.vdocuments.site/reader033/viewer/2022061218/54b796a24a795912598b45a5/html5/thumbnails/64.jpg)
Package Objects
package object demo { implicit def toPoint(name: String) = new Point}
package object demo { implicit def toPoint(name: String) = new Point}
![Page 65: SDC - Einführung in Scala](https://reader033.vdocuments.site/reader033/viewer/2022061218/54b796a24a795912598b45a5/html5/thumbnails/65.jpg)
Scala in Action
![Page 66: SDC - Einführung in Scala](https://reader033.vdocuments.site/reader033/viewer/2022061218/54b796a24a795912598b45a5/html5/thumbnails/66.jpg)
Java Beans mit Scala
class PersonBean {
@scala.reflect.BeanProperty var name : String = "" }
![Page 67: SDC - Einführung in Scala](https://reader033.vdocuments.site/reader033/viewer/2022061218/54b796a24a795912598b45a5/html5/thumbnails/67.jpg)
JUnit Tests with Scalaimport org.junit._import org.junit.Assert._;
class MaxTest {
var max : MathUtils = null; @Before def setup(){ max = new MathUtils } @Test def max_fiveGreaterThenFour() { assertEquals(5, max.max(5, 4)); } }
![Page 68: SDC - Einführung in Scala](https://reader033.vdocuments.site/reader033/viewer/2022061218/54b796a24a795912598b45a5/html5/thumbnails/68.jpg)
Scala als DSL Toolkit
![Page 69: SDC - Einführung in Scala](https://reader033.vdocuments.site/reader033/viewer/2022061218/54b796a24a795912598b45a5/html5/thumbnails/69.jpg)
Internal DSLs mit Scala
![Page 70: SDC - Einführung in Scala](https://reader033.vdocuments.site/reader033/viewer/2022061218/54b796a24a795912598b45a5/html5/thumbnails/70.jpg)
Robot DSLobject DemoRobot extends RobotProgram with Application {
000 PRINT "Lunar Mars Program starts." 001 MOVE(1, 10) 002 SLEEP2000 003 MOVE(10, 10) 004 PRINT"POSITION Now 10, 10" 005 MOVE(20, 20) 006 END RUN
}
![Page 71: SDC - Einführung in Scala](https://reader033.vdocuments.site/reader033/viewer/2022061218/54b796a24a795912598b45a5/html5/thumbnails/71.jpg)
External DSLs mit Scala
![Page 72: SDC - Einführung in Scala](https://reader033.vdocuments.site/reader033/viewer/2022061218/54b796a24a795912598b45a5/html5/thumbnails/72.jpg)
Fragen ?
![Page 73: SDC - Einführung in Scala](https://reader033.vdocuments.site/reader033/viewer/2022061218/54b796a24a795912598b45a5/html5/thumbnails/73.jpg)
References
• Scala – http://www.scala-lang.org/• Scala IDE - http://www.scala-ide.org/• Scala Code Snippets
http://www.scala-lang.org/node/220• Scala By Example
http://www.scala-lang.org/docu/files/ScalaByExample.pdf
• Scala Tutorialhttp://www.scala-lang.org/docu/files/ScalaTutorial.pdf
• Scala Actors - http://www.scala-lang.org/node/242
• Lift http://liftweb.net