标签:valueat ceil object code imp style import 1.4 color
1.作为值的函数
import scala.math._ object Test extends App { //scala中函数是头等公民,可以像其他类型一样被传递 val num = 3.14 val fun: Double => Double = ceil _ //将ceil方法转成函数 //调用函数 println(fun(4)) //传递给其他函数 val a = Array(3.14,1.42,2.5).map(fun) }
2.匿名函数
object Test extends App { //匿名函数 val fun1 = (x: Double) => x * 3 def fun2(x: Double) = { x * 3 } }
3.带函数参数的函数
import scala.math._ object Test extends App { def valueAtOneQuarter(num: Double, f: Double => Double) = { f(num) } val result = valueAtOneQuarter(25, ceil _) println(result) val a: (Double, Double => Double) => Double = valueAtOneQuarter _ //函数类型 (Double, Double => Double) => Double //函数产出函数 def mulBy(factor:Double) = { (x:Double)=>x*3 } val b: Double => Double => Double = mulBy _ }
4.
5.
6.
7.
8.
标签:valueat ceil object code imp style import 1.4 color
原文地址:https://www.cnblogs.com/yin-fei/p/10840380.html