Skip to content

Instantly share code, notes, and snippets.

@SethTisue
Created November 7, 2014 04:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SethTisue/86547a2b8521c3d6a765 to your computer and use it in GitHub Desktop.
Save SethTisue/86547a2b8521c3d6a765 to your computer and use it in GitHub Desktop.
type ParseState = Int
type ParseResult[A] = (A, ParseState)
type Parser[A] = ParseState => ParseResult[A]
def map[A, B](p: Parser[A])(fn: A => B): Parser[B] =
p(_) match {
case (a, n) => (fn(a), n)
}
def flatMap[A, B](p: Parser[A])(fn: A => Parser[B]): Parser[B] =
p(_) match {
case (a, n) => fn(a)(n)
}
def expect[A, B](p: Parser[A], expected: A, p2: Parser[B]): Parser[B] =
flatMap(p){a =>
require(a == expected, a.toString)
p2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment