在 Scala 中组合部分函数
Composing partial functions in Scala
我有两个偏函数 f1
和 f2
,我想将它们组合成一个新的偏函数 f
以便 f.isDefinedAt(x)
iff f1.isDefinedAt(x) || f2.isDefinedAt(x)
。我是说
--
| f1(x) iff f1.isDefinedAt(x)
|
f(x) = |
|
| f2(x) iff !f1.isDefinedAt(x) && f2.isDefinedAt(x)
--
有没有办法这样组合 f1
和 f2
?
使用PartialFunction
的orElse
f1 orElse f2
在 PartialFunction
上使用方法 .orElse
:
scala> val f1: PartialFunction[Int, Unit] = { case x if x > 0 => println(s"called f1 with $x") }
f1: PartialFunction[Int,Unit] = <function1>
scala> val f2: PartialFunction[Int, Unit] = { case x if x < 0 => println(s"called f2 with $x") }
f2: PartialFunction[Int,Unit] = <function1>
scala> (f1 orElse f2)(1)
called f1 with 1
scala> (f1 orElse f2)(-1)
called f2 with -1
scala> (f1 orElse f2)(0)
scala.MatchError: 0 (of class java.lang.Integer)
at scala.PartialFunction$$anon.apply(PartialFunction.scala:253)
at scala.PartialFunction$$anon.apply(PartialFunction.scala:251)
at $anonfun.applyOrElse(<console>:11)
at $anonfun.applyOrElse(<console>:11)
at scala.runtime.AbstractPartialFunction$mcVI$sp.apply$mcVI$sp(AbstractPartialFunction.scala:36)
at scala.runtime.AbstractPartialFunction$mcVI$sp.apply(AbstractPartialFunction.scala:36)
at scala.runtime.AbstractPartialFunction$mcVI$sp.apply(AbstractPartialFunction.scala:28)
at $anonfun.applyOrElse(<console>:11)
at $anonfun.applyOrElse(<console>:11)
at scala.PartialFunction$OrElse.apply(PartialFunction.scala:167)
at scala.Function1$class.apply$mcVI$sp(Function1.scala:36)
at scala.PartialFunction$OrElse.apply$mcVI$sp(PartialFunction.scala:164)
... 32 elided
我有两个偏函数 f1
和 f2
,我想将它们组合成一个新的偏函数 f
以便 f.isDefinedAt(x)
iff f1.isDefinedAt(x) || f2.isDefinedAt(x)
。我是说
--
| f1(x) iff f1.isDefinedAt(x)
|
f(x) = |
|
| f2(x) iff !f1.isDefinedAt(x) && f2.isDefinedAt(x)
--
有没有办法这样组合 f1
和 f2
?
使用PartialFunction
的orElse
f1 orElse f2
在 PartialFunction
上使用方法 .orElse
:
scala> val f1: PartialFunction[Int, Unit] = { case x if x > 0 => println(s"called f1 with $x") }
f1: PartialFunction[Int,Unit] = <function1>
scala> val f2: PartialFunction[Int, Unit] = { case x if x < 0 => println(s"called f2 with $x") }
f2: PartialFunction[Int,Unit] = <function1>
scala> (f1 orElse f2)(1)
called f1 with 1
scala> (f1 orElse f2)(-1)
called f2 with -1
scala> (f1 orElse f2)(0)
scala.MatchError: 0 (of class java.lang.Integer)
at scala.PartialFunction$$anon.apply(PartialFunction.scala:253)
at scala.PartialFunction$$anon.apply(PartialFunction.scala:251)
at $anonfun.applyOrElse(<console>:11)
at $anonfun.applyOrElse(<console>:11)
at scala.runtime.AbstractPartialFunction$mcVI$sp.apply$mcVI$sp(AbstractPartialFunction.scala:36)
at scala.runtime.AbstractPartialFunction$mcVI$sp.apply(AbstractPartialFunction.scala:36)
at scala.runtime.AbstractPartialFunction$mcVI$sp.apply(AbstractPartialFunction.scala:28)
at $anonfun.applyOrElse(<console>:11)
at $anonfun.applyOrElse(<console>:11)
at scala.PartialFunction$OrElse.apply(PartialFunction.scala:167)
at scala.Function1$class.apply$mcVI$sp(Function1.scala:36)
at scala.PartialFunction$OrElse.apply$mcVI$sp(PartialFunction.scala:164)
... 32 elided