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. Oracle 11g xe版本---总结1

    一.创建用户和授予权限 1.1 环境: Oracle 11g xe 第三方图形客户端: PLSQL Windows 10 必须登录 HR 用户,下面的查询会使用到 HR 中的表. 1.2 SQL 语句 ...

  2. 如何利用 iTunes 把 m4a/wav 文件转成 MP3 格式

    MAC技巧 | 如何利用 iTunes 把 m4a/wav 文件转成 MP3 格式 - 简书

  3. Android GridView去除自带边框点击效果、去除右侧滚动条、禁止上下滑动

    一.去除自带边框点击效果: <com.example.gridview.MyGridView android:id="@+id/grid_upload_pictures" a ...

  4. SringMVC笔记

    SpringMvc主要是三个Servlet:HttpServletBean,FramwworkServlet,DispatcherServlet,它们是依次继承的关系,其处理过程大致功能如下: 1.H ...

  5. element-ui 文件上传

    <el-form-item> <el-upload ref="upload" class="upload-demo" :action=&quo ...

  6. HTML中的标题<h1>标签的用法!

    在HTML中,<h1>标签是命名标题的元素,并且在网页中起到了很重要的作用,但是不要只是因为想要弄成黑粗字体就使用<h1>标题标签这样不好.如果说你还想要做完网站后需要进一步做 ...

  7. HIVE常用命令之MSCK REPAIR TABLE

    MSCK REPAIR TABLE命令主要是用来解决通过hdfs dfs -put或者hdfs api写入hive分区表的数据在hive中无法被查询到的问题.我们知道hive有个服务叫metastor ...

  8. Linux——发行版

    主流发行版 1. Red Hat Linux Red Hat 公司一直是Linux 乃至开源世界的领导者.其有两个不同的发行版本: 一个商用版,称为Red Hat Enterprise Linux,专 ...

  9. unity和lua开发游戏常备技能

    推荐阅读:  我的CSDN  我的博客园  QQ群:704621321  我的个人博客 一.使用制作滑动列表:使用UILayout做虚拟列表 ui.list = base:findcom(" ...

  10. java学习(1) ----getMethod()和getDeclaredMethod()的区别(转)

    转自: https://blog.csdn.net/qq_36443736/article/details/82890011 getMethod():获取自身能用所有的public公共方法.1.类本身 ...