Applicative parser example in Scala

前端 未结 1 745
自闭症患者
自闭症患者 2021-01-22 04:40

This is a new version of my previous question

We can define a parser as type Parser[A] = String => List[(A, String)]. The parser takes an input string an

相关标签:
1条回答
  • 2021-01-22 05:11

    <*> (terrible name) apparently has this signature:

    <*>[B](f: F[(A) ⇒ B]): F[B]
    

    So let's just chase the types through, thinking about what a parser should do - helped by the fact that List already implements flatMap:

    def <*>[A, B](fa: Parser[A], fab: Parser[(A) => B]) =
      new Parser[B] {
        def apply(s: String) =
          for {
            (a, rem1) ← fa(s)
            (ab, rem2) ← fab(rem1)
          } yield (ab(a), rem2)
      }
    

    That looks like a sensible implementation - parse a first, then parse ab from the remainder, and then we have our result.

    That example is too symbolic for me and I don't know Haskell - if you can find documentation for the <$ and <|> then I'll give it a go.

    0 讨论(0)
提交回复
热议问题