SlideShare a Scribd company logo
Einführung   in Scala SDC 2011 - Seitenbau Konstanz Christian Baranowski Dennis Braunsdorf
Was ist Scala?
Scala  a Scalable  Language
Scala  Bazaar  Scala = Bazaar Java = Cathedral “ The Cathedral and the Bazaar” - Eric S. Raymond
Warum Scala?
Scala ist objekt-orientiert
Everything is a Object
Scala ist funktional
Scala ist statisch typisiert
Scala Basics
Variablen var  msg =  "Hello SDC!" // Java Code String  msg  =  "Hello World" ; val  msg =  "Hello SDC!“ // Java Code final  String  msg  =  "Hello World" ;
Funktionen def  max(x : Int, y : Int) : Int = { if (x > y) { return  x; } else  { return  y; } }
Funktionen def  max(x : Int, y : Int) : Int = { if (x > y) { x } else  { y } }
Funktionen def  max(x : Int, y : Int) : Int = { if (x > y) x else  y }
Funktionen def  max(x : Int, y : Int) = { if (x > y) x else  y }
Procedure def  printer(msg : String) { ... }
Funktionen Composition and Scoping def  max(x : Int, y : Int) = { def  xGreater() = { x > y  } if (xGreater()) x else  y }
Funktionen Composition and Scoping def  max(x : Int, y : Int) = { def  xGreater() = { x > y  } if (xGreater) x else  y }
Functions are objects def  oncePerSecond(callback: () => Unit) { while  ( true ) { callback(); Thread sleep 1000 } } def  timeFlies() { println( "time flies like an arrow..." ) } oncePerSecond(timeFlies)
Anonymous Functions def  oncePerSecond(callback: () => Unit) { while  ( true ) { callback(); Thread sleep 1000 } } oncePerSecond(() =>  println( "time flies like an arrow..." ))
Arrays val  names =  new  Array[String](2) names(0) =  "Dennis" names(1) =  "Christian"
Arrays val  names = Array( "Dennis" ,  "Christian" )
Arrays val  names = Array( "Dennis" ,  "Christian" ,  true , 0, 1.5) val  names : Array[Any]  = Array( "Dennis" ,  "Christian" ,    true , 0, 1.5)
Listen val  oneTwo = List(1, 2) val  threeFour = List(3, 4) val  oneTwoThreeFour = oneTwo ::: threeFour val  oneTwoThree = 1 :: 2 :: 3 :: Nil
Tuples val  dennis = (1,  "Dennis" ) val  christian = (2,  "Christian" ) val  dennis = ( "Dennis" ,  "Braunsdorf" ) println(dennis._1) println(dennis._2)
Sets
Sets import  scala.collection.mutable.HashSet val  jetSet =  new  HashSet[String] jetSet +=  "Lear" jetSet += ( "Boeing" ,  "Airbus" ) println(jetSet.contains( "Cessna" ))
Sets val  jetSet = Set( "AirBus" ) jetSet: scala.collection.immutable.Set[java.lang.String]
Maps
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))
Maps val  treasureMap = Map( (1,  "Go to island." ),  (2,  "Find big X on ground." ),  (3,  "Dig." ) )
While Schleife val  names = Array( &quot;Dennis&quot; ,  &quot;Christian&quot; ) var  i = 0 while (i < names.length) { println(names(i)) i = i + 1 }
For Schleife val  names = Array( &quot;Dennis&quot; ,  &quot;Christian&quot; ) for (name <- names) println(name)
Foreach Collection val  names = Array( &quot;Dennis&quot; ,  &quot;Christian&quot; ) names.foreach(name => println(name))
Throw Exceptions def  max(x : Int, y : Int) : Int = { if (x < 0) throw   new  Exception( &quot;Negativ Int!&quot; ) ... }
Catch Exceptions try  {  ...  }  catch  { case  ioe: IOException  => println( &quot;Fehler beim  lesen auf dem Filesystem&quot; ) case  e: Exception  => println( &quot;Unbekannter Fehler&quot; ) }
Scala OOP Basics
Everything is a Object
Numbers are Objects 1 + 2 * 3 / x = (1).+(((2).*(3))./(x))
Objekte object  DemoService { def  print(msg : String) { println(msg) } } DemoService.print( &quot;Hello World&quot; )
Klassen class  Complex(real: Double, imaginary: Double) { def  re() = real def  im() = imaginary }
Klassen class  Complex(real: Double, imaginary: Double) { def  re = real def  im = imaginary }
Abstrakte Klassen abstract class  Complex(real: Double,  imaginary: Double) { abstract def  re() def  im = imaginary }
Klassen protected class  Complex(real: Double,  imaginary: Double) { private   def  re() = real protected   def  im() = imaginary }
Auxiliary   Constructors class  Complex(real: Double,  imaginary: Double) { def   this () =  this (0,0) def  re() = real def  im() = imaginary }
Packages und Imports package  demo import  javax._ import  scala.collection.mutable.HashMap
Scala Live Demo Getting Started ….
Scala OOP Teil 2
Traits trait  Ord { def  < (that: Any): Boolean def  <=(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   }
Operators + * - /  class  Demo { def  + (x : Int) = 1 + x } var  o1 =  new  MyOrd println(o1 + 1)
Methoden Overloading class  Demo { def  + (x : Int) = 1 + x def  + (x : Demo) = 1 + 1 }
Methoden Überschreiben class  Complex(real: Double, imaginary: Double) { def  re = real def  im = imaginary override   def  toString() =  &quot;&quot;  + re + ( if  (im < 0)  &quot;&quot;   else   &quot;+&quot; ) + im +  &quot;i„ }
Case Klassen abstract   class  Tree case   class  Sum(l: Tree, r: Tree)  extends  Tree case   class  Var(n: String)  extends  Tree case   class  Const(v: Int)  extends  Tree
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( &quot;x&quot; ),Var( &quot;x&quot; )),Sum(Const(7),Var( &quot;y&quot; ))) val  env: Environment = {  case   &quot;x&quot;  => 5  case   &quot;y&quot;  => 7 } println( &quot;Expression: &quot;  + exp) println( &quot;Evaluation with x=5, y=7: &quot;  + eval(exp, env)) println( &quot;Derivative relative to x: &quot;  + derive(exp,  &quot;x&quot; )) println( &quot;Derivative relative to y: &quot;  + derive(exp,  &quot;y&quot; ))
Advanced  Features
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  &quot;Ullman&quot; ) yield  b.title
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( &quot;Reference contains the half of &quot;   + (cell.get * 2)) } }
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( &quot;EmptyStack.top&quot; ) def  pop = error( &quot;EmptyStack.pop&quot; ) } class  NonEmptyStack[A](elem: A, rest: Stack[A])  extends  Stack[A] { def  isEmpty =  false def  top = elem def  pop = rest }
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) }
Implicit Parameters implicit   object  stringMonoid  extends  Monoid[String] { def  add(x: String, y: String): String = x.concat(y) def  unit: String =  &quot;&quot; } implicit   object  intMonoid  extends  Monoid[Int] { def  add(x: Int, y: Int): Int = x + y def  unit: Int = 0 }
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 }
Annotations @field class  BeanProperty  extends  annotation.StaticAnnotation
Scala Java Integration // Java  public   class  MainJava { public   static   void  main(String[] args) { Point point =  new  Point(); point.name(); } } // Scala class  Point(x : Int, y : Int) { var  name =  &quot;Bin ein Point&quot; def   this () =  this (0, 0) override   def  toString = name +  &quot; : x=&quot;  + x +  &quot; : y=&quot;  + y }
Package Objects package   object  demo { implicit   def  toPoint(name: String) =  new  Point }
Scala in Action
Java Beans mit Scala class  PersonBean { @scala.reflect.BeanProperty var  name : String  =  &quot;&quot; }
JUnit Tests with Scala import  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)); } }
Scala als DSL Toolkit
Internal DSLs mit Scala
Robot DSL object  DemoRobot  extends  RobotProgram  with  Application { 000 PRINT  &quot;Lunar Mars Program starts.&quot; 001 MOVE(1, 10) 002 SLEEP2000 003 MOVE(10, 10) 004 PRINT &quot;POSITION Now 10, 10&quot; 005 MOVE(20, 20) 006 END RUN }
External DSLs mit Scala
Fragen ?
References ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]

More Related Content

What's hot

From Java to Kotlin beyond alt+shift+cmd+k - Kotlin Community Conf Milan
From Java to Kotlin beyond alt+shift+cmd+k - Kotlin Community Conf MilanFrom Java to Kotlin beyond alt+shift+cmd+k - Kotlin Community Conf Milan
From Java to Kotlin beyond alt+shift+cmd+k - Kotlin Community Conf Milan
Fabio Collini
 
Async code on kotlin: rx java or/and coroutines - Kotlin Night Turin
Async code on kotlin: rx java or/and coroutines - Kotlin Night TurinAsync code on kotlin: rx java or/and coroutines - Kotlin Night Turin
Async code on kotlin: rx java or/and coroutines - Kotlin Night Turin
Fabio Collini
 
From java to kotlin beyond alt+shift+cmd+k - Droidcon italy
From java to kotlin beyond alt+shift+cmd+k - Droidcon italyFrom java to kotlin beyond alt+shift+cmd+k - Droidcon italy
From java to kotlin beyond alt+shift+cmd+k - Droidcon italy
Fabio Collini
 
A tour of Python
A tour of PythonA tour of Python
A tour of Python
Aleksandar Veselinovic
 
Scala best practices
Scala best practicesScala best practices
Scala best practices
Alexander Zaidel
 
How to Clone Flappy Bird in Swift
How to Clone Flappy Bird in SwiftHow to Clone Flappy Bird in Swift
How to Clone Flappy Bird in SwiftGiordano Scalzo
 
λ | Lenses
λ | Lensesλ | Lenses
λ | Lenses
Open-IT
 
The Macronomicon
The MacronomiconThe Macronomicon
The Macronomicon
Mike Fogus
 
Building fast interpreters in Rust
Building fast interpreters in RustBuilding fast interpreters in Rust
Building fast interpreters in Rust
Ingvar Stepanyan
 
Kotlin Basics - Apalon Kotlin Sprint Part 2
Kotlin Basics - Apalon Kotlin Sprint Part 2Kotlin Basics - Apalon Kotlin Sprint Part 2
Kotlin Basics - Apalon Kotlin Sprint Part 2
Kirill Rozov
 
ddd+scala
ddd+scaladdd+scala
ddd+scala
潤一 加藤
 
Scala vs Java 8 in a Java 8 World
Scala vs Java 8 in a Java 8 WorldScala vs Java 8 in a Java 8 World
Scala vs Java 8 in a Java 8 World
BTI360
 
Grammarware Memes
Grammarware MemesGrammarware Memes
Grammarware Memes
Eelco Visser
 
Atomically { Delete Your Actors }
Atomically { Delete Your Actors }Atomically { Delete Your Actors }
Atomically { Delete Your Actors }
John De Goes
 
Introduction to Python
Introduction to PythonIntroduction to Python
Introduction to Python
UC San Diego
 
Naïveté vs. Experience
Naïveté vs. ExperienceNaïveté vs. Experience
Naïveté vs. Experience
Mike Fogus
 
Beyond Scala Lens
Beyond Scala LensBeyond Scala Lens
Beyond Scala Lens
Julien Truffaut
 
Scala in practice
Scala in practiceScala in practice
Scala in practice
andyrobinson8
 
Pyimproved again
Pyimproved againPyimproved again
Pyimproved again
rik0
 
Concurrent Application Development using Scala
Concurrent Application Development using ScalaConcurrent Application Development using Scala
Concurrent Application Development using Scala
Siarhiej Siemianchuk
 

What's hot (20)

From Java to Kotlin beyond alt+shift+cmd+k - Kotlin Community Conf Milan
From Java to Kotlin beyond alt+shift+cmd+k - Kotlin Community Conf MilanFrom Java to Kotlin beyond alt+shift+cmd+k - Kotlin Community Conf Milan
From Java to Kotlin beyond alt+shift+cmd+k - Kotlin Community Conf Milan
 
Async code on kotlin: rx java or/and coroutines - Kotlin Night Turin
Async code on kotlin: rx java or/and coroutines - Kotlin Night TurinAsync code on kotlin: rx java or/and coroutines - Kotlin Night Turin
Async code on kotlin: rx java or/and coroutines - Kotlin Night Turin
 
From java to kotlin beyond alt+shift+cmd+k - Droidcon italy
From java to kotlin beyond alt+shift+cmd+k - Droidcon italyFrom java to kotlin beyond alt+shift+cmd+k - Droidcon italy
From java to kotlin beyond alt+shift+cmd+k - Droidcon italy
 
A tour of Python
A tour of PythonA tour of Python
A tour of Python
 
Scala best practices
Scala best practicesScala best practices
Scala best practices
 
How to Clone Flappy Bird in Swift
How to Clone Flappy Bird in SwiftHow to Clone Flappy Bird in Swift
How to Clone Flappy Bird in Swift
 
λ | Lenses
λ | Lensesλ | Lenses
λ | Lenses
 
The Macronomicon
The MacronomiconThe Macronomicon
The Macronomicon
 
Building fast interpreters in Rust
Building fast interpreters in RustBuilding fast interpreters in Rust
Building fast interpreters in Rust
 
Kotlin Basics - Apalon Kotlin Sprint Part 2
Kotlin Basics - Apalon Kotlin Sprint Part 2Kotlin Basics - Apalon Kotlin Sprint Part 2
Kotlin Basics - Apalon Kotlin Sprint Part 2
 
ddd+scala
ddd+scaladdd+scala
ddd+scala
 
Scala vs Java 8 in a Java 8 World
Scala vs Java 8 in a Java 8 WorldScala vs Java 8 in a Java 8 World
Scala vs Java 8 in a Java 8 World
 
Grammarware Memes
Grammarware MemesGrammarware Memes
Grammarware Memes
 
Atomically { Delete Your Actors }
Atomically { Delete Your Actors }Atomically { Delete Your Actors }
Atomically { Delete Your Actors }
 
Introduction to Python
Introduction to PythonIntroduction to Python
Introduction to Python
 
Naïveté vs. Experience
Naïveté vs. ExperienceNaïveté vs. Experience
Naïveté vs. Experience
 
Beyond Scala Lens
Beyond Scala LensBeyond Scala Lens
Beyond Scala Lens
 
Scala in practice
Scala in practiceScala in practice
Scala in practice
 
Pyimproved again
Pyimproved againPyimproved again
Pyimproved again
 
Concurrent Application Development using Scala
Concurrent Application Development using ScalaConcurrent Application Development using Scala
Concurrent Application Development using Scala
 

Similar to SDC - Einführung in Scala

Scala - where objects and functions meet
Scala - where objects and functions meetScala - where objects and functions meet
Scala - where objects and functions meetMario Fusco
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfHiroshi Ono
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfHiroshi Ono
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfHiroshi Ono
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfHiroshi Ono
 
Pragmatic Real-World Scala
Pragmatic Real-World ScalaPragmatic Real-World Scala
Pragmatic Real-World Scalaparag978978
 
Pragmatic Real-World Scala (short version)
Pragmatic Real-World Scala (short version)Pragmatic Real-World Scala (short version)
Pragmatic Real-World Scala (short version)
Jonas Bonér
 
JBUG 11 - Scala For Java Programmers
JBUG 11 - Scala For Java ProgrammersJBUG 11 - Scala For Java Programmers
JBUG 11 - Scala For Java Programmers
Tikal Knowledge
 
Scala - brief intro
Scala - brief introScala - brief intro
Scala - brief intro
Razvan Cojocaru
 
Rewriting Java In Scala
Rewriting Java In ScalaRewriting Java In Scala
Rewriting Java In Scala
Skills Matter
 
Scala presentation by Aleksandar Prokopec
Scala presentation by Aleksandar ProkopecScala presentation by Aleksandar Prokopec
Scala presentation by Aleksandar ProkopecLoïc Descotte
 
여자개발자모임터 6주년 개발 세미나 - Scala Language
여자개발자모임터 6주년 개발 세미나 - Scala Language여자개발자모임터 6주년 개발 세미나 - Scala Language
여자개발자모임터 6주년 개발 세미나 - Scala Language
Ashal aka JOKER
 
TypeScript Introduction
TypeScript IntroductionTypeScript Introduction
TypeScript Introduction
Dmitry Sheiko
 
(How) can we benefit from adopting scala?
(How) can we benefit from adopting scala?(How) can we benefit from adopting scala?
(How) can we benefit from adopting scala?Tomasz Wrobel
 
Monadologie
MonadologieMonadologie
Monadologie
league
 
From Java to Scala - advantages and possible risks
From Java to Scala - advantages and possible risksFrom Java to Scala - advantages and possible risks
From Java to Scala - advantages and possible risks
SeniorDevOnly
 
Scala introduction
Scala introductionScala introduction
Scala introduction
Yardena Meymann
 
EcmaScript unchained
EcmaScript unchainedEcmaScript unchained
EcmaScript unchained
Eduard Tomàs
 

Similar to SDC - Einführung in Scala (20)

Scala 2 + 2 > 4
Scala 2 + 2 > 4Scala 2 + 2 > 4
Scala 2 + 2 > 4
 
Scala - where objects and functions meet
Scala - where objects and functions meetScala - where objects and functions meet
Scala - where objects and functions meet
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
 
Pragmatic Real-World Scala
Pragmatic Real-World ScalaPragmatic Real-World Scala
Pragmatic Real-World Scala
 
Pragmatic Real-World Scala (short version)
Pragmatic Real-World Scala (short version)Pragmatic Real-World Scala (short version)
Pragmatic Real-World Scala (short version)
 
JBUG 11 - Scala For Java Programmers
JBUG 11 - Scala For Java ProgrammersJBUG 11 - Scala For Java Programmers
JBUG 11 - Scala For Java Programmers
 
Scala - brief intro
Scala - brief introScala - brief intro
Scala - brief intro
 
Rewriting Java In Scala
Rewriting Java In ScalaRewriting Java In Scala
Rewriting Java In Scala
 
Scala introduction
Scala introductionScala introduction
Scala introduction
 
Scala presentation by Aleksandar Prokopec
Scala presentation by Aleksandar ProkopecScala presentation by Aleksandar Prokopec
Scala presentation by Aleksandar Prokopec
 
여자개발자모임터 6주년 개발 세미나 - Scala Language
여자개발자모임터 6주년 개발 세미나 - Scala Language여자개발자모임터 6주년 개발 세미나 - Scala Language
여자개발자모임터 6주년 개발 세미나 - Scala Language
 
TypeScript Introduction
TypeScript IntroductionTypeScript Introduction
TypeScript Introduction
 
(How) can we benefit from adopting scala?
(How) can we benefit from adopting scala?(How) can we benefit from adopting scala?
(How) can we benefit from adopting scala?
 
Monadologie
MonadologieMonadologie
Monadologie
 
From Java to Scala - advantages and possible risks
From Java to Scala - advantages and possible risksFrom Java to Scala - advantages and possible risks
From Java to Scala - advantages and possible risks
 
Scala introduction
Scala introductionScala introduction
Scala introduction
 
EcmaScript unchained
EcmaScript unchainedEcmaScript unchained
EcmaScript unchained
 

More from Christian Baranowski

Microservices – die Architektur für Agile-Entwicklung?
Microservices – die Architektur für Agile-Entwicklung?Microservices – die Architektur für Agile-Entwicklung?
Microservices – die Architektur für Agile-Entwicklung?
Christian Baranowski
 
OSGi and Spring Data for simple (Web) Application Development
OSGi and Spring Data  for simple (Web) Application DevelopmentOSGi and Spring Data  for simple (Web) Application Development
OSGi and Spring Data for simple (Web) Application Development
Christian Baranowski
 
BDD - Behavior Driven Development Webapps mit Groovy Spock und Geb
BDD - Behavior Driven Development Webapps mit Groovy Spock und GebBDD - Behavior Driven Development Webapps mit Groovy Spock und Geb
BDD - Behavior Driven Development Webapps mit Groovy Spock und Geb
Christian Baranowski
 
Komponententests und Testabdeckung
Komponententests und TestabdeckungKomponententests und Testabdeckung
Komponententests und Testabdeckung
Christian Baranowski
 
Einführung in die Software-Qualitätssicherung
Einführung in die Software-QualitätssicherungEinführung in die Software-Qualitätssicherung
Einführung in die Software-QualitätssicherungChristian Baranowski
 
OSGi Web Development in Action
OSGi Web Development in ActionOSGi Web Development in Action
OSGi Web Development in Action
Christian Baranowski
 
Semantic Versioning
Semantic VersioningSemantic Versioning
Semantic Versioning
Christian Baranowski
 
OSGi Community Updates 2012
OSGi Community Updates 2012OSGi Community Updates 2012
OSGi Community Updates 2012
Christian Baranowski
 
Warum OSGi?
Warum OSGi?Warum OSGi?
Top10- Software Engineering Books
Top10- Software Engineering BooksTop10- Software Engineering Books
Top10- Software Engineering Books
Christian Baranowski
 
Domain Driven Design - 10min
Domain Driven Design - 10minDomain Driven Design - 10min
Domain Driven Design - 10min
Christian Baranowski
 
Einführung Vorgehensmodelle und Agile Software Entwicklung
Einführung Vorgehensmodelle und Agile Software EntwicklungEinführung Vorgehensmodelle und Agile Software Entwicklung
Einführung Vorgehensmodelle und Agile Software EntwicklungChristian Baranowski
 
Software Testing und Qualitätssicherung
Software Testing und QualitätssicherungSoftware Testing und Qualitätssicherung
Software Testing und QualitätssicherungChristian Baranowski
 
Einführung Software Testing und Qualitätssicherung
Einführung Software Testing und QualitätssicherungEinführung Software Testing und Qualitätssicherung
Einführung Software Testing und QualitätssicherungChristian Baranowski
 
Datenbankzugriff mit der Java Persistence Api
Datenbankzugriff mit der Java Persistence ApiDatenbankzugriff mit der Java Persistence Api
Datenbankzugriff mit der Java Persistence ApiChristian Baranowski
 

More from Christian Baranowski (20)

Microservices – die Architektur für Agile-Entwicklung?
Microservices – die Architektur für Agile-Entwicklung?Microservices – die Architektur für Agile-Entwicklung?
Microservices – die Architektur für Agile-Entwicklung?
 
OSGi and Spring Data for simple (Web) Application Development
OSGi and Spring Data  for simple (Web) Application DevelopmentOSGi and Spring Data  for simple (Web) Application Development
OSGi and Spring Data for simple (Web) Application Development
 
BDD - Behavior Driven Development Webapps mit Groovy Spock und Geb
BDD - Behavior Driven Development Webapps mit Groovy Spock und GebBDD - Behavior Driven Development Webapps mit Groovy Spock und Geb
BDD - Behavior Driven Development Webapps mit Groovy Spock und Geb
 
Komponententests und Testabdeckung
Komponententests und TestabdeckungKomponententests und Testabdeckung
Komponententests und Testabdeckung
 
Einführung in die Software-Qualitätssicherung
Einführung in die Software-QualitätssicherungEinführung in die Software-Qualitätssicherung
Einführung in die Software-Qualitätssicherung
 
OSGi Web Development in Action
OSGi Web Development in ActionOSGi Web Development in Action
OSGi Web Development in Action
 
Spock and Geb in Action
Spock and Geb in ActionSpock and Geb in Action
Spock and Geb in Action
 
Continuous Delivery in Action
Continuous Delivery in ActionContinuous Delivery in Action
Continuous Delivery in Action
 
Gradle and Continuous Delivery
Gradle and Continuous DeliveryGradle and Continuous Delivery
Gradle and Continuous Delivery
 
Spock and Geb
Spock and GebSpock and Geb
Spock and Geb
 
Semantic Versioning
Semantic VersioningSemantic Versioning
Semantic Versioning
 
OSGi Community Updates 2012
OSGi Community Updates 2012OSGi Community Updates 2012
OSGi Community Updates 2012
 
OSGi Mars World in Action
OSGi Mars World in ActionOSGi Mars World in Action
OSGi Mars World in Action
 
Warum OSGi?
Warum OSGi?Warum OSGi?
Warum OSGi?
 
Top10- Software Engineering Books
Top10- Software Engineering BooksTop10- Software Engineering Books
Top10- Software Engineering Books
 
Domain Driven Design - 10min
Domain Driven Design - 10minDomain Driven Design - 10min
Domain Driven Design - 10min
 
Einführung Vorgehensmodelle und Agile Software Entwicklung
Einführung Vorgehensmodelle und Agile Software EntwicklungEinführung Vorgehensmodelle und Agile Software Entwicklung
Einführung Vorgehensmodelle und Agile Software Entwicklung
 
Software Testing und Qualitätssicherung
Software Testing und QualitätssicherungSoftware Testing und Qualitätssicherung
Software Testing und Qualitätssicherung
 
Einführung Software Testing und Qualitätssicherung
Einführung Software Testing und QualitätssicherungEinführung Software Testing und Qualitätssicherung
Einführung Software Testing und Qualitätssicherung
 
Datenbankzugriff mit der Java Persistence Api
Datenbankzugriff mit der Java Persistence ApiDatenbankzugriff mit der Java Persistence Api
Datenbankzugriff mit der Java Persistence Api
 

Recently uploaded

Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*
Frank van Harmelen
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
BookNet Canada
 
Elevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object CalisthenicsElevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object Calisthenics
Dorra BARTAGUIZ
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a button
DianaGray10
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
Jemma Hussein Allen
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
OnBoard
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
UiPathCommunity
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
Kari Kakkonen
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
Laura Byrne
 
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Ramesh Iyer
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
Alison B. Lowndes
 
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdfFIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
Thijs Feryn
 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
Product School
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Product School
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
DianaGray10
 
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Thierry Lestable
 
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
Paul Groth
 

Recently uploaded (20)

Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
 
Elevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object CalisthenicsElevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object Calisthenics
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a button
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
 
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
 
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdfFIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
 
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
 
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
 

SDC - Einführung in Scala

  • 1. Einführung in Scala SDC 2011 - Seitenbau Konstanz Christian Baranowski Dennis Braunsdorf
  • 3. Scala a Scalable Language
  • 4. Scala Bazaar Scala = Bazaar Java = Cathedral “ The Cathedral and the Bazaar” - Eric S. Raymond
  • 9. Scala ist statisch typisiert
  • 11. Variablen var msg = &quot;Hello SDC!&quot; // Java Code String msg = &quot;Hello World&quot; ; val msg = &quot;Hello SDC!“ // Java Code final String msg = &quot;Hello World&quot; ;
  • 12. Funktionen def max(x : Int, y : Int) : Int = { if (x > y) { return x; } else { return y; } }
  • 13. Funktionen def max(x : Int, y : Int) : Int = { if (x > y) { x } else { y } }
  • 14. Funktionen def max(x : Int, y : Int) : Int = { if (x > y) x else y }
  • 15. Funktionen def max(x : Int, y : Int) = { if (x > y) x else y }
  • 16. Procedure def printer(msg : String) { ... }
  • 17. Funktionen Composition and Scoping def max(x : Int, y : Int) = { def xGreater() = { x > y } if (xGreater()) x else y }
  • 18. Funktionen Composition and Scoping def max(x : Int, y : Int) = { def xGreater() = { x > y } if (xGreater) x else y }
  • 19. Functions are objects def oncePerSecond(callback: () => Unit) { while ( true ) { callback(); Thread sleep 1000 } } def timeFlies() { println( &quot;time flies like an arrow...&quot; ) } oncePerSecond(timeFlies)
  • 20. Anonymous Functions def oncePerSecond(callback: () => Unit) { while ( true ) { callback(); Thread sleep 1000 } } oncePerSecond(() => println( &quot;time flies like an arrow...&quot; ))
  • 21. Arrays val names = new Array[String](2) names(0) = &quot;Dennis&quot; names(1) = &quot;Christian&quot;
  • 22. Arrays val names = Array( &quot;Dennis&quot; , &quot;Christian&quot; )
  • 23. Arrays val names = Array( &quot;Dennis&quot; , &quot;Christian&quot; , true , 0, 1.5) val names : Array[Any] = Array( &quot;Dennis&quot; , &quot;Christian&quot; , true , 0, 1.5)
  • 24. Listen val oneTwo = List(1, 2) val threeFour = List(3, 4) val oneTwoThreeFour = oneTwo ::: threeFour val oneTwoThree = 1 :: 2 :: 3 :: Nil
  • 25. Tuples val dennis = (1, &quot;Dennis&quot; ) val christian = (2, &quot;Christian&quot; ) val dennis = ( &quot;Dennis&quot; , &quot;Braunsdorf&quot; ) println(dennis._1) println(dennis._2)
  • 26. Sets
  • 27. Sets import scala.collection.mutable.HashSet val jetSet = new HashSet[String] jetSet += &quot;Lear&quot; jetSet += ( &quot;Boeing&quot; , &quot;Airbus&quot; ) println(jetSet.contains( &quot;Cessna&quot; ))
  • 28. Sets val jetSet = Set( &quot;AirBus&quot; ) jetSet: scala.collection.immutable.Set[java.lang.String]
  • 29. Maps
  • 30. Maps import scala.collection.mutable.HashMap val treasureMap = new HashMap[Int, String] treasureMap += 1 -> &quot;Go to island.&quot; treasureMap += 2 -> &quot;Find big X on ground.&quot; treasureMap += 3 -> &quot;Dig.&quot; println(treasureMap(2))
  • 31. Maps val treasureMap = Map( (1, &quot;Go to island.&quot; ), (2, &quot;Find big X on ground.&quot; ), (3, &quot;Dig.&quot; ) )
  • 32. While Schleife val names = Array( &quot;Dennis&quot; , &quot;Christian&quot; ) var i = 0 while (i < names.length) { println(names(i)) i = i + 1 }
  • 33. For Schleife val names = Array( &quot;Dennis&quot; , &quot;Christian&quot; ) for (name <- names) println(name)
  • 34. Foreach Collection val names = Array( &quot;Dennis&quot; , &quot;Christian&quot; ) names.foreach(name => println(name))
  • 35. Throw Exceptions def max(x : Int, y : Int) : Int = { if (x < 0) throw new Exception( &quot;Negativ Int!&quot; ) ... }
  • 36. Catch Exceptions try { ... } catch { case ioe: IOException => println( &quot;Fehler beim lesen auf dem Filesystem&quot; ) case e: Exception => println( &quot;Unbekannter Fehler&quot; ) }
  • 38. Everything is a Object
  • 39. Numbers are Objects 1 + 2 * 3 / x = (1).+(((2).*(3))./(x))
  • 40. Objekte object DemoService { def print(msg : String) { println(msg) } } DemoService.print( &quot;Hello World&quot; )
  • 41. Klassen class Complex(real: Double, imaginary: Double) { def re() = real def im() = imaginary }
  • 42. Klassen class Complex(real: Double, imaginary: Double) { def re = real def im = imaginary }
  • 43. Abstrakte Klassen abstract class Complex(real: Double, imaginary: Double) { abstract def re() def im = imaginary }
  • 44. Klassen protected class Complex(real: Double, imaginary: Double) { private def re() = real protected def im() = imaginary }
  • 45. Auxiliary Constructors class Complex(real: Double, imaginary: Double) { def this () = this (0,0) def re() = real def im() = imaginary }
  • 46. Packages und Imports package demo import javax._ import scala.collection.mutable.HashMap
  • 47. Scala Live Demo Getting Started ….
  • 49. Traits trait Ord { def < (that: Any): Boolean def <=(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 }
  • 50. Operators + * - / class Demo { def + (x : Int) = 1 + x } var o1 = new MyOrd println(o1 + 1)
  • 51. Methoden Overloading class Demo { def + (x : Int) = 1 + x def + (x : Demo) = 1 + 1 }
  • 52. Methoden Überschreiben class Complex(real: Double, imaginary: Double) { def re = real def im = imaginary override def toString() = &quot;&quot; + re + ( if (im < 0) &quot;&quot; else &quot;+&quot; ) + im + &quot;i„ }
  • 53. Case Klassen abstract class Tree case class Sum(l: Tree, r: Tree) extends Tree case class Var(n: String) extends Tree case class Const(v: Int) extends Tree
  • 54. 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( &quot;x&quot; ),Var( &quot;x&quot; )),Sum(Const(7),Var( &quot;y&quot; ))) val env: Environment = { case &quot;x&quot; => 5 case &quot;y&quot; => 7 } println( &quot;Expression: &quot; + exp) println( &quot;Evaluation with x=5, y=7: &quot; + eval(exp, env)) println( &quot;Derivative relative to x: &quot; + derive(exp, &quot;x&quot; )) println( &quot;Derivative relative to y: &quot; + derive(exp, &quot;y&quot; ))
  • 56. 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 &quot;Ullman&quot; ) yield b.title
  • 57. 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( &quot;Reference contains the half of &quot; + (cell.get * 2)) } }
  • 58. 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( &quot;EmptyStack.top&quot; ) def pop = error( &quot;EmptyStack.pop&quot; ) } class NonEmptyStack[A](elem: A, rest: Stack[A]) extends Stack[A] { def isEmpty = false def top = elem def pop = rest }
  • 59. 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) }
  • 60. Implicit Parameters implicit object stringMonoid extends Monoid[String] { def add(x: String, y: String): String = x.concat(y) def unit: String = &quot;&quot; } implicit object intMonoid extends Monoid[Int] { def add(x: Int, y: Int): Int = x + y def unit: Int = 0 }
  • 61. 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 }
  • 62. Annotations @field class BeanProperty extends annotation.StaticAnnotation
  • 63. Scala Java Integration // Java public class MainJava { public static void main(String[] args) { Point point = new Point(); point.name(); } } // Scala class Point(x : Int, y : Int) { var name = &quot;Bin ein Point&quot; def this () = this (0, 0) override def toString = name + &quot; : x=&quot; + x + &quot; : y=&quot; + y }
  • 64. Package Objects package object demo { implicit def toPoint(name: String) = new Point }
  • 66. Java Beans mit Scala class PersonBean { @scala.reflect.BeanProperty var name : String = &quot;&quot; }
  • 67. JUnit Tests with Scala import 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)); } }
  • 68. Scala als DSL Toolkit
  • 70. Robot DSL object DemoRobot extends RobotProgram with Application { 000 PRINT &quot;Lunar Mars Program starts.&quot; 001 MOVE(1, 10) 002 SLEEP2000 003 MOVE(10, 10) 004 PRINT &quot;POSITION Now 10, 10&quot; 005 MOVE(20, 20) 006 END RUN }
  • 73.