def square(a: Int) = a * aprintln("square(2):" + square(2))println(1 + 1)println(1. + 1)class IntAdder(var value: Int) {def + (aValue: Int): Int = {value += aValuevalue}}println(new IntAdder(2) + 2)
object Closer { def using(closeable: { def close(): Unit }, f: => Unit) { try { f } finally { closeable.close } } }class Connection {def close() {}def preClose() {}}class App {def close() {}def authenticate() {}}object Test {def main(args: Array[String]) {val connection = new Connectionval app = new AppCloser.using(connection, connection.preClose)Closer.using(app, app.authenticate)}}
val numbers = List(1, 2, 3, 4)numbers.map(_ * 2) // _ represents every members in Listnumbers.map((i: Int) => i * 2) // actually define a anonymous functiondef isEven((i: Int): Boolean = i % 2 == 0numbers.filter(isEven _)
def multiply(left: Int)(right: Int) = left * rightval multiply10 = multiply(10)(_)multiply10(10)val right: Int = 10def multiply(left: Int) = left * rightmultiply(10)