scala 函数编程
scala 中的函数式一等公民 可以独立存在 可以将函数作为值赋给变量
scala> def sayHello(name:String) {println(" hello "+name)}
sayHello: (name: String)Unit
scala> sayHello("cat")
hello cat
注意这里的 空格 下划线
scala> val sayHelloFunc = sayHello _
sayHelloFunc: String => Unit = $$Lambda$1210/524541191@26db2470
scala> sayHelloFunc(" hadoop ")
hello hadoop
----------------------------------
匿名函数
注意这里的 =>
scala> val helloFunc = (name:String)=>{ println("--hello--"+name)}
helloFunc: String => Unit = $$Lambda$1221/1679266544@472bf56c
scala> helloFunc(" scala ")
--hello-- scala
scala>
----------------------------------
高阶函数
接收函数作为参数
scala> val helloFunc = (name:String)=>{ println("--hello--"+name)}
helloFunc: String => Unit = $$Lambda$1222/1726168677@2c3d1e97
scala> def greeting(func:(String)=> Unit ,name:String){ func(name)}
greeting: (func: String => Unit, name: String)Unit
scala> greeting(helloFunc," hive ")
--hello-- hive
----------------------------------
函数的返回值 为函数
scala> def mulBy(factor:Double)=(x:Double)=>factor*x
mulBy: (factor: Double)Double => Double
scala> val mulBy2 = mulBy(2)
mulBy2: Double => Double = $$Lambda$1350/973028061@775d7f5d
scala> mulBy2(1)
res18: Double = 2.0
scala> mulBy2(2)
res19: Double = 4.0
scala> mulBy2(3)
res20: Double = 6.0
----------------------------------