当前位置:网站首页>Scala之偏函数Partial Function
Scala之偏函数Partial Function
2022-06-27 06:10:00 【YaoYong_BigData】
一、定义
1.被包在花括号内没有match的一组case语句是一个偏函数
2.偏函数是PartialFunction[A, B]的一个实例
- A代表输入参数类型
- B代表返回结果类型
3.偏函数在scala中是一个特质
二、实例
1.需求
给你一个集合 val list = List(1, 2, 3, "a", true) ,请完成如下要求:
将集合list中的所有数字乘以10,并返回一个新的集合
要求忽略掉非数字的元素,即返回的新的集合形式为 (10,20,30)
2.代码
object _06PartialFunction {
def main(args: Array[String]): Unit = {
val list = List(1, 2, 3, "a", true)
//方式1
list.filter(x => x.isInstanceOf[Int]).map(x => x.asInstanceOf[Int] * 10).foreach(println)
println("-" * 60)
//方式2
val list0 = list.collect{
case x:Int => x * 10
}
list0.foreach(println)
println("------list0------")
//方式3:推荐用法
val pf1:PartialFunction[Any, Int] = {
case x:Int => x * 10
}
//collect方法,类似于map方法,collect方法只能传递偏函数
list.collect(pf1).foreach(println)
println("------pf1------")
//方式4
val pf2 = new PartialFunction[Any,Int] {
//只对返回true的时候交给apply处理
override def isDefinedAt(x: Any): Boolean = x.isInstanceOf[Int]
//处理方法
override def apply(v1: Any): Int = v1.asInstanceOf[Int] * 10
}
list.collect(pf2).foreach(println)
println("------pf2------")
//方式5
val pf3 = new PartialFunction[Any, Int] {
override def isDefinedAt(x: Any): Boolean = x match {
case a:Int => true
case _ => false
}
override def apply(v1: Any): Int = v1 match {
case a:Int => a * 10
}
}
list.collect(pf3).foreach(println)
println("------pf3------")
}
}
上述每种方式都会打印:
10
20
30
三、偏函数的小结
1.使用构建特质的实现类(使用的方式是PartialFunction的匿名子类);
2.PartialFunction 是个特质(看源码);
3.构建偏函数时,参数形式 [Any, Int]是泛型,第一个表示参数类型,第二个表示返回参数;
4.当使用偏函数时,会遍历集合的所有元素,编译器执行流程时先执行isDefinedAt()如果为true ,就会执行 apply, 构建一个新的Int 对象返回;
5.执行isDefinedAt() 为false 就过滤掉这个元素,即不构建新的Int对象;
6.map函数不支持偏函数,因为map底层的机制就是所有循环遍历,无法过滤处理原来集合的元素;collect函数支持偏函数。
边栏推荐
- 函数栈帧的形成与释放
- yaml文件加密
- Thinking technology: how to solve the dilemma in work and life?
- 日期 数据库日期 字符串 之间互相转换
- Us camera cloud service scheme: designed for lightweight video production scenes
- G1和ZGC垃圾收集器
- Inter thread wait and wake-up mechanism, singleton mode, blocking queue, timer
- [cocos creator 3.5.1] addition of coordinates
- 427- binary tree (617. merge binary tree, 700. search in binary search tree, 98. verify binary search tree, 530. minimum absolute difference of binary search tree)
- Small program of C language practice (consolidate and deepen the understanding of knowledge points)
猜你喜欢

汇编语言-王爽 第11章 标志寄存器-笔记

427-二叉树(617.合并二叉树、700.二叉搜索树中的搜索、98. 验证二叉搜索树、530.二叉搜索树的最小绝对差)

Altium Designer 19 器件丝印标号位置批量统一摆放

Crawler learning 5--- anti crawling identification picture verification code (ddddocr and pyteseract measured effect)

matlab GUI界面仿真直流电机和交流电机转速仿真

Run opcua protocol demo on raspberry pie 4B to access kubeedge

427- binary tree (617. merge binary tree, 700. search in binary search tree, 98. verify binary search tree, 530. minimum absolute difference of binary search tree)

多线程基础部分Part2

Distribution gaussienne, régression linéaire, régression logistique

Us camera cloud service scheme: designed for lightweight video production scenes
随机推荐
1317. 将整数转换为两个无零整数的和
Unrecognized VM option ‘‘
Built in functions of spark
Distribution gaussienne, régression linéaire, régression logistique
Go log -uber open source library zap use
Add widget on qlistwidgetitem
分数阶PID控制
多线程基础部分Part 1
Configuring the help class iconfiguration in C # NETCORE
Webrtc Series - Network Transport 7 - ice Supplement nominations and ice Modèle
KubeSphere 集群配置 NFS 存储解决方案-收藏版
JVM garbage collection mechanism
EasyExcel:读取Excel数据到List集合中
[cocos creator 3.5.1] addition of coordinates
我对于测试团队建设的意见
汇编语言-王爽 第11章 标志寄存器-笔记
30 SCM common problems and solutions!
多线程基础部分Part3
C语言练手小项目(巩固加深知识点理解)
693. alternate bit binary number