当前位置:网站首页>Partial function of Scala
Partial function of Scala
2022-06-27 06:32:00 【YaoYong_ BigData】
One 、 Definition
1. The quilt is wrapped in curly brackets match A group of case Statement is a partial function
2. The partial function is PartialFunction[A, B] An example of
- A Represents the type of input parameter
- B Represents the return result type
3. Partial functions are in scala Middle is a trait
Two 、 example
1. demand
Give you a collection val list = List(1, 2, 3, "a", true) , Please complete the following requirements :
Set list Multiply all numbers in by 10, And return to a new set
It is required to ignore non numeric elements , That is, the new set form returned is (10,20,30)
2. Code
object _06PartialFunction {
def main(args: Array[String]): Unit = {
val list = List(1, 2, 3, "a", true)
// The way 1
list.filter(x => x.isInstanceOf[Int]).map(x => x.asInstanceOf[Int] * 10).foreach(println)
println("-" * 60)
// The way 2
val list0 = list.collect{
case x:Int => x * 10
}
list0.foreach(println)
println("------list0------")
// The way 3: Recommended use
val pf1:PartialFunction[Any, Int] = {
case x:Int => x * 10
}
//collect Method , Be similar to map Method ,collect Methods can only pass partial functions
list.collect(pf1).foreach(println)
println("------pf1------")
// The way 4
val pf2 = new PartialFunction[Any,Int] {
// Return only true Give it to apply Handle
override def isDefinedAt(x: Any): Boolean = x.isInstanceOf[Int]
// processing method
override def apply(v1: Any): Int = v1.asInstanceOf[Int] * 10
}
list.collect(pf2).foreach(println)
println("------pf2------")
// The way 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------")
}
}
Each of the above methods will print :
10
20
30
3、 ... and 、 Summary of partial functions
1. Use implementation classes that build idiosyncrasies ( The way to use it is PartialFunction Anonymous subclass of );
2.PartialFunction It's a trait ( Look at the source code );
3. When constructing partial functions , Parameter form [Any, Int] Is a generic , The first represents the parameter type , The second represents the return parameter ;
4. When using partial functions , Will traverse all elements of the collection , The compiler executes the process first isDefinedAt() If true , Will execute apply, Building a new Int Object returns ;
5. perform isDefinedAt() by false Just filter out this element , That is, no new Int object ;
6.map Function does not support partial functions , because map The underlying mechanism is all loop traversal , Cannot filter elements of the original collection ;collect Functions support partial functions .
边栏推荐
- Scala之偏函数Partial Function
- 426 binary tree (513. find the value in the lower left corner of the tree, 112. sum of paths, 106. construct a binary tree from the middle order and post order traversal sequence, 654. maximum binary
- Redis 缓存穿透、缓存击穿、缓存雪崩
- Us camera cloud service scheme: designed for lightweight video production scenes
- [QT notes] simple understanding of QT meta object system
- Convolution neural network -- Application of CNN model (ore prospecting prediction)
- 693. alternate bit binary number
- 2018年数学建模竞赛-高温作业专用服装设计
- [QT notes] basic use of qregularexpression in QT
- TiDB 中的视图功能
猜你喜欢

0.0.0.0:x的含义

JVM类加载机制

Thinking technology: how to solve the dilemma in work and life?

建模竞赛-光传送网建模与价值评估

The restart status of the openstack instance will change to the error handling method. The openstack built by the container restarts the compute service method of the computing node and prompts the gi

JVM overall structure analysis

快速实现单片机和手机蓝牙通信

路由器和交换机的区别
Software testing year end summary report template

Redis 缓存穿透、缓存击穿、缓存雪崩
随机推荐
JVM常用指令
Thinking technology: how to solve the dilemma in work and life?
汇编语言-王爽 第13章 int指令-笔记
下载cuda和cudnn
使用CSDN 开发云搭建导航网站
快速实现Thread Mesh组网详解
426-二叉树(513.找树左下角的值、112. 路径总和、106.从中序与后序遍历序列构造二叉树、654. 最大二叉树)
Program ape learning Tiktok short video production
310. minimum height tree
Fast realization of Bluetooth communication between MCU and mobile phone
Crawler learning 5--- anti crawling identification picture verification code (ddddocr and pyteseract measured effect)
分数阶PID控制
Scala advanced_ Member access modifier
快速实现蓝牙iBeacn功能详解
Active learning
汇编语言-王爽 第9章 转移指令的原理-笔记
Software testing year end summary report template
汇编语言-王爽 第11章 标志寄存器-笔记
426 binary tree (513. find the value in the lower left corner of the tree, 112. sum of paths, 106. construct a binary tree from the middle order and post order traversal sequence, 654. maximum binary
Altium designer 19 device silk screen label position shall be placed uniformly in batches