A partial function of type PartialFunction[A, B] is a unary function where the domain does not necessarily include all values of type A.

一个PartialFunction[A, B]类型的偏函数是一个一元函数。它的输入值的范围并不包含所有A类型的值。

The function isDefinedAtallows to test dynamically if a value is in the domain of the function.

给定一个A类型的值,isDefinedAt函数可以用来动态地检测这个值是否属于此偏函数的输入范围。

Even if isDefinedAt returns true for an a: A, calling apply(a) may still throw an exception, so the following code is legal:

val f: PartialFunction[Int, Any] = { case _ => 1/0 }

如果有一个a: A,虽然isDefinedAt返回true,但apply(A)可能会返回一个异常。

It is the responsibility of the caller to call isDefinedAt before calling apply, because if isDefinedAt is false, it is not guaranteed apply will throw an exception to indicate an error condition. If an exception is not thrown, evaluation may result in an arbitrary value.

调用者有责任在调用apply之前先调用isDefinedAt来做检查。因为如果isDefinedAt返回了false,apply方法并不能保证能做出抛异常的回应,有可能返回了意料之外的值。

The main distinction between PartialFunction and scala.Function1 is that the user of a PartialFunction may choose to do something different with input that is declared to be outside its domain. For example:

偏函数和Function1的区别是,偏函数的用户可以选择对输入域以外的输入值做一些特殊的处理。

val sample = 1 to 10
val isEven: PartialFunction[Int, String] = {
case x if x % 2 == 0 => x+" is even"
} // the method collect can use isDefinedAt to select which members to collect
val evenNumbers = sample collect isEven val isOdd: PartialFunction[Int, String] = {
case x if x % 2 == 1 => x+" is odd"
} // the method orElse allows chaining another partial function to handle
// input outside the declared domain
val numbers = sample map (isEven orElse isOdd)

Scala Partial Function从官方文档解析的更多相关文章

  1. Android Google官方文档解析之——Application Fundamentals

    Android apps are written in the java programming language.The Android SDK tools compile your code-al ...

  2. node.js官方文档解析 02—buffer 缓冲器

    Buffer 类的实例类似于整数数组,但 Buffer 的大小是固定的.且在 V8 堆外分配物理内存.Buffer 的大小在被创建时确定,且无法调整. Buffer 类在 Node.js 中是一个全局 ...

  3. Android Google官方文档解析之——Device Compatibility

    Android is designed to run on many different types of devices, from phones to tablets and television ...

  4. node.js官方文档解析 01—assert 断言

    assert-------断言 new assert.AssertionError(options) Error 的一个子类,表明断言的失败. options(选项)有下列对象 message < ...

  5. sanic官方文档解析之Example(二)

    1,通过docker部署sanic项目 通过Docker和Docker Compose部署SANIC应用程序是一项很容易实现的任务,下面的示例提供了示例simple_server.py的部署 FROM ...

  6. sanic官方文档解析之Deploying(部署)和Extension(扩展)

    1,Deploying(部署) 通过内置的websocket可以很简单的部署sanic项目,之后通过实例sanic.Sanic,我们可以运行run这个方法通过接下来的关键字参数 host (defau ...

  7. sanic官方文档解析之Example(一)

    1,示例 这部的文档是简单的示例集合,它能够帮助你快速的启动应用大部分的应用,这些应用大多事分类的,并且提供给ini工作的连接代码: 1.1,基础示例 这部分示例集成了提供简单sanic简单的代码 单 ...

  8. sanic官方文档解析之ssl,debug mode模式和test(测试)

    1,ssl 示例: 可选择的SSLContent from sanic import Sanic import ssl context = ssl.create_default_context(pur ...

  9. sanic官方文档解析之Custom Protocols(自定义协议)和Socket(网络套接字)

    1,Custom Protocol:自定义协议 温馨提示:自定义协议是一个高级用法,大多数的读者不需要用到此功能 通过特殊的自定义协议,你可以改变sanic的协议,自定义协议需要继承子类asyncio ...

随机推荐

  1. 浏览器端-W3School-HTML:HTML DOM Script 对象

    ylbtech-浏览器端-W3School-HTML:HTML DOM Script 对象 1.返回顶部 1. HTML DOM Script 对象 Script 对象 Script 对象表示 HTM ...

  2. 学习笔记 - MarkDown 语法

    学习参考网址:https://www.appinn.com/markdown/index.html # **gitskill** ## 标题 ># 这是 H1 >## 这是 H2 > ...

  3. 我非要捅穿这 Neutron(一)网络实现模型篇

    目录 文章目录 目录 前言 传统网络到虚拟化网络的演进 单一平面网络到混合平面网络的演进 Neutron 简述 Neutron 的网络实现模型 计算节点网络实现模型 内外 VID 转换 网络节点网络实 ...

  4. nginx禁止指定的user_agent访问

    #禁止指定user_agent(浏览器标识)if ($http_user_agent ~ 'curl|baidu'){ return 403;} if ($http_user_agent ~* 'cu ...

  5. SAE Django如何禁止外部IP访问

    在SAE上基于Django搭建的Web工程有时需要禁止来自某些特定IP地址的访问请求. 例如一个为搭建在SAE的其他项目提供服务的内部工程,可以设置为只允许SAE内部的IP地址访问,从而提高项目的安全 ...

  6. angularjs的部分总结

    就在这个星期,我们学习了一个神奇的框架叫:"Angular js",它的神奇之处不是它的功能有多强,甚至它的功能还是很简陋的,但是它的那种思想是非常牛逼的;他完全抛弃了我们现在所常 ...

  7. java:Mybatis框架2(基于mapper接口的开发,多种查询,复合类型查询,resultMap定义,多表联查,sql片段)

    1.mybatis02: mybatis-config.xml: <?xml version="1.0" encoding="UTF-8"?> &l ...

  8. Selenium+java自动化测试常用知识点

    一.元素的定位 1.通过ID定位元素: findElement(By.id(element)); 2.通过元素的名称定位元素: findElement(By.name(element)); 3.通过元 ...

  9. C#学习笔记四(LINQ,错误和异常,异步编程,反射元数据和动态编程)

    LINQ 1.使用类似的数据库语言来操作集合? 错误和异常 异步编程 1.异步和线程的区别: 多线程和异步操作两者都可以达到避免调用线程阻塞的目的.但是,多线程和异步操作还是有一些区别的.而这些区别造 ...

  10. 简述Js中,判断对象为空对象的几种方式

    1.空对象.空引用以及undefined三种概念的区别 空对象:是对象,但它的值是指向没有任何属性的对象的引用, {}是指 不含任何属性 的对象,当然对象属性包括 字面值和函数: 空引用:obj=nu ...