如何在Scala的一个解析器中解析两种不同的类型?

鲁比比奇

我写了以下代码来解析表达式中的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)

leftright从scalaz所做的几乎可以达到您的期望-它们创建了一个left或right值。您传递给left或right的type参数用于构造完整类型,因为该值仅提供left或right类型,但是其中一个full类型也需要另一边的类型(right / left),因此其他类型也需要传递。

另一方面,我认为您稍后会在pathxml中收到类似的错误消息。以类似的方式修复它,除了用Right代替Left

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何使用与另一个Parsec解析器具有不同流类型的Parsec解析器?

键入一个根据可选输入返回两种不同类型的函数

如何在两个不同的片段(TabLayout)中设置两种不同的字体

如何在Golang中将具有两种不同数据类型的JSON数组解析为结构

Scala解析器削减最后一个括号

Python-在一个函数中拼合两种不同类型的列表的列表

使用Scala解析器组合器在另一个解析器中重用解析器

解析一个解析器中的所有字段VS每个解析器中的resolve字段

如何使用Aeson解析JSON字符串,它可以是两种不同类型之一

一个人如何为同一实体上的两种不同类型创建一个静态URL?

如何在一个类型中添加多个解析器(Apollo服务器)

我应该为C ++中的每个类型提供一个解析器吗?

将两种不同的返回类型保存在一个变量中

如何在CSS中定位两种可能的类型中的第一种

如何在一个屏幕上收听两种不同的期货?

如何在一个iOS应用中运行两种独立的ARKit体验

我该如何构成一个解析器来解析fastparse中引用的正则表达式?

另一个解析器中的C字符串解析器

如何在一个按钮中实现两种功能。CSS BOOTSTRAP

使用scala packrat解析器解析一个简单的字符串

构建一个简单的解析器,能够使用PyParse解析不同的日期格式

如何在CSS中为一个元素设置两种背景色?

共享合同:如何在一个页面中共享两种不同类型的数据?

您可以使一个函数接受两种不同的数据类型吗?

使用andThe组合解析器以创建另一个不同类型的解析器

如何给btw数组元素一个空格或一个空格,以显示两种不同类型的数组

如何在一个POSIX线程中结合两种不同的等待机制?

如何在 Apollo GraphQL 解析器的一个函数中解析 2 种不同的方法?

一个函数可以有两种不同的返回类型吗?