我写了以下代码来解析表达式中的RDD类型和Float。解析由float和RDD组成的算术表达式,例如:“ firstRDD + 2”:
def term2: Parser[List[Either[Float, RDD[(Int,Array[Float])]]]] = rep(factor2)
def factor2: Parser[Either[Float, RDD[(Int,Array[Float])]]] = pathxml | num
def pathxml: Parser[RDD[(Int,Array[Float])]] = pathIdent ^^ {s => pathToRDD(s) } //pathToRDD is a function that gets the path in string and create an RDD from the file inside that path and pathIdent parse to see whether the input string is a path or not
def num: Parser[Float] = floatingPointNumber ^^ (_.toFloat)
现在我收到此错误:
[error] type mismatch;
[error] found : ParseExp.this.Parser[Float]
[error] required: ParseExp.this.Parser[Either[Float,org.apache.spark.rdd.RDD[(Int, Array[Float])]]]
[error] def factor2: Parser[Either[Float, RDD[(Int,Array[Float])]]] = pathxml | num
[error] ^
除了使用“ Either”之外,我不知道该怎么做,也不知道如何解决这种类型的不匹配!请注意,如果我使用“任何”,则无法解析RDD。
它要一个Either
而不是一个Float
,所以给它一个Either
。但是我们不能简单地从输出中创建值,因为解析器使用的是函数,而不是值。
def num: Parser[Either[Float, RDD[(Int,Array[Float])]]] = floatingPointNumber ^^ (n => Left(n).toFloat)
希望它能工作。如果没有,请走很长的路:
def num: Parser[Either[Float, RDD[(Int,Array[Float])]]] = floatingPointNumber ^^ (n =>
val res: Either[Float, RDD[(Int,Array[Float])]] = n.toFloat
res
)
或scalaz路由(您必须重写代码以\/
代替Either
:
import scalaz._
import Scalaz._
def term2: Parser[List[\/[Float, RDD[(Int,Array[Float])]]]] = rep(factor2)
def factor2: Parser[\/[Float, RDD[(Int,Array[Float])]]] = pathxml | num
def pathxml: Parser[RDD[(Int,Array[Float])]] = pathIdent ^^ {s => pathToRDD(s) } //pathToRDD is a function that gets the path in string and create an RDD from the file inside that path and pathIdent parse to see whether the input string is a path or not
def num: Parser[\/[Float, RDD[(Int,Array[Float])]]] = floatingPointNumber ^^ (n => n.left[RDD[(Int,Array[Float])]].toFloat)
left
而right
从scalaz所做的几乎可以达到您的期望-它们创建了一个left或right值。您传递给left或right的type参数用于构造完整类型,因为该值仅提供left或right类型,但是其中一个full类型也需要另一边的类型(right / left),因此其他类型也需要传递。
另一方面,我认为您稍后会在pathxml中收到类似的错误消息。以类似的方式修复它,除了用Right
代替Left
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句