反向PartialFunction的提升方法

时间:2011-05-05 18:09:33

标签: scala

PartialFunction的{​​{1}}方法将PartialFunction转换为lift,返回Function结果。

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

6 个答案:

答案 0 :(得分:39)

很难从一系列scala杰出人物中获得所有这些优秀答案,但如果您想了解标准库中的那个,那么它就在scala.Function伴侣对象中。 (在2.9。)

/** Turns a function `A => Option[B]` into a `PartialFunction[A, B]`.  Important note:
 *  this transformation implies the original function will be called 2 or more
 *  times on each logical invocation, because the only way to supply an implementation
 *  of isDefinedAt is to call the function and examine the return value.
 *
 *  @param   f    a function T => Option[R]
 *  @return       a partial function defined for those inputs where
 *                f returns Some(_) and undefined where f returns None.
 *  @see PartialFunction#lift
 */
def unlift[T, R](f: T => Option[R]): PartialFunction[T, R] = new PartialFunction[T, R] {
  def apply(x: T): R = f(x).get
  def isDefinedAt(x: T): Boolean = f(x).isDefined
  override def lift: T => Option[R] = f
}

答案 1 :(得分:6)

不在库中,但很容易构建。但是,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。

答案 2 :(得分:6)

以更复杂的例子为基础建立James的答案,我在我的库中有以下代码 - Scala-library-forgot(或者不信任你):

class DroppedFunction[-A,+B](f: A => Option[B]) extends PartialFunction[A,B] {
  private[this] var tested = false
  private[this] var arg: A = _
  private[this] var ans: Option[B] = None
  private[this] def cache(a: A) {
    if (!tested || a != arg) {
      tested = true
      arg = a
      ans = f(a)
    }
  }        
  def isDefinedAt(a: A) = {
    cache(a)
    ans.isDefined
  }
  def apply(a: A) = {
    cache(a)
    ans.get
  }
}
class DroppableFunction[A,B](f: A => Option[B]) {
  def drop = new DroppedFunction(f)
}
implicit def function_is_droppable[A,B](f: A => Option[B]) = new DroppableFunction(f)

大多数代码专门用于确保缓存函数评估(只要apply恰好在isDefinedAt之后)。使用示例:

scala> val f = (x: Int) => if (x>=0) Some(x) else None
f: (Int) => Option[Int] = <function1>

scala> Array(-2,-1,0,1,2).collect(f.drop)
res0: Array[Int] = Array(0, 1, 2)

缓存有助于加快速度并避免出现双面效应问题(至少在isDefinedAt之前使用apply时,以及函数在返回时忽略副作用{{1} })。

答案 3 :(得分:1)

以詹姆斯的答案为基础......

也可以缓存isDefinedAt中生成的结果,然后在调用apply时返回此结果,从而避免重复执行。

然而,Scala并没有强制执行纯函数,所以很容易找到现实生活中的例子,其中任何一个提升策略都会产生令人惊讶和意想不到的结果。因此,将某些内容“解除”为PartialFunction

通常是不明智的

答案 4 :(得分:1)

您可以使用Function.unliftSee docs

答案 5 :(得分:0)

我们应该始终使用部分函数文字来构建PartialFunction,因为对applyOrElse正确实现PartialFunction来说太简单了。

因此,正确的unlift应该这样实现:

// Use AnyVal to avoid the indirect reference to f
class Extractor[A, B](val f: A => Option[B]) extends AnyVal {
  def unapply(a: A) = f(a)
}

def unlift[A, B](f: A => Option[B]): PartialFunction[A, B] = {
  val LocalExtractor = new Extractor(f)

  // Create the PartialFunction from a partial function literal
  { case LocalExtractor(b) => b }
}