SDC - Einführung in Scala
Upcoming SlideShare
Loading in...5
×

Like this? Share it with your network

Share

SDC - Einführung in Scala

  • 1,451 views
Uploaded on

Einführung in Scala - Seitenbau Developer Convention 2011. Basics der Sprache Scala.

Einführung in Scala - Seitenbau Developer Convention 2011. Basics der Sprache Scala.

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
1,451
On Slideshare
1,216
From Embeds
235
Number of Embeds
14

Actions

Shares
Downloads
6
Comments
0
Likes
0

Embeds 235

http://tux2323.blogspot.com 200
http://tux2323.blogspot.co.uk 10
http://tux2323.blogspot.in 9
http://tux2323.blogspot.fr 4
http://tux2323.blogspot.kr 2
http://tux2323.blogspot.de 2
http://tux2323.blogspot.com.au 1
http://tux2323.blogspot.com.br 1
http://tux2323.blogspot.it 1
http://tux2323.blogspot.se 1
http://tux2323.blogspot.tw 1
http://tux2323.blogspot.com.ar 1
http://tux2323.blogspot.co.at 1
http://scratchpad58.rssing.com 1

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Einführung in Scala SDC 2011 - Seitenbau Konstanz Christian Baranowski Dennis Braunsdorf
  • 2. Was ist Scala?
  • 3. Scala a Scalable Language
  • 4. Scala Bazaar Scala = Bazaar Java = Cathedral “ The Cathedral and the Bazaar” - Eric S. Raymond
  • 5. Warum Scala?
  • 6. Scala ist objekt-orientiert
  • 7. Everything is a Object
  • 8. Scala ist funktional
  • 9. Scala ist statisch typisiert
  • 10. Scala Basics
  • 11. Variablen var msg = "Hello SDC!" // Java Code String msg = "Hello World" ; val msg = "Hello SDC!“ // Java Code final String msg = "Hello World" ;
  • 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( "time flies like an arrow..." ) } oncePerSecond(timeFlies)
  • 20. Anonymous Functions def oncePerSecond(callback: () => Unit) { while ( true ) { callback(); Thread sleep 1000 } } oncePerSecond(() => println( "time flies like an arrow..." ))
  • 21. Arrays val names = new Array[String](2) names(0) = "Dennis" names(1) = "Christian"
  • 22. Arrays val names = Array( "Dennis" , "Christian" )
  • 23. Arrays val names = Array( "Dennis" , "Christian" , true , 0, 1.5) val names : Array[Any] = Array( "Dennis" , "Christian" , 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, "Dennis" ) val christian = (2, "Christian" ) val dennis = ( "Dennis" , "Braunsdorf" ) println(dennis._1) println(dennis._2)
  • 26. Sets
  • 27. Sets import scala.collection.mutable.HashSet val jetSet = new HashSet[String] jetSet += "Lear" jetSet += ( "Boeing" , "Airbus" ) println(jetSet.contains( "Cessna" ))
  • 28. Sets val jetSet = Set( "AirBus" ) 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 -> "Go to island." treasureMap += 2 -> "Find big X on ground." treasureMap += 3 -> "Dig." println(treasureMap(2))
  • 31. Maps val treasureMap = Map( (1, "Go to island." ), (2, "Find big X on ground." ), (3, "Dig." ) )
  • 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; ) }
  • 37. Scala OOP Basics
  • 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 ….
  • 48. Scala OOP Teil 2
  • 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:n &quot; + derive(exp, &quot;x&quot; )) println( &quot;Derivative relative to y:n &quot; + derive(exp, &quot;y&quot; ))
  • 55. Advanced Features
  • 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 }
  • 65. Scala in Action
  • 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
  • 69. Internal DSLs mit Scala
  • 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 }
  • 71. External DSLs mit Scala
  • 72. Fragen ?
  • 73. 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 Tutorial http://www.scala-lang.org/docu/files/ScalaTutorial.pdf
    • Scala Actors - http://www.scala-lang.org/node/242
    • Lift http://liftweb.net