Partial函数的定义

scala> val isVeryTasty: PartialFunction[String, String] = { case "Glazed Donut" | "Strawberry Donut" => "Very Tasty"}
isVeryTasty: PartialFunction[String,String] = <function1>

scala> isVeryTasty("Glazed Donut")
res3: String = Very Tasty

Partianl函数的组合使用:

code :

  println("\nStep 1: How to define a Partial Function named isVeryTasty")
val isVeryTasty: PartialFunction[String, String] = { case "Glazed Donut" | "Strawberry Donut" => "Very Tasty"} println("\nStep 2: How to call the Partial Function named isVeryTasty")
println(s"Calling partial function isVeryTasty = ${isVeryTasty("Glazed Donut")}")
// NOTE: you will get scala.MatchError println("\nStep 3: How to define PartialFunction named isTasty and unknownTaste")
val isTasty: PartialFunction[String, String] = {
case "Plain Donut" => "Tasty"
} val unknownTaste: PartialFunction[String, String] = {
case donut @ _ => s"Unknown taste for donut = $donut"
} println("\nStep 4: How to compose PartialFunction using orElse")
val donutTaste = isVeryTasty orElse isTasty orElse unknownTaste
println(donutTaste("Glazed Donut"))
println(donutTaste("Plain Donut"))
println(donutTaste("Chocolate Donut"))

result:

Step : How to define a Partial Function named isVeryTasty

Step : How to call the Partial Function named isVeryTasty
Calling partial function isVeryTasty = Very Tasty Step : How to define PartialFunction named isTasty and unknownTaste Step : How to compose PartialFunction using orElse
Very Tasty
Tasty
Unknown taste for donut = Chocolate Donut

learning scala PartialFunction的更多相关文章

  1. scala PartialFunction

    1.orElse和andThen的区别 源码如下,区别很明显,orElse是并列的关系,而andThen是调用者的结果作为k的输入. trait PartialFunction[-A, +B] ext ...

  2. learning scala 数组和容器

    数组:可变的,可索引的,元素具有相同类型的数据集合 一维数组 scala> val intValueArr = new Array[Int](3)intValueArr: Array[Int] ...

  3. learning scala control statement

    1 .if satement 与其它语言不同的是,scala if statement 返回的是一个值 scala> val a = if ( 6 > 0 ) 1 else -1a: In ...

  4. learning scala read from file

    scala读文件:   example: scala> import scala.io.Sourceimport scala.io.Source scala> var inputFile ...

  5. learning scala write to file

    scala 写文件功能: scala> import java.io.PrintWriterimport java.io.PrintWriter scala> val outputFile ...

  6. learning scala output to console

    控制台输出语句: print println example: scala> print("i=");print(i)i=345scala> println(" ...

  7. learning scala read from console

    控制台输入语句: readInt, readDouble, readByte, readShort, readLong, readChar, readBoolean, readLine example ...

  8. learning scala 变量

    scala 变量: val : 声明时,必须被初始化,不能再重新赋值. scala> test = "only1"<console>:11: error: not ...

  9. learning scala 操作符

    scala 操作符: 算术运算符:  +  - *  / % 关系统运算符: > , < ,= ,!= ,>=,<=, 逻辑运算符: && . || , ! 位 ...

随机推荐

  1. ActiveMQ处理Message(String -javabean)

    一.ActiveMq想要实现必备的六要素(基于jms) //链接工厂.用于创建链接 private ConnectionFactory factory; //用于访问Mq的链接,由链接工厂创建 pri ...

  2. BUG:WSL 的 ssh server 无法启动

    BUG 使用 sudo service ssh start 启动 ssh 服务,提示: * Restarting OpenBSD Secure Shell server sshd Could not ...

  3. Python 在气象上的应用

    Python 在气象上的应用 grug350关注 0.7892019.03.15 23:19:31字数 913阅读 1,024 为什么选择python 免费和开源,没有商业许可限制anaconda p ...

  4. ivew数控件Tree自定义节点内容示例分析

    ivew数控件Tree自定义节点内容示例分析 demo地址:https://run.iviewui.com/plcWlM4H <template> <Tree :data=" ...

  5. 基于【 MySql 】二 || mysql详细学习笔记

    mysql重点学习笔记 /* Windows服务 */ -- 启动MySQL net start mysql -- 创建Windows服务 sc create mysql binPath= mysql ...

  6. 【转】JRE和JDK的区别

    用一幅图来宏观的看一下 从图中可以看出JDK包含JRE包含JVM. JDK:java development kit (java开发工具) JRE:java runtime environment ( ...

  7. wepy框架 怎么在template模板中使用函数

    呵呵.介绍说是类似vue,用起来真累人,就想在模板中使用个函数都要查N久的文档才知道. 具体要怎么操作呢? 要先创建个wxs脚本文件,在里面定义函数或其它的,然后在页面或组件中引入这文件,就可以在模板 ...

  8. rem em min-width: 30em 的意思

    30em=30rem=30x16px=480px @media only screen and (min-width:30 em){ }

  9. vue 将时间戳转换成日期格式 (一)

    (1)创建一个处理时间格式的js,内容如下: ../../utils/formatDate.js export function formatDate(date, fmt) { if (/(y+)/. ...

  10. 小数据玩转Pyspark(2)

    一.客户画像 客户画像应用:精准营销(精准预测.个性化推荐.联合营销):风险管控(高风险用户识别.异常用户识别.高可疑交易识别):运营优化(快速决策.产品组合优化.舆情分析.服务升级):业务创新(批量 ...