gpt4 book ai didi

scala - 使用未应用功能丰富 PartialFunction

转载 作者:行者123 更新时间:2023-12-04 02:17:02 24 4
gpt4 key购买 nike

PartialFunction 是一个天然的提取器,它的 lift 方法提供了精确的提取器功能。所以使用部分函数作为提取器会非常方便。这将允许以比可用于 PartialFunction

的普通 orElse 更复杂的方式组合模式匹配表达式

所以我尝试使用 pimp my library 方法但失败了


更新:正如@Archeg 所示,还有另一种有效的转换方法。所以我将它包含在提供的代码中。

我也尝试了一些更复杂的解决方案,但都失败了


object Test {
class UnapplyPartial[-R, +T](val fun : PartialFunction[R,T]) {
def unapply(source : R) : Option[T] = fun.lift(source)
}
implicit def toUnapply[R,T](fun : PartialFunction[R,T]) : UnapplyPartial[R,T] = new UnapplyPartial(fun)

class PartialFunOps[-R, +T](val fun : PartialFunction[R,T]) {
def u : UnapplyPartial[R, T] = new UnapplyPartial(fun)
}
implicit def toPartFunOps[R,T](fun : PartialFunction[R,T]) : PartialFunOps[R,T] = new PartialFunOps(fun)


val f : PartialFunction[String, Int] = {
case "bingo" => 0
}
val u = toUnapply(f)

def g(compare : String) : PartialFunction[String, Int] = {
case `compare` => 0
}

// error while trying to use implicit conversion
def testF(x : String) : Unit = x match {
case f(i) => println(i)
case _ => println("nothing")
}

// external explicit conversion is Ok
def testU(x : String) : Unit = x match {
case u(i) => println(i)
case _ => println("nothing")
}

// embedded explicit conversion fails
def testA(x : String) : Unit = x match {
case toUnapply(f)(i) => println(i)
case _ => println("nothing")
}

// implicit explicit conversion is Ok
def testI(x : String) : Unit = x match {
case f.u(i) => println(i)
case _ => println("nothing")
}

// nested case sentences fails
def testInplace(x : String) : Unit = x match {
case { case "bingo" => 0 }.u(i) => println(i)
case _ => println("nothing")
}

// build on the fly fails
def testGen(x : String) : Unit = x match {
case g("bingo").u(i) => println(i)
case _ => println("nothing")
}

// implicit conversion without case is also Ok
def testFA(x : String) : Option[Int] =
f.unapply(x)
}

我收到以下错误消息:

UnapplyImplicitly.scala:16: error: value f is not a case class, nor does it have an unapply/unapplySeq member case f(i) => println(i)

UnapplyImplicitly.scala:28: error: '=>' expected but '(' found. case toUnapply(f)(i) => println(i)

这个错误可以通过假设的形式避免,如 TestI 所示。但我很好奇是否有可能避免 testInplace 错误:

UnapplyImplicitly.scala:46: error: illegal start of simple pattern case { case "bingo" => 0 }.u(i) => println(i) ^

UnapplyImplicitly.scala:47: error: '=>' expected but ';' found. case _ => println("nothing")

UnapplyImplicitly.scala:56: error: '=>' expected but '.' found. case g("bingo").u(i) => println(i) ^

最佳答案

我不确定你最终想要达到什么目的,但据我所知,提取器应该始终是对象,你无法通过类来获取它。它实际上在文档中称为 Extractor Object。考虑一下:

class Wrapper[R, T](fun: PartialFunction[R, T]) {
object PartialExtractor {
def unapply(p: R): Option[T] = fun.lift(p)
}
}

implicit def toWrapper[R,T](fun : PartialFunction[R,T]) : Wrapper[R, T] = new Wrapper(fun)


val f : PartialFunction[String, Int] = {
case "bingo" => 0
}

def testFF(x : String) : Unit = x match {
case f.PartialExtractor(i) => println(i)
case _ => println("nothing")
}

更新

我能想到的最好的:

def testInplace(x : String) : Unit ={
val ff = { case "bingo" => 0 } : PartialFunction[String, Int]
x match {
case ff.PartialExtractor(Test(i)) => println(i)
case "sd" => println("nothing") }
}

关于scala - 使用未应用功能丰富 PartialFunction,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33182235/

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