内容:
1、本地函数代码实战(函数内部函数)
2、作为一等公民的函数的代码实战
var increase = (x: Int) => x + 1
println(increase(10))
increase = (x: Int) => x + 9999
//将函数(一等公民)作为一个值进行赋值
def processData(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)
模块化的内部函数,体现了代码的“强内聚,弱耦合”特征
函数processLine为函数processDate的内部函数也为私有函数,外部不可访问,是函数操作的实现即打印满足条件的数据。Source读取文件,调用函数processLine进行函数实现。