Partial Functions(偏函数)
来源:互联网 发布:淘宝网的运营模式是() 编辑:程序博客网 时间:2024/06/07 01:52
1.Definition
Partial functions are partial in the sense that they aren’t defined for all possible inputs, only those inputs that match at least one of the specified case
clauses.
Only case
clauses can be specified in a partial function and the entire function must be enclosed in curly braces. In contrast, “regular” function literals can be wrapped in parentheses or curly braces.
If the function is called with an input that doesn’t match one of the case clauses, a MatchError
is thrown at runtime.
code example
package com.brown/** * Created by BrownWong on 2016/9/29. */object Hello { def main(args: Array[String]): Unit = { val pf: PartialFunction[Any, String] = {case s:String => "Yes"} println(pf("HAHA")) println(pf(12)) }}
output
YesException in thread "main" scala.MatchError: 12 (of class java.lang.Integer) at scala.PartialFunction$$anon$1.apply(PartialFunction.scala:253)at scala.PartialFunction$$anon$1.apply(PartialFunction.scala:251) at com.brown.Hello$$anonfun$1.applyOrElse(Hello.scala:8) at scala.runtime.AbstractPartialFunction.apply(AbstractPartialFunction.scala:36) at com.brown.Hello$.main(Hello.scala:10) at com.brown.Hello.main(Hello.scala) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:483) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
2.isDefinedAt()
method
You can test if a Partial Function will match an input using the isDefinedAt
method. This function avoids the risk of throwing a MatchError
exception.
code example
package com.brown/** * Created by BrownWong on 2016/9/29. */object Hello { def main(args: Array[String]): Unit = { val pf: PartialFunction[Any, String] = {case s:String => "Yes"} println(pf.isDefinedAt("HAHA")) println(pf.isDefinedAt(12)) }}
output
truefalse
3.“chain” Partial Functions
You can “chain” PartialFunctions together: pf1 orElse pf2 orElse pf3 ….
If pf1
doesn’t match, then pf2
is tried, then pf3
, etc. A MathError
is only thrown if none of them matches.
code example
package com.brown/** * Created by BrownWong on 2016/9/29. */object Hello { def main(args: Array[String]): Unit = { val pf1: PartialFunction[Any, String] = {case s:String => "Yes"} val pf2: PartialFunction[Any, String] = {case s:Int => "Yes"} val pf3 = pf1 orElse pf2 println(pf3.isDefinedAt("HAHA")) println(pf3.isDefinedAt(12)) }}
output
truetrue
Ref
《Programming Scala》
- Partial Functions(偏函数)
- python偏函数partial
- 偏函数functools.partial
- 【python】偏函数 functools partial
- 偏函数(Partial-Function)
- 飘逸的python - 偏函数functools.partial
- Scala之偏函数Partial Function
- 飘逸的python - 偏函数functools.partial
- python进阶8:偏函数functools.partial
- Scala之偏函数Partial Function
- python 中偏函数 partial 的使用
- python 中偏函数 partial 的使用
- python 偏函数(functoosl, partial)
- python的偏函数应用partial
- Python3学习(18)--偏函数(Partial)
- Python基础-偏函数 Partial function
- Scala: 偏函数(PartialFunction) && 偏应用函数(Partial Applied Function)
- python中的partial函数
- shell脚本——正则表达式
- sessionStorage 、localStorage 和 cookie 之间的区别
- Java-IO之RandomAccessFile
- LeetCode oj 283. Move Zeroes (选择排序)
- 41. First Missing Positive
- Partial Functions(偏函数)
- 按钮的callback
- XML学习笔记(1)-DTD
- 【周赛】第一周周赛——欢迎16级的新同学题解(题目出自codeforces 318A,546A,431C,665E,HDU 4104)
- (Java)LeetCode-58. Length of Last Word
- nyist 1272 表达式求值 (河南省第九届省赛) 中缀转后缀
- atoi和stoi
- Java基础 关于强引用,软引用,弱引用,虚引用
- 百度导航切换