Scala
the smarter cousin




                     David Rosell
Scala
the smarter cousin
Scala
                  the smarter cousin




Object Oriented
  Functional
Scala
                              the smarter cousin



 general-purpose, type-safe


Object Oriented
  Functional
Scala
                              the smarter cousin



 general-purpose, type-safe


Object Oriented
  Functional
 runs on JVM (and CLR)
OOP
                                  as it should be done

Collections

                Scala are
              library oriented
                                               Duck typing




      Equals                     DSL’s
                                           n!
                                     = 1*2*3*...*n
Closures
scala> var more = 1
more: Int = 1

scala> val addMore = (x: Int) => x + more
addMore: (Int) => Int = <function>
scala> var more = 1
more: Int = 1

scala> val addMore = (x: Int) => x + more
addMore: (Int) => Int = <function>

scala> addMore(1)
res1: Int = 2

scala> more = 10
more: Int = 10

scala> addMore(1)
res3: Int = 11
Using
resources
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
    }
}
Background




val list = List(“Pelle”, “Stina”)
for (name <- list) {
  println(name)
}
Background




val list = List(“Pelle”, “Stina”)

list.foreach( aFunc(...) )
Background




val 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(_) )
Background




val list = List(“Pelle”, “Stina”)

list.foreach( println _ )
Background




val 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 free
http://www.artima.com/
pins1ed/
David Rosell
David Rosell

BlogSpot: http://drsimplestuff.blogspot.com
 GitHub: https://github.com/daros
   E-mail: david.rosell@redpill-linpro.com

Scala - den smarta kusinen

  • 14.
  • 15.
  • 16.
    Scala the smarter cousin Object Oriented Functional
  • 17.
    Scala the smarter cousin general-purpose, type-safe Object Oriented Functional
  • 18.
    Scala the smarter cousin general-purpose, type-safe Object Oriented Functional runs on JVM (and CLR)
  • 20.
    OOP as it should be done Collections Scala are library oriented Duck typing Equals DSL’s n! = 1*2*3*...*n
  • 21.
  • 23.
    scala> var more= 1 more: Int = 1 scala> val addMore = (x: Int) => x + more addMore: (Int) => Int = <function>
  • 24.
    scala> var more= 1 more: Int = 1 scala> val addMore = (x: Int) => x + more addMore: (Int) => Int = <function> scala> addMore(1) res1: Int = 2 scala> more = 10 more: Int = 10 scala> addMore(1) res3: Int = 11
  • 26.
  • 27.
    def writeSomething(file: File):Unit = { val writer = new PrintWriter(file) try { writer.println(“Hello Kitty”) writer.println(new Date) } finally { writer.close } }
  • 28.
    def writeSomething(file: File):Unit = { val writer = new PrintWriter(file) try { writer.println(“Hello Kitty”) writer.println(new Date) } finally { writer.close } }
  • 29.
    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 { ... }
  • 30.
    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 } }
  • 31.
    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 } }
  • 32.
    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 } }
  • 33.
    def writeSomething(file: File){ val writer = new PrintWriter(file) try { writer.println(“Hello Kitty”) writer.println(new Date) } finally { writer.close } }
  • 34.
    def writeSomething(file: File){ val writer = new PrintWriter(file) try { writer.println(“Hello Kitty”) writer.println(new Date) } finally { writer.close } }
  • 35.
    writer.println(“Hello Kitty”) writer.println(new Date) def writeSomething(file: File) { val writer = new PrintWriter(file) try { } finally { writer.close } }
  • 36.
    Background val list =List(“Pelle”, “Stina”) for (name <- list) { println(name) }
  • 37.
    Background val list =List(“Pelle”, “Stina”) list.foreach( aFunc(...) )
  • 38.
    Background val list =List(“Pelle”, “Stina”) list.foreach( aFunc(...) ) def aFunc(s: String): Unit = { println(s) }
  • 39.
    Background def aFunc(s: String): Unit = { println(s) } val list = List(“Pelle”, “Stina”) list.foreach( (s: String): Unit => { println(s) } )
  • 40.
    Background def aFunc(s: String): Unit = { println(s) } val list = List(“Pelle”, “Stina”) list.foreach( (s: String) => { println(s) } )
  • 41.
    Background def aFunc(s: String): Unit = { println(s) } val list = List(“Pelle”, “Stina”) list.foreach( s => println(s) )
  • 42.
    Background def aFunc(s: String): Unit = { println(s) } val list = List(“Pelle”, “Stina”) list.foreach( _ => println(_) )
  • 43.
    Background def aFunc(s: String): Unit = { println(s) } val list = List(“Pelle”, “Stina”) list.foreach( println(_) )
  • 44.
    Background val list =List(“Pelle”, “Stina”) list.foreach( println _ )
  • 45.
    Background val list =List(“Pelle”, “Stina”) list.foreach( println _ ) for (name <- list) { println(name) }
  • 46.
    def writeTo(file: File){ val writer = new PrintWriter(file) try { writer.println(“Hello Kitty”) ... writer.println(new Date) } finally { writer.close } }
  • 47.
    writer.println(“Hello Kitty”) writer.println(new Date) def writeTo(file: File) { val writer = new PrintWriter(file) try { ... } finally { writer.close } }
  • 48.
    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 } }
  • 49.
    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 } }
  • 50.
    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 } }
  • 51.
    def op(writer: PrintWriter){ writer.println(“Hello Kitty”) writer.println(new Date) }
  • 52.
    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 } }
  • 53.
    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 } }
  • 54.
    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)
  • 55.
    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)
  • 56.
    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) }
  • 57.
    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) })
  • 58.
    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) }
  • 59.
    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) })
  • 60.
    def writeTo(file: File,op: PrintWriter => Unit) { val writer = new PrintWriter(file) try { op(writer) } finally { writer.close } }
  • 61.
    )( def writeTo(file: File,op: PrintWriter => Unit) { val writer = new PrintWriter(file) try { op(writer) } finally { writer.close } }
  • 62.
    Currying )( def writeTo(file: File, op: PrintWriter => Unit) { val writer = new PrintWriter(file) try { op(writer) } finally { writer.close } }
  • 63.
    )( 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))
  • 64.
    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) }
  • 65.
    “calling writeTo mustwrite 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 } }
  • 66.
    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 } }
  • 67.
    “calling writeTo mustwrite 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 } }
  • 68.
    “calling writeTo mustwrite 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 } }
  • 69.
    “calling writeTo mustwrite 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 } }
  • 70.
    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 } }
  • 71.
    “calling writeSomething mustwrite 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 } }
  • 72.
    “calling writeSomething mustwrite 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 } }
  • 73.
    “calling writeSomething mustwrite 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 } }
  • 74.
    “calling writeSomething mustwrite 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 } }
  • 75.
    “calling writeSomething mustwrite 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) }
  • 76.
    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) }
  • 77.
    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) }
  • 78.
    object WriteToFileSpecs extendsSpecification 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) } }
  • 83.
    JVM Community Language
  • 84.
    JVM Community
  • 85.
    Scala gives • Moreconcise - type inference • More expressive - operator notation • OOP for real • Slick functional collection api • Full access to Java
  • 87.
  • 88.
  • 89.
    David Rosell BlogSpot: http://drsimplestuff.blogspot.com GitHub: https://github.com/daros E-mail: david.rosell@redpill-linpro.com

Editor's Notes