Skip to content

Instantly share code, notes, and snippets.

@j5ik2o
Last active March 24, 2023 00:04
Show Gist options
  • Save j5ik2o/505d8bda022678f1c6bf0a64e09aea40 to your computer and use it in GitHub Desktop.
Save j5ik2o/505d8bda022678f1c6bf0a64e09aea40 to your computer and use it in GitHub Desktop.
// https://github.com/fpinscala/fpinscala/.../parsing/Parsers.scala の一部
extension [A](p: Parser[A])
// 中略
def opt: Parser[Option[A]] = p.map(Some(_)) | succeed(None)
def map[B](f: A => B): Parser[B] = p.flatMap(f andThen succeed)
def |(p2: => Parser[A]): Parser[A] = p.or(p2)
infix def or(p2: => Parser[A]): Parser[A]
def flatMap[B](f: A => Parser[B]): Parser[B]
// 以下略
// 使うとき
val name: Either[ParseError, String] = name.opt.run(input)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment