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
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
}
}
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 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
}
}
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 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
}
}
68. “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
}
}
69. “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
}
}
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 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
}
}
72. “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
}
}
73. “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
}
}
74. “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
}
}
75. “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)
}
85. Scala gives
• More concise - type inference
• More expressive - operator notation
• OOP for real
• Slick functional collection api
• Full access to Java