如何使用for-comprehension而不是嵌套的flatMap调用来编写它?

时间:2014-08-20 09:16:47

标签: scala parsing

我正在尝试将示例从this article翻译为Scala。

所以我定义了一个Parser的monadic类success作为return函数。

class Parser[A](val run: String => List[(A, String)]) {
  def apply(s: String) = run(s)
  def flatMap[B](f: A => Parser[B]): Parser[B] = {
    val runB = {s: String => for((r, rest) <- run(s); rb <- f(r)(rest)) yield rb}
    new Parser(runB)
  }
}

def success[A](a:A):Parser[A] = {
  val run = {s:String => List((a, s))}
  new Parser(run)
}

我还定义了一个新的解析器item来返回输入的第一个字符。

def item(): Parser[Char] = {
  val run = {s: String => if (s.isEmpty) Nil else List((s.head, s.tail))}
  new Parser(run)
}

现在我正在定义一个新的解析器item12: Parser[(Char, Char)]来返回一对第一和第二个字符

 def item12():Parser[(Char, Char)] = 
   item().flatMap(a => (item().flatMap(b => success(a, b))))

我想用 for-comprehension 而不是嵌套的flatMap调用来编写它。我知道我需要为map定义Parser方法。你会怎么做?

1 个答案:

答案 0 :(得分:2)

  

我知道我需要为Parser定义一个map方法。你会怎么做?

def map[B](f: A => B): Parser[B] = {
  val runB = {s: String => ???} // you need to call run and f here
  new Parser(runB)
}

我要离开runB的遗体,以防你想自己做。