Scala - den smarta kusinen
Upcoming SlideShare
Loading in...5
×
 

Scala - den smarta kusinen

on

  • 364 views

 

Statistics

Views

Total Views
364
Slideshare-icon Views on SlideShare
364
Embed Views
0

Actions

Likes
0
Downloads
0
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Apple Keynote

Usage Rights

CC Attribution-ShareAlike LicenseCC Attribution-ShareAlike License

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • general-purpose, \ntype-safe language \n for the Java Platform \nthat combines \n object-oriented \n and \n functional programming\n
  • general-purpose, \ntype-safe language \n for the Java Platform \nthat combines \n object-oriented \n and \n functional programming\n
  • general-purpose, \ntype-safe language \n for the Java Platform \nthat combines \n object-oriented \n and \n functional programming\n
  • general-purpose, \ntype-safe language \n for the Java Platform \nthat combines \n object-oriented \n and \n functional programming\n
  • general-purpose, \ntype-safe language \n for the Java Platform \nthat combines \n object-oriented \n and \n functional programming\n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n

Scala - den smarta kusinen Scala - den smarta kusinen Presentation Transcript

  • Scalathe smarter cousin David Rosell
  • Scalathe smarter cousin
  • Scala the smarter cousinObject Oriented Functional
  • Scala the smarter cousin general-purpose, type-safeObject Oriented Functional
  • Scala the smarter cousin general-purpose, type-safeObject Oriented Functional runs on JVM (and CLR)
  • OOP as it should be doneCollections Scala are library oriented Duck typing Equals DSL’s n! = 1*2*3*...*n
  • Closures
  • scala> var more = 1more: Int = 1scala> val addMore = (x: Int) => x + moreaddMore: (Int) => Int = <function>
  • scala> var more = 1more: Int = 1scala> val addMore = (x: Int) => x + moreaddMore: (Int) => Int = <function>scala> addMore(1)res1: Int = 2scala> more = 10more: Int = 10scala> addMore(1)res3: Int = 11
  • Usingresources
  • def writeSomething(file: File): Unit = { val writer = new PrintWriter(file) try { writer.println(“Hello Kitty”) writer.println(new Date) } finally { writer.close }}
  • def writeSomething(file: File): Unit = { val writer = new PrintWriter(file) try { writer.println(“Hello Kitty”) writer.println(new Date) } finally { writer.close }}
  • def writeSomething(file: File): Unit = { val writer = new PrintWriter(file) try { writer.println(“Hello Kitty”) writer.println(new Date) } finally { writer.close }}object WriteToFileSpecs extends Specification { ...}
  • def writeSomething(file: File): Unit = { val writer = new PrintWriter(file) try { writer.println(“Hello Kitty”) writer.println(new Date) } finally { writer.close }}“calling writeSomething must write to file” in { val file = new File(“test.txt”) try { writeSomething(file) val cont = Source.fromFile(file).getLines.toList cont(0) must be equalTo “Hello Kitty” cont(1) } finally { file.delete }}
  • def writeSomething(file: File) { val writer = new PrintWriter(file) try { writer.println(“Hello Kitty”) writer.println(new Date) } finally { writer.close }}“calling writeSomething must write to file” in { val file = new File(“test.txt”) try { writeSomething(file) val cont = Source.fromFile(file).getLines.toList cont(0) must be equalTo “Hello Kitty” cont(1) } finally { file.delete }}
  • writer.println(“Hello Kitty”) writer.println(new Date)“calling writeSomething must write to file” in { val file = new File(“test.txt”) try { writeSomething(file) val cont = Source.fromFile(file).getLines.toList cont(0) must be equalTo “Hello Kitty” cont(1) ??? how to test a ‘now’-date } finally { file.delete }}
  • def writeSomething(file: File) { val writer = new PrintWriter(file) try { writer.println(“Hello Kitty”) writer.println(new Date) } finally { writer.close }}
  • def writeSomething(file: File) { val writer = new PrintWriter(file) try { writer.println(“Hello Kitty”) writer.println(new Date) } finally { writer.close }}
  • writer.println(“Hello Kitty”) writer.println(new Date)def writeSomething(file: File) { val writer = new PrintWriter(file) try { } finally { writer.close }}
  • Backgroundval list = List(“Pelle”, “Stina”)for (name <- list) { println(name)}
  • Backgroundval list = List(“Pelle”, “Stina”)list.foreach( aFunc(...) )
  • Backgroundval list = List(“Pelle”, “Stina”)list.foreach( aFunc(...) )def aFunc(s: String): Unit = { println(s)}
  • Background def aFunc(s: String): Unit = { println(s) }val list = List(“Pelle”, “Stina”)list.foreach( (s: String): Unit => { println(s) } )
  • Background def aFunc(s: String): Unit = { println(s) }val list = List(“Pelle”, “Stina”)list.foreach( (s: String) => { println(s) } )
  • Background def aFunc(s: String): Unit = { println(s) }val list = List(“Pelle”, “Stina”)list.foreach( s => println(s) )
  • Background def aFunc(s: String): Unit = { println(s) }val list = List(“Pelle”, “Stina”)list.foreach( _ => println(_) )
  • Background def aFunc(s: String): Unit = { println(s) }val list = List(“Pelle”, “Stina”)list.foreach( println(_) )
  • Backgroundval list = List(“Pelle”, “Stina”)list.foreach( println _ )
  • Backgroundval list = List(“Pelle”, “Stina”)list.foreach( println _ )for (name <- list) { println(name)}
  • def writeTo(file: File) { val writer = new PrintWriter(file) try { writer.println(“Hello Kitty”) ... writer.println(new Date) } finally { writer.close }}
  • writer.println(“Hello Kitty”) writer.println(new Date)def writeTo(file: File) { val writer = new PrintWriter(file) try { ... } finally { writer.close }}
  • def op(writer: PrintWriter) { writer.println(“Hello Kitty”) writer.println(new Date)}def writeTo(file: File) { val writer = new PrintWriter(file) try { ... } finally { writer.close }}
  • def op(writer: PrintWriter) { writer.println(“Hello Kitty”) writer.println(new Date)} def writeTo(file: File) { val writer = new PrintWriter(file) try { ... } finally { writer.close } }
  • def op(writer: PrintWriter) { writer.println(“Hello Kitty”) writer.println(new Date)} def writeTo(file: File) { val writer = new PrintWriter(file) try { ... } finally { writer.close } }
  • def op(writer: PrintWriter) { writer.println(“Hello Kitty”) writer.println(new Date)}
  • def op(writer: PrintWriter) { writer.println(“Hello Kitty”) writer.println(new Date)}def writeTo(file: File, op: PrintWriter => Unit) { val writer = new PrintWriter(file) try { ... } finally { writer.close }}
  • def op(writer: PrintWriter) { writer.println(“Hello Kitty”) writer.println(new Date)}def writeTo(file: File, op: PrintWriter => Unit) { val writer = new PrintWriter(file) try { op(writer) } finally { writer.close }}
  • def writeTo(file: File, op: PrintWriter => Unit) { val writer = new PrintWriter(file) try { op(writer) } finally { writer.close }}def op(writer: PrintWriter) { writer.println(“Hello Kitty”) writer.println(new Date)}val file = new File(“test.txt”)val op = (writer: PrintWriter) => { writer.println(“Hello Kitty”) writer.println(new Date) }writeTo(file, op)
  • def writeTo(file: File, op: PrintWriter => Unit) { val writer = new PrintWriter(file) try { op(writer) } finally { writer.close }}val file = new File(“test.txt”)val op = (writer: PrintWriter) => { writer.println(“Hello Kitty”) writer.println(new Date) }writeTo(file, op)
  • def writeTo(file: File, op: PrintWriter => Unit) { val writer = new PrintWriter(file) try { op(writer) } finally { writer.close }}val file = new File(“test.txt”)writeTo(file, (writer: PrintWriter) => { op) writer.println(“Hello Kitty”) writer.println(new Date) }
  • def writeTo(file: File, op: PrintWriter => Unit) { val writer = new PrintWriter(file) try { op(writer) } finally { writer.close }}val file = new File(“test.txt”)writeTo(file, (writer: PrintWriter) => { writer.println(“Hello Kitty”) writer.println(new Date) })
  • def writeTo(file: File, op: PrintWriter => Unit) { val writer = new PrintWriter(file) try { op(writer) } finally { writer.close }}val file = new File(“test.txt”) writer => { writer.println(“Hello Kitty”) writer.println(new Date) }
  • def writeTo(file: File, op: PrintWriter => Unit) { val writer = new PrintWriter(file) try { op(writer) } finally { writer.close }}val file = new File(“test.txt”)writeTo(file, writer => { writer.println(“Hello Kitty”) writer.println(new Date) })
  • def writeTo(file: File, op: PrintWriter => Unit) { val writer = new PrintWriter(file) try { op(writer) } finally { writer.close }}
  • )(def writeTo(file: File, op: PrintWriter => Unit) { val writer = new PrintWriter(file) try { op(writer) } finally { writer.close }}
  • Currying )(def writeTo(file: File, op: PrintWriter => Unit) { val writer = new PrintWriter(file) try { op(writer) } finally { writer.close }}
  • )(def writeTo(file: File, op: PrintWriter => Unit) { val writer = new PrintWriter(file) try { op(writer) } finally { writer.close }}val file = new File(“test.txt”)writeTo(file)(writer => writer.println(“Hello Kitty”) writer.println(new Date))
  • def writeTo(file: File)(op: PrintWriter => Unit) { val writer = new PrintWriter(file) try { op(writer) } finally { writer.close }}val file = new File(“test.txt”)writeTo(file) { writer => writer.println(“Hello Kitty”) writer.println(new Date)}
  • “calling writeTo must write to file” in { val file = new File(“test.txt”) try { writeTo(file) val cont = Source.fromFile(file).getLines.toList cont(0) must be equalTo “Hello Kitty” cont(1) ??? how to test a ‘now’-date } finally { file.delete }}
  • writeTo(file) { writer => writer.println(“Hello Kitty”) writer.println(new Date) }“calling writeTo must write to file” in { val file = new File(“test.txt”) try { writeTo(file) val cont = Source.fromFile(file).getLines.toList cont(0) must be equalTo “Hello Kitty” cont(1) ??? how to test a ‘now’-date } finally { file.delete }}
  • “calling writeTo must write to file” in { val file = new File(“test.txt”) try { writeTo(file) { writer => writer.println(“Hello Kitty”) writer.println(new Date) } val cont = Source.fromFile(file).getLines.toList cont(0) must be equalTo “Hello Kitty” cont(1) ??? how to test a ‘now’-date } finally { file.delete }}
  • “calling writeTo must write to file” in { val file = new File(“test.txt”) try { val now = new Date writeTo(file) { writer => writer.println(“Hello Kitty”) writer.println(new Date) } val cont = Source.fromFile(file).getLines.toList cont(0) must be equalTo “Hello Kitty” cont(1) ??? how to test a ‘now’-date } finally { file.delete }}
  • “calling writeTo must write to file” in { val file = new File(“test.txt”) try { val now = new Date writeTo(file) { writer => writer.println(“Hello Kitty”) writer.println(now) } val cont = Source.fromFile(file).getLines.toList cont(0) must be equalTo “Hello Kitty” cont(1) must be equalTo now.toString } finally { file.delete }}
  • def writeTo(file: File, op: PrintWriter => Unit) { val writer = new PrintWriter(file) try { op(writer) } finally { writer.close }}“calling writeTo must write to file” in { val file = new File(“test.txt”) try { val now = new Date writeTo(file) { writer => writer.println(“Hello Kitty”) writer.println(now) } val cont = Source.fromFile(file).getLines.toList cont(0) must be equalTo “Hello Kitty” cont(1) must be equalTo now.toString } finally { file.delete }}
  • “calling writeSomething must write to file” in { val file = new File(“test.txt”) try { val now = new Date writeTo(file) { writer => writer.println(“Hello Kitty”) writer.println(now) } val cont = Source.fromFile(file).getLines.toList cont(0) must be equalTo “Hello Kitty” cont(1) must be equalTo now.toString } finally { file.delete }}
  • “calling writeSomething must write to file” in { withFile { file => val now = new Date writeTo(file) { writer => writer.println(“Hello Kitty”) writer.println(now) } val cont = Source.fromFile(file).getLines.toList cont(0) must be equalTo “Hello Kitty” cont(1) must be equalTo now.toString }}def withFile(op: File => Unit) { val file = new File(“test.txt”) try { op(file) } finally { file.delete }}
  • “calling writeSomething must write to file” in { withFile { file => val now = new Date writeTo(file) { writer => writer.println(“Hello Kitty”) writer.println(now) } val cont = Source.fromFile(file).getLines.toList cont(0) must be equalTo “Hello Kitty” cont(1) must be equalTo now.toString }}
  • “calling writeSomething must write to file” in { withFile { file => val now = new Date writeTo(file) { writer => writer.println(“Hello Kitty”) writer.println(now) } val cont = Source.fromFile(file).getLines.toList validate(file) { cont => cont(0) must be equalTo “Hello Kitty” cont(1) must be equalTo now.toString }}
  • “calling writeSomething must write to file” in { withFile { file => val now = new Date writeTo(file) { writer => writer.println(“Hello Kitty”) writer.println(now) } validate(file) { cont => cont(0) must be equalTo “Hello Kitty” cont(1) must be equalTo now.toString } }}def validate(file: File)(op: List[String] => Unit) { val cont = Source.fromFile(file).getLines.toList op(cont)}
  • def withFile(op: File => Unit) { val file = new File(“test.txt”) try { op(file) } finally { file.delete }}def validate(file: File)(op: List[String] => Unit) { val cont = Source.fromFile(file).getLines.toList op(cont)}
  • def withFile(op: File => Unit) { val file = new File(“test.txt”) try { op(file) } finally { file.delete }}def validate(file: File)(op: List[String] => Unit) { val cont = Source.fromFile(file).getLines.toList op(cont)}
  • object WriteToFileSpecs extends Specification with TestFileHelper { ...}trait TestFileHelper { def withFile(op: File => Unit) { val file = new File(“test.txt”) try { op(file) } finally { file.delete } } def validate(file: File)(op: List[String] => Unit) { val cont = Source.fromFile(file).getLines.toList op(cont) }}
  • JVM Community Language
  • JVM Community
  • Scala gives• More concise - type inference• More expressive - operator notation• OOP for real• Slick functional collection api• Full access to Java
  • 1’st edition freehttp://www.artima.com/pins1ed/
  • David Rosell
  • David RosellBlogSpot: http://drsimplestuff.blogspot.com GitHub: https://github.com/daros E-mail: david.rosell@redpill-linpro.com