gpt4 book ai didi

scala - PartialFunction的提升方法的逆函数

转载 作者:行者123 更新时间:2023-12-03 11:33:12 25 4
gpt4 key购买 nike

PartialFunctionlift方法将PartialFunction转换为Function,并返回Option结果。

是否有相反的操作,将Function1[A, Option[B]]转换为PartialFunction[A, B]

最佳答案

不在库中,但是很容易构建。但是,isDefinedAt将必须全面评估该功能,使其比通过模式匹配构建的部分功能的典型功能更为昂贵,并且还可能导致不良的副作用。

scala> def unlift[A, B](f : (A => Option[B])) = new PartialFunction[A,B] {
| def isDefinedAt(x : A) = f(x).isDefined
| def apply(x : A) = f(x).get
| }
unlift: [A,B](f: (A) => Option[B])java.lang.Object with PartialFunction[A,B]
scala> def f(x : Int) = if (x == 1) Some(1) else None
f: (x: Int)Option[Int]
scala> val g = unlift(f)
g: java.lang.Object with PartialFunction[Int,Int] = <function1>
scala> g.isDefinedAt(1)
res0: Boolean = true
scala> g.isDefinedAt(2)
res1: Boolean = false
scala> g(1)
res2: Int = 1
scala> g(2)
java.util.NoSuchElementException: None.get
at scala.None$.get(Option.scala:262)
at scala.None$.get(Option.scala:260)
at $anon$1.apply(<console>:7)
at scala.Function1$class.apply$mcII$sp(Function1.scala:39)
at $anon$1.apply$mcII$sp(<console>:5)
at .<init>(<console>:9)
at .<clinit>(<console>)
at RequestResult$.<init>(<console>:9)
at RequestResult$.<clinit>(<console>)
at RequestResult$scala_repl_result(<console>)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at scala.tools.nsc.Interpreter$Request$$anonfun$loadAndRun$1$$anonfun$apply$17.apply(Interpreter.scala:988)
at scala.tools....

纯粹主义者也可以用try / catch块包装isDefinedAt,以在异常时返回false。

关于scala - PartialFunction的提升方法的逆函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5902266/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com