偏函数

偏函数(Partial Function),是一个数学概念它不是”函数”的一种, 它跟函数是平行的概念。
Scala中的Partia Function是一个Trait,其的类型为PartialFunction[A,B],其中接收一个类型为A的参数,返回一个类型为B的结果。

偏函数内部有一些方法,比如isDefinedAt、OrElse、 andThen、applyOrElse等等。

  1. scala> val pf:PartialFunction[Int,String] = {
  2. | case 1=>"One"
  3. | case 2=>"Two"
  4. | case 3=>"Three"
  5. | case _=>"Other"
  6. | }
  7. pf: PartialFunction[Int,String] = <function1>
  8. scala> pf(1)
  9. res0: String = One
  10. scala> pf(2)
  11. res1: String = Two
  12. scala> pf(3)
  13. res2: String = Three
  14. scala> pf(4)
  15. res3: String = Other

isDefinedAt

isDefinedAt : 这个函数的作用是判断传入来的参数是否在这个偏函数所处理的范围内。
刚才定义的pf来尝试使用isDefinedAt(),只要是数字都是正确的,因为有case _=>”Other”这一句。如果换成其他类型就会报错

  1. scala> pf.isDefinedAt(1)
  2. res4: Boolean = true
  3. scala> pf.isDefinedAt(2)
  4. res5: Boolean = true
  5. scala> pf.isDefinedAt("1")
  6. <console>:13: error: type mismatch;
  7. found : String("1")
  8. required: Int
  9. pf.isDefinedAt("1")
  10. ^
  11. scala> pf.isDefinedAt(100)
  12. res7: Boolean = true

那我们再定义一个PartialFunction
去掉了原先的最后一句,再执行anotherPF.isDefinedAt(4)会返回false

  1. scala> val anotherPF:PartialFunction[Int,String] = {
  2. | case 1=>"One"
  3. | case 2=>"Two"
  4. | case 3=>"Three"
  5. | }
  6. anotherPF: PartialFunction[Int,String] = <function1>
  7. scala> anotherPF.isDefinedAt(1)
  8. res8: Boolean = true
  9. scala> anotherPF.isDefinedAt(2)
  10. res9: Boolean = true
  11. scala> anotherPF.isDefinedAt(3)
  12. res10: Boolean = true
  13. scala> anotherPF.isDefinedAt(4)
  14. res11: Boolean = false

orElse

orElse : 将多个偏函数组合起来使用,效果类似case语句
这样,newPF跟原先的pf效果是一样的

  1. scala> val onePF:PartialFunction[Int,String] = {
  2. | case 1=>"One"
  3. | }
  4. onePF: PartialFunction[Int,String] = <function1>
  5. scala> val twoPF:PartialFunction[Int,String] = {
  6. | case 2=>"Two"
  7. | }
  8. twoPF: PartialFunction[Int,String] = <function1>
  9. scala> val threePF:PartialFunction[Int,String] = {
  10. | case 3=>"Three"
  11. | }
  12. threePF: PartialFunction[Int,String] = <function1>
  13. scala> val otherPF:PartialFunction[Int,String] = {
  14. | case _=>"Other"
  15. | }
  16. otherPF: PartialFunction[Int,String] = <function1>
  17. scala> val newPF = onePF orElse twoPF orElse threePF orElse otherPF
  18. newPF: PartialFunction[Int,String] = <function1>
  19. scala> newPF(1)
  20. res0: String = One
  21. scala> newPF(2)
  22. res1: String = Two
  23. scala> newPF(3)
  24. res2: String = Three
  25. scala> newPF(4)
  26. res3: String = Other

andThen

andThen: 相当于方法的连续调用,比如g(f(x))
pf1的结果返回类型必须和pf2的参数传入类型必须一致,否则会报错

  1. scala> val pf1:PartialFunction[Int,String] = {
  2. | case i if i == 1 => "One"
  3. | }
  4. pf1: PartialFunction[Int,String] = <function1>
  5. scala> val pf2:PartialFunction[String,String] = {
  6. | case str if str eq "One" => "The num is 1"
  7. | }
  8. pf2: PartialFunction[String,String] = <function1>
  9. scala> val num = pf1 andThen pf2
  10. num: PartialFunction[Int,String] = <function1>
  11. scala> num(1)
  12. res4: String = The num is 1

applyOrElse

applyOrElse:它接收2个参数,第一个是调用的参数,第二个是个回调函数。如果第一个调用的参数匹配,返回匹配的值,否则调用回调函数

  1. scala> onePF.applyOrElse(1,{num:Int=>"two"})
  2. res5: String = One
  3. scala> onePF.applyOrElse(2,{num:Int=>"two"})
  4. res6: String = two

在这个例子中,第一次onePF匹配了1成功则返回的是”One”字符串。第二次onePF匹配2失败则触发回调函数,返回的是”Two”字符串。

偏应用函数

偏应用函数(Partial Applied Function)也叫部分应用函数,跟偏函数(Partial Function)从英文名来看只有一字之差,但他们二者之间却有天壤之别。
部分应用函数, 是指一个函数有n个参数, 而我们为其提供少于n个参数, 那就得到了一个部分应用函数。
个人理解的偏应用函数类似于柯里化
举个例子,定义好一个函数有3个参数,再提供几个有1-2个已知参数的偏应用函数

  1. scala> def add(x:Int,y:Int,z:Int) = x+y+z
  2. add: (x: Int, y: Int, z: Int)Int
  3. scala> def addX = add(1,_:Int,_:Int) // x 已知
  4. addX: (Int, Int) => Int
  5. scala> addX(2,3)
  6. res1: Int = 6
  7. scala> addX(3,4)
  8. res2: Int = 8
  9. scala> def addXAndY = add(10,100,_:Int) // x 和 y 已知
  10. addXAndY: Int => Int
  11. scala> addXAndY(1)
  12. res3: Int = 111
  13. scala> def addZ = add(_:Int,_:Int,10) // z 已知
  14. addZ: (Int, Int) => Int
  15. scala> addZ(1,2)
  16. res4: Int = 13