经过了2次学习,这个地方运用了偏函数的写法
首先看一下akka的源码:
type Receive = akka.actor.Actor.Receive
/**
* 也就代表了我这个地方是重写了一个偏函数,传入的值为Any然后不进行返回
**/
type Receive = scala.PartialFunction[scala.Any, scala.Unit]
偏函数的具体使用如下~~~~
object PartialLearn{
def main(args: Array[String]): Unit = {
/**
* 定义了一个偏函数 pp 传入的参数为 Any
* 输出为 Int
* 重写里面的[[PartialFunction.isDefinedAt()]] 和
* [[PartialFunction.apply()]]
* isDefinedAt 会对进行的数据进行过滤,如果为true则会调用
* apply方法
*/
val pp=new PartialFunction[Any,Int] {
override def isDefinedAt(x: Any): Boolean = {
x.isInstanceOf[Int]
}
override def apply(v1: Any): Int = {
v1.asInstanceOf[Int]+1
}
}
val list=List(1,2,3,13,1,"123")
val f1:PartialFunction[Any,Int]={
case x:Int=> x+1
}
/**
* 使用偏函数后就不可以使用[[list.map()]]
* 而需要使用[[list.collect()]]
*/
print(list.collect(f1))
print(list.collect(pp))
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…