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 :

  1. println("\nStep 1: How to define a Partial Function named isVeryTasty")
  2. val isVeryTasty: PartialFunction[String, String] = { case "Glazed Donut" | "Strawberry Donut" => "Very Tasty"}
  3.  
  4. println("\nStep 2: How to call the Partial Function named isVeryTasty")
  5. println(s"Calling partial function isVeryTasty = ${isVeryTasty("Glazed Donut")}")
  6. // NOTE: you will get scala.MatchError
  7.  
  8. println("\nStep 3: How to define PartialFunction named isTasty and unknownTaste")
  9. val isTasty: PartialFunction[String, String] = {
  10. case "Plain Donut" => "Tasty"
  11. }
  12.  
  13. val unknownTaste: PartialFunction[String, String] = {
  14. case donut @ _ => s"Unknown taste for donut = $donut"
  15. }
  16.  
  17. println("\nStep 4: How to compose PartialFunction using orElse")
  18. val donutTaste = isVeryTasty orElse isTasty orElse unknownTaste
  19. println(donutTaste("Glazed Donut"))
  20. println(donutTaste("Plain Donut"))
  21. println(donutTaste("Chocolate Donut"))

result:

  1. Step : How to define a Partial Function named isVeryTasty
  2.  
  3. Step : How to call the Partial Function named isVeryTasty
  4. Calling partial function isVeryTasty = Very Tasty
  5.  
  6. Step : How to define PartialFunction named isTasty and unknownTaste
  7.  
  8. Step : How to compose PartialFunction using orElse
  9. Very Tasty
  10. Tasty
  11. 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. Python进阶:对象复制与比较,分深浅,见真假

    "==" 与 is python 为 10 开辟内存空间, a与b同时指向这块内存,即a与b的值相等,a与b的id也相等.因此 a==b 与 a is b 都返回True: a = ...

  2. sass快速使用

    sass的使用 建议使用一种语法格式(scss) scss sass转换 sass-convert main.scss main.sass sass变量声明 example: $headline-ff ...

  3. Vue组件全局/局部注册

    全局注册 main.js中创建 Vue.component('button-counter', { data: function () { return { count: 0 } }, templat ...

  4. ionic开发遇到的问题总结

    前言 ionic是一个用来开发混合手机应用的,开源的,免费的代码库.可以优化html.css和js的性能,构建高效的应用程序,而且还可以用于构建Sass和AngularJS的优化.ionic会是一个可 ...

  5. 一般处理程序里使用session对象

    在一般处理程序里使用session,必须继承  IRequiresSessionState  接口.

  6. Topics in CS(difference between compile and interpret)

    编译 Compile:把整个程序源代码翻译成另外一种代码,然后等待被执行,发生在运行之前,产物是「另一份代码」. 解释 Interpret:把程序源代码一行一行的读懂然后执行,发生在运行时,产物是「运 ...

  7. K2 BPM_采购端到端解决方案,激活合规采购新动能_十年专注业务流程管理系统

    「方案背景」企业管理标准化演进之路 企业的成长离不开标准化,企业的可持续发展更离不开标准化.随着市场竞争的日趋激烈,标准化已经成为企业参与市场竞争的战略性手段,也成为企业可持续发展的重要手段.聚焦到采 ...

  8. oracle:sql添加合计语句(union)

    转自:https://blog.csdn.net/qq_43563538/article/details/90370925 原表见下图: 查询全部的语句: SELECT * from BD_MARK_ ...

  9. dstat 监控工具

    dstat 监控工具 Linux 自带的监控工具.界面相当友好,可以替代其他的监控工具. 安装 yum install -y dstat 命令使用 dstat -h # 帮助 dstat -tpcdr ...

  10. Android笔记(十二)AndroidManiFest.xml

    AndroidManiFest.xml清单文件是每个Android项目所必须的,它是整个Android应用的全局描述文件.AndroidManiFest.xml清单文件说明了该应用的名称.所使用的图标 ...