定义本地函数示例
/**
* 自定义数据处理函数
* @param fileName
* @param width
*/
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)
}
调用执行示例
package kmust.hjr.learningScala20
import scala.io.Source
/**
* Created by Administrator on 2015/10/17.
*/
object FunctionOps {
def main(args:Array[String]){
processData("E:\\scalaIO.txt",0)
}
函数应用
var increase=(x:Int) => x+1
println(increase(10))
increase=(x:Int) => x+9999
//遍历并打印所有值
val someNumbers=List(-11,-10,-5,0,5,10)
someNumbers.foreach((x:Int)=>println(x))
//过滤打印大于0的值
//someNumbers.filter((x:Int) => x>0)
println(someNumbers.filter((x:Int) => x>0))
//someNumbers.filter((x) => x>0)
println(someNumbers.filter((x) => x>0))
//someNumbers.filter(x => x>0)
println(someNumbers.filter(x => x>0))
//someNumbers.filter(_>0)
println(someNumbers.filter(_>0))
val f=(_:Int)+(_:Int)
f(5,10)
println(f(5,10))