scala函数式编程&柯里化&偏函数

函数式:实现了某个特质的对象,有22个function

编程语言的范式:

    命令式:面向过程、面向对象

    函数式:hashkey,scala

 

lambda函数

object ObjectDemo {
   def main(args: Array[String]): Unit = {
     //println(sum(4,8))
     
     //println(Fsum(3,9))
     
     val sum = new Function2[Int,Int,Int](){
       def apply(x:Int, y:Int):Int = {
         x+y
       }
     }
     println(sum.apply(9, 8));
     
//     val MyFun = new SumFunction
//     println(MyFun.func(22, 11))
     
     val MyFun = new MyFunction2[Int, Int, Int]{
       def func(x:Int, y:Int):Int={
         x + y
       }
     }
     //println(MyFun.func(3, 7))
     
     //testFunction1(Fsum);
     testFunction2((x, y) => x * y);
   }

   def testFunction1(f : MyFunction2[Int,Int,Int]):Unit = {
     val a = 33
     val b = 55
     println(f.func(a, b));
   }

    def testFunction2(f : Function2[Int,Int,Int]):Unit = {
     val a = 3
     val b = 9
     println(f(a, b));
   }
   
   //方法
   def sum(x:Int, y:Int):Int={
     x + y
   }

   //函数-----是一个Lambda表达式,它背后有就一个函数式接口
   val Fsum = (x:Int, y:Int)=> x + y
}

trait MyFunction2[X, Y, Z]{
  def func(a:X, b:Y):Z 
}

class SumFunction extends MyFunction2[Int, Int, Int]{
   def func(x:Int, y:Int):Int={
     x + y
   }
}

 

柯里化

class CurryDemo {
  //定义高阶函数
	def func2(op:(Int,Int) => Int, a:Int, b:Int):Int={
	  op(a,b)
	}
  def func1(op:Function2[Int,Int,Int], a:Int, b:Int):Int = op(a,b)

 // println(func1((x,y)=>x*y, 3, 4))
  
  
  def func3(op:(Int, Int) => Int, a:Int, b:Int):(Int,Int)=>Int ={
    op
  }
  
  val f9 = func3((x,y)=>x*y,4,6)
  //println(f.toString())
  //println(f9(4,9))

	//调用高阶函数
	val f = func3(add,1,2)
	//println(f(6, 9))
	
	//调用函数
	def add(a:Int,b:Int) = a + b
	
	//柯里化----------逻辑数学----- 这个世界基本的组成元素就是 a=>a
	def plusBy1(factor:Int) = (x:Int) => x + factor
	def plusBy(factor:Int) = {
	   (x:Int) => x + factor
	}
	
	val f1 = plusBy(3)
	println(f1(5))
	
	val r1 = plusBy(3)(5)
	println(r1)
//	
  def add2(a:Int) = (b:Int) => a + b
  println(add2(3)(4))

}

object CurryDemo{
  def main(args: Array[String]): Unit = {
    new CurryDemo
  }
}

偏函数

//偏函数 ------- Partial Function

object PartialFunctionDemo2 {
  def main(args: Array[String]): Unit = {
    val signal:PartialFunction[Int,Int] = {
      case x if x > 0 => 1
      case x if x < 0 => -1
    }
    if(signal.isDefinedAt(0) == true){
       println(signal(0))
    }
    
    val compose_signal:PartialFunction[Int,Int] = signal.orElse{
      case 0 => 0
    }
    
    println(compose_signal(6))
    //println(signal(0))
    
    val new_signal:Function[Int,Int] = signal.compose{
      case x => x - 1
    }
    println(signal(4))
    println(new_signal(4))
    
    
  }
}

偏应用函数

//偏应用函数  Partial applied Function---------- 科里化是偏应用函数的特殊形式

object PartialApplyFunctionDemo {
  def main(args: Array[String]): Unit = {
    def add(x:Int,y:Int,z:Int) = x+y+z

    def addX = add(1,_:Int,_:Int) // x 已知
    println(addX(2,3))

    def addXAndY = add(10,100,_:Int) // x 和 y 已知
    println(addXAndY(1))
    
    def addZ = add(_:Int,_:Int,10) // z 已知
    println(addZ(1,2))
    
    //def fadd = add(_:Int, _:Int, _:Int)
    def fadd = add _
    
    val f3= fadd(1,2,_:Int)
    println(f3(3))
  }
}

 

posted @ 2017-12-25 00:00  appointint  阅读(138)  评论(0编辑  收藏  举报