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 ...
随机推荐
- 自动化框架Quantum Automation Framework+cucumber+perfecto
名词解释 Quantum: 一款基于JAVA的自动化框架,支持手机和桌面WEB的自动化测试.与cucumber和perfecto实现了整合,用于BDD自动化. Refer: http://projec ...
- csp2019 Emiya家今天的饭题解
qwq 由于窝太菜了,实在是不会,所以在题解的帮助下过掉了这道题. 写此博客来整理一下思路 正文 传送 简化一下题意:现在有\(n\)行\(m\)列数,选\(k\)个数的合法方案需满足: 1.一行最多 ...
- Python基本语法_函数_返回值
目录 目录 前言 函数的返回值 在函数中Return和Print的区别 函数中的文档 前言 函数的返回值是函数重要的组成部分.函数的根本在于实现程序的部分功能,所以很多时候我们需要将函数执行后的结果返 ...
- 分布式任务celery
Celery的架构由三部分组成,消息中间件(message broker),任务执行单元(worker)和任务执行结果存储(task result store)组成. 消息中间件 Celery本身不提 ...
- 64位编译器下,将指针转换成UINT32,不需要修改编译选项的编码方式
一些严格的64位编译器,将指针转换成UINT32,会报各种丢失精度的错误. 但很显然,有些时候,我们就是需要转换,且并不会真正丢失精度. 此时不需要修改编译选项的编码方式,有些用处了 示例如下: un ...
- Spring容器启动源码分析
1. 前言 最近搭建的工程都是基于SpringBoot,简化配置的感觉真爽.但有个以前的项目还是用SpringMvc写的,看到满满的配置xml文件,却有一种想去深入了解的冲动.折腾了好几天,决心去写这 ...
- gin框架教程一: go框架gin的基本使用
我们在用http的时候一般都会用一些web框架来进行开发,gin就是这样的一个框架,它有哪些特点呢 一:gin特点 1.性能优秀2.基于官方的net/http的有限封装3.方便 灵活的中间件4.数据绑 ...
- CPU 使用率低 but负载高
一.关于负载 什么是负载:负载就是cpu在一段时间内正在处理以及等待cpu处理的进程数之和的统计信息,也就是cpu使用队列的长度统计信息,这个数字越小越好(如果超过CPU核心*0.7就是不正常) 负载 ...
- 【VS开发】【智能语音处理】语音信号处理之(一)动态时间规整(DTW)
语音信号处理之(一)动态时间规整(DTW) zouxy09@qq.com http://blog.csdn.net/zouxy09 这学期有<语音信号处理>这门课,快考试了,所以也要了解了 ...
- 9.Jmeter 多个threadgroup 中的配置元件会一次性进行初始化
例如3个threadGroup,每一个threadGroup中都会定义了 一些配置原件,例如 用户定义变量, jdbc 链接配置等. 当执行testplan(测试计划)时, 这些配置元件会一起初始 ...