Partial Functions(偏函数)

本文介绍了Scala中偏函数的概念及应用。偏函数只对部分输入有效,并可通过isDefinedAt方法检查有效性,避免运行时错误。此外,还可以通过链式调用组合多个偏函数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.Definition

Partial functions are partial in the sense that they aren’t defined for all possible inputs, only those inputs that match at least one of the specified case clauses.

Only case clauses can be specified in a partial function and the entire function must be enclosed in curly braces. In contrast, “regular” function literals can be wrapped in parentheses or curly braces.

If the function is called with an input that doesn’t match one of the case clauses, a MatchError is thrown at runtime.

code example

package com.brown

/**
  * Created by BrownWong on 2016/9/29.
  */
object Hello {
  def main(args: Array[String]): Unit = {
    val pf: PartialFunction[Any, String] = {case s:String => "Yes"}
    println(pf("HAHA"))
    println(pf(12))
  }
}

output

Yes
Exception in thread "main" scala.MatchError: 12 (of class java.lang.Integer)
    at scala.PartialFunction$$anon$1.apply(PartialFunction.scala:253)
	at scala.PartialFunction$$anon$1.apply(PartialFunction.scala:251)
    at com.brown.Hello$$anonfun$1.applyOrElse(Hello.scala:8)
    at scala.runtime.AbstractPartialFunction.apply(AbstractPartialFunction.scala:36)
    at com.brown.Hello$.main(Hello.scala:10)
    at com.brown.Hello.main(Hello.scala)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)

2.isDefinedAt()method

You can test if a Partial Function will match an input using the isDefinedAt method. This function avoids the risk of throwing a MatchError exception.

code example

package com.brown

/**
  * Created by BrownWong on 2016/9/29.
  */
object Hello {
  def main(args: Array[String]): Unit = {
    val pf: PartialFunction[Any, String] = {case s:String => "Yes"}
    println(pf.isDefinedAt("HAHA"))
    println(pf.isDefinedAt(12))
  }
}

output

true
false

3.“chain” Partial Functions

You can “chain” PartialFunctions together: pf1 orElse pf2 orElse pf3 …. If pf1 doesn’t match, then pf2 is tried, then pf3, etc. A MathError is only thrown if none of them matches.

code example

package com.brown

/**
  * Created by BrownWong on 2016/9/29.
  */
object Hello {
  def main(args: Array[String]): Unit = {
    val pf1: PartialFunction[Any, String] = {case s:String => "Yes"}
    val pf2: PartialFunction[Any, String] = {case s:Int => "Yes"}
    val pf3 = pf1 orElse pf2
    println(pf3.isDefinedAt("HAHA"))
    println(pf3.isDefinedAt(12))
  }
}

output

true
true


Ref

《Programming Scala》

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值