Scala Partial Function从官方文档解析
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从官方文档解析的更多相关文章
- Android Google官方文档解析之——Application Fundamentals
Android apps are written in the java programming language.The Android SDK tools compile your code-al ...
- node.js官方文档解析 02—buffer 缓冲器
Buffer 类的实例类似于整数数组,但 Buffer 的大小是固定的.且在 V8 堆外分配物理内存.Buffer 的大小在被创建时确定,且无法调整. Buffer 类在 Node.js 中是一个全局 ...
- Android Google官方文档解析之——Device Compatibility
Android is designed to run on many different types of devices, from phones to tablets and television ...
- node.js官方文档解析 01—assert 断言
assert-------断言 new assert.AssertionError(options) Error 的一个子类,表明断言的失败. options(选项)有下列对象 message < ...
- sanic官方文档解析之Example(二)
1,通过docker部署sanic项目 通过Docker和Docker Compose部署SANIC应用程序是一项很容易实现的任务,下面的示例提供了示例simple_server.py的部署 FROM ...
- sanic官方文档解析之Deploying(部署)和Extension(扩展)
1,Deploying(部署) 通过内置的websocket可以很简单的部署sanic项目,之后通过实例sanic.Sanic,我们可以运行run这个方法通过接下来的关键字参数 host (defau ...
- sanic官方文档解析之Example(一)
1,示例 这部的文档是简单的示例集合,它能够帮助你快速的启动应用大部分的应用,这些应用大多事分类的,并且提供给ini工作的连接代码: 1.1,基础示例 这部分示例集成了提供简单sanic简单的代码 单 ...
- sanic官方文档解析之ssl,debug mode模式和test(测试)
1,ssl 示例: 可选择的SSLContent from sanic import Sanic import ssl context = ssl.create_default_context(pur ...
- sanic官方文档解析之Custom Protocols(自定义协议)和Socket(网络套接字)
1,Custom Protocol:自定义协议 温馨提示:自定义协议是一个高级用法,大多数的读者不需要用到此功能 通过特殊的自定义协议,你可以改变sanic的协议,自定义协议需要继承子类asyncio ...
随机推荐
- maven 配置国内镜像仓库加速获取jar包的配置方法
在 maven 的 conf/settings.xml 中配置 <mirrors> <!-- mirror | Specifies a repository mirror site ...
- LeetCode 56. 合并区间(Merge Intervals)
题目描述 给出一个区间的集合,请合并所有重叠的区间. 示例 1: 输入: [[1,3],[2,6],[8,10],[15,18]] 输出: [[1,6],[8,10],[15,18]] 解释: 区间 ...
- http请求状态码解析
1xx(临时响应) 2xx(成功) 3xx(重定向) 4xx(请求错误) http状态码 400 (错误请求) 服务器不理解请求的语法. http状态码 401 (未授权) 请求要求身份验证. 对于需 ...
- Android 客户端应用开发结构框架
本文算是一篇漫谈,谈一谈关于android开发中工程初始化的时候如何在初期我们就能搭建一个好的架构.关于android架构,因为手机的限制,目前我觉得也确实没什么大谈特谈的,但是从开发的角度,看到整齐 ...
- Java中的低级错误
1. 不能用“==”比较两个字符串内容相等. 2. 对list做foreach循环时,循环代码中不能修改list的结构. 3. ...
- 阶段3 2.Spring_05.基于XML的IOC的案例1_1 基于XML的IOC的案例-案例准备
导坐标 创建数据库表 create table account( id int primary key auto_increment, name varchar(40), money float )c ...
- 十七:jinja2之宏
宏有点类似于函数,接收一些数据,进行处理,但是没有返回值,可以将一些常用的代码片段放到宏中,然后把不固定的值抽出来做变量 使用maacro来定义宏,使用宏的时候,参数可以设默认值 自定义一个input ...
- 布局复习---BFC
其实在一开始我是没有BFC的这个概念的,只是知道在浮动过后,后续的元素如果出现问题,就做我们常说的:overflow:hidden.其中的原因还是不甚了解.不是说以前老师没有讲解过,而是以前根本就没有 ...
- java:容器/集合(Map(HashMap,TreeMap)Collection和Collections,(Collection-value();)
*Map接口:采用是键值对的方式存放数据.无序 HashMap: *常见的实现类: *--HashMap:基于哈希表的 Map 接口的实现. *常用的构造方法: * HashMap()构造一个具有默认 ...
- 【MM系列】SAP S/4 HANA 1511的BP角色创建及供应商数据的创建方法
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[MM系列]SAP S/4 HANA 1511的 ...