import scala.io.Source/** * Created by ly on 13-12-26. */object FunctionsAndClosures { def main(args: Array[String]) { // 8.1 Methods, like in Java /* 8.2 Local functions an important design principle of the functional programming style:programs should be decomposed into many small functions that each do awell-defined task.避免命名冲突,将helper function放在function里面(比private更好) */ def processFile(filename: String, width: Int) { def processLine(line: String) { if (line.length > width) println(filename +": "+ line) // 可以放外外围函数的参数 } val source = Source.fromFile(filename) for (line <- source.getLines()) processLine(line) } /* 8.3 First-class functionsA function literal is compiled into a class that when instantiated at runtime is a function value.Function values are objects, so you can store them in variables if you like.They are functions, too, so you can invoke them using the usual parenthesesfunction-call notation. */ /* 8.4 Short forms of function literals */ val someNumbers = List(-11, -10, -5, 0, 5, 10) someNumbers.filter((x: Int) => x > 0) someNumbers.filter((x) => x > 0) // target typing someNumbers.filter(x => x > 0) // eave out parentheses around a parameter whose type is inferred. /* 8.5 Placeholder syntaxyou can use underscores as placeholders for one or more parameters, so long as each parameter appearsonly one time within the function literal.The first underscore represents the first parameter, the second underscore the second parameter,the third underscore the third parameter, and so on. */ someNumbers.filter(_ > 0) // val f = (_: Int) + (_: Int) // scala不能类型推断的时候需要显示指明type /* 8.6 Partially applied functions A partially applied function is an expression in which you don’t supply allof the arguments needed by the function. Instead, you supply some, or none,of the needed arguments. */ someNumbers.foreach(println _) // the underscore in this case is not a placeholder for a single parameter. It is a placeholder for an entire parameter list. /* Although you can’t assign amethod or nested function to a variable, or pass it as an argument to anotherfunction, you can do these things if you wrap the method or nested functionin a function value by placing an underscore after its name. */ someNumbers.foreach(println) // allowed only in places where a function is required, such as the invocation of foreach in this example. def sum(a: Int, b: Int, c: Int) = a + b + c //val x = sum // compile error! val y = sum _ /* 8.7 Closures The function value (the object) that’s created at runtime from this functionliteral is called a closure. The name arises from the act of “closing” the func-tion literal by “capturing” the bindings of its free variables. A function literalwith no free variables, such as (x: Int) => x + 1 , is called a closed term,where a term is a bit of source code. Thus a function value created at run-time from this function literal is not a closure in the strictest sense, because(x: Int) => x + 1 is already closed as written. But any function literal withfree variables, such as (x: Int) => x + more , is an open term. Therefore,any function value created at runtime from (x: Int) => x + more will bydefinition require that a binding for its free variable, more , be captured. Theresulting function value, which will contain a reference to the captured morevariable, is called a closure, therefore, because the function value is the endproduct of the act of closing the open term, (x: Int) => x + more . */ var more = 10 def increase = (x: Int) => {val xplus = x + more; more += 10; xplus} // more is a free variable, x is a bound variable println(increase(1)) println(more) // closure可以改变free variable more = 11 println(increase(1)) // closure可以看到more的变化 /* the instance used is the one that was active at the time the closure was created. The Scala compiler rearranges things in cases like this so that the captured parameter lives out on the heap, instead of the stack, and thus can outlive the method call that created it. */ def makeIncreaser(more: Int) = (x: Int) => x + more val inc1 = makeIncreaser(1) val inc9999 = makeIncreaser(9999) /* 8.8 Special function call forms */ // Repeated parameters def echo(args: String*) = for (arg <- args) println(arg) // args: Array[String] val arr = Array("What's", "up", "doc?") echo(arr: _*) // 将Array打散 // Named arguments def speed(distance: Float, time: Float): Float = distance / time println(speed(time = 10, distance = 100)) // 可以将顺序打乱, It is also possible to mix positional and named arguments. In that case, the positional arguments come first. // Default parameter values // Named arguments are most frequently used in combination with default parameter values. def printTime2(out: java.io.PrintStream = Console.out, divisor: Int = 1) = out.println("time = "+ System.currentTimeMillis()/divisor) printTime2() printTime2(Console.out) printTime2(out = Console.out) printTime2(divisor = 100) /* 8.9 Tail recursion Often, a recursive solution is more elegant and concise than a loop-based one. If the solution is tail recursive, therewon’t be any runtime overhead to be paid. */ // -g:notailcalls来查看stack frame /* Limits of tail recursion tail-call optimization is limited to situations in which a method or nested function calls itself directly as itslast operation, without going through a function value or some other intermediary.下面几个都不行 */ def isEven(x: Int): Boolean = if (x == 0) true else isOdd(x - 1) def isOdd(x: Int): Boolean = if (x == 0) false else isEven(x - 1) val funValue = nestedFun _ def nestedFun(x: Int) { if (x != 0) { println(x); funValue(x - 1) } } }}