如果你想定义一个函数,而让它只接受和处理其参数定义域范围内的子集,对于这个参数范围外的参数则抛出异常,这样的函数就是偏函数(顾名思异就是这个函数只处理传入来的部分参数)。

偏函数是个特质其的类型为PartialFunction[A,B],其中接收一个类型为A的参数,返回一个类型为B的结果。
其有个重要的函数就是:
def isDefinedAt(x: A): Boolean //作用是判断传入来的参数是否在这个偏函数所处理的范围内

定义一个普通的除法函数:

scala> val divide = (x : Int) => 100/x
divide: Int => Int = <function1>
输入参数0
scala> divide(0)
java.lang.ArithmeticException: / by zero 
显然,当我们将0作为参数传入时会报错,一般的解决办法就是使用try/catch来捕捉异常或者对参数进行判断看其是否等于0;但是在Scala的偏函数中这些都已经封装好了,如下:将divide定义成一个偏函数()
val divide = new PartialFunction[Int,Int] {
def isDefinedAt(x: Int): Boolean = x != 0 //判断x是否等于0,当x = 0时抛出异常
def apply(x: Int): Int = 100/x
}  
但是,当偏函数与case语句结合起来就能使代码更简洁,如下:
val divide1 : PartialFunction[Int,Int] = {
case d : Int if d != 0 => 100/d //功能和上面的代码一样,这就是偏函数的强大之处,方便,简洁!!
} 
其实这代码就是对上面那段代码的封装,这里同样调用isDefinedAt方法
scala> divide1.isDefinedAt(0)
res1: Boolean = false scala> divide1.isDefinedAt(10)
res2: Boolean = true 
再举个与case结合使用的例子:
val rs : PartialFunction[Int , String] = {
case 1 => "One"
case 2 => "Two"
case _ => "Other"
}
输入:
scala> rs(1)
res4: String = One scala> rs(2)
res5: String = Two scala> rs(100)
res6: String = Other
 
OrElse
OrElse方法可以将多个偏函数组合起来使用,结合起来的效果类似case语句,但是每个偏函数里又可以再使用case
scala> val or1 : PartialFunction[Int,String] = {case 1 => "One"}
or1: PartialFunction[Int,String] = <function1> scala> val or2 : PartialFunction[Int,String] = {case 2 => "Two"}
or2: PartialFunction[Int,String] = <function1> scala> val or_ : PartialFunction[Int,String] = {case _ => "Other"}
or_: PartialFunction[Int,String] = <function1> scala> val or = or1 orElse or2 orElse or_ //使用orElse将多个偏结合起来
or: PartialFunction[Int,String] = <function1> scala> or(1)
res7: String = One scala> or(20)
res9: String = Other
orElse还可以直接连接case使用
scala> val orCase:(Int => String) = or1 orElse {case _ => "Other"}
orCase: Int => String = <function1> scala> orCase(1)
res10: String = One scala> orCase(10)
res11: String = Other
  
 
andThen
对函数的结果进行下一步的处理
scala> val at1 : PartialFunction[Int,String] = {case cs if cs == 1 => "One"}
at1: PartialFunction[Int,String] = <function1> scala> val at2 : PartialFunction[String,String] = {case cs if cs eq "One" => "The num is 1"}
at2: PartialFunction[String,String] = <function1> scala> val num = at1 andThen at2
num: PartialFunction[Int,String] = <function1> scala> num(1)
res18: String = The num is 1
 
注意:at1函数的结果返回类型必须和at2函数的参数传入类型一致,不然会编译报错
 

Scala-Partial Functions(偏函数)的更多相关文章

  1. Partial Functions(偏函数)

    如果你想定义一个函数,而让它只接受和处理其参数定义域范围内的子集,对于这个参数范围外的参数则抛出异常,这样的函数就是偏函数(顾名思异就是这个函数只处理传入来的部分参数). 偏函数是个特质其的类型为Pa ...

  2. Python partial function 偏函数

    Partial function 偏函数是将所要承载的函数作为partial()函数的第一个参数,原函数的各个参数依次作为partial()函数后续的参数,除非使用关键字参数. 当函数的参数个数太多, ...

  3. Python使用functools模块中的partial函数生成偏函数

    所谓偏函数即是规定了固定参数的函数,在函数式编程中我们经常可以用到,这里我们就来看一下Python使用functools模块中的partial函数生成偏函数的方法 python 中提供一种用于对函数固 ...

  4. Scala中的偏函数与部分应用函数

    Scala中有PartialFunction的概念, 同时还要一个概念叫Partial Applied Function. 前者译作偏函数, 后者译作"偏应用函数"或"部 ...

  5. Scala Partial Function从官方文档解析

    A partial function of type PartialFunction[A, B] is a unary function where the domain does not neces ...

  6. Scala:Functions and Closures

    object Functions { def main(args: Array[String]) { // 本地函数 def localFun(msg: String) = println(msg) ...

  7. Scala方法定义,方法和函数的区别,将方法转换成函数

    1. 定义方法和函数 1.1. 定义方法 方法的返回值类型可以不写,编译器可以自动推断出来,但是对于递归函数,必须指定返回类型 1.2. 定义函数 1.3.方法和函数的区别 在函数式编程语言中,函数是 ...

  8. Scala偏函数与部分函数

    函数 1.部分函数 部分应用函数(Partial Applied Function)是缺少部分参数的函数,是一个逻辑上概念. def sum(x: Int, y: Int, z: Int) = x + ...

  9. scala偏函数

    package com.ming.test /** * 在Scala中,偏函数是具有类型PartialFunction[-T,+V]的一种函数.T是其接受的函数类型,V是其返回的结果类型. * 偏函数 ...

随机推荐

  1. 【leetcode】Merge k Sorted Lists(按大小顺序连接k个链表)

    题目:Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity ...

  2. ORA-02069: global_names parameter must be set to TRUE for this operation

    原因:在对远程表增删改操作的时候,调用了本地函数.  比如:insert into trans_load_rate@DC values(rate_s(1)); trans_load_rate是DC库的 ...

  3. WndProc函数(转)

    WndProc函数作用: 主要用在拦截并处理系统消息和自定义消息 比如:windows程序会产生很多消息,比如你单击鼠标,移动窗口都会产生消息.这个函数就是默认的消息处理函数.你可以重载这个函数来制定 ...

  4. listener笔记

    listener 分四步: 在被观察者类中创建 onXXListener Interface,包含一个方法:xxxListener(object o),参数根据需要观察者需要设定. public in ...

  5. SQL获取选中时间的交集

    如上图:t1,t2代表要选择的时间段,t3,t4代表系统时间. 那么如果要获取选中时间段所有的交集为: 条件1 and ((t3>t1 and t1>t2) or (t3<t2 an ...

  6. java内部类实现多继承

    class Example1 { public String name() { return "liutao"; } } class Example2 { public int a ...

  7. Java的一点内容(2)

    1 面向对象的三个原则 封装性 封装的基本单元是类(class),类是一个抽象的逻辑结构,而类的对象是一个真实的物理实体:类的目的是封装复杂性,在类内部存在隐藏实现复杂性机制: 封装(encapsul ...

  8. hdu 油菜花王国

    Problem Description 程序设计竞赛即将到来,作为学校ACM集训队主力,小明训练一直很努力.今天天气不错,教练也心情大好,破例给各位队员放假一天,小明就骑着自己的小电驴到郊外踏青去了. ...

  9. mongodb内嵌文档的查询

    本文转自:http://blog.163.com/wm_at163/blog/static/1321734902012526103825481/ 1 > db.blog.findOne() { ...

  10. beini系列_2_beini装入虚拟机