scala之柯里化

scala中可以定义一个函数

 //1.柯里化,注意cookFood与cookFood1、cookFood2是同一个方法,但是参数展示的形式被分开,这就是柯里化。
def cookFood(potato:String,tomato:String,cook:(String)=>Unit): Unit ={
  val food = potato+tomato
  cook("做熟"+food)
}
 def cookFood1(potato:String,tomato:String)(cook:(String)=>Unit): Unit ={
   val food = potato+tomato
   cook("做熟"+food)
 }

 def cookFood2(potato:String)(tomato:String)(cook:(String)=>Unit): Unit ={
   val food = potato+tomato
   cook("做熟"+food)
 }

//2、由柯里化引申出的自定义处理结构。最后一个参数,传参的时候可以写成大括号

    cookFood1("土豆","西红柿"){
      println(_)
    }

//3、scala中柯里化实例

  val list = List(1,2,3,4,5,6)
      list.foreach((x:Int)=>{val r = x*10;println(r)})
      list.foreach((x:Int)=>println(x))

猜你喜欢

转载自blog.csdn.net/starkpan/article/details/86609950