No case in regex!

This commit is contained in:
Natsu Kagami 2023-12-02 15:39:06 +01:00
parent ad788f4758
commit c9395e793c
Signed by: nki
GPG key ID: 55A032EB38B49ADB

View file

@ -14,12 +14,11 @@ case class Game(id: Int, hands: List[Hand]):
object Parser extends RegexParsers: object Parser extends RegexParsers:
val num = """(0|[1-9]\d*)""".r ^^ { _.toInt } val num = """(0|[1-9]\d*)""".r ^^ { _.toInt }
val update = val update =
num ~ """(red|green|blue)""".r ^^ { case (num ~ color) => num ~ (
color match "red" ^^ (_ => (num: Int) => (h: Hand) => h.copy(red = num)) |
case "red" => (h: Hand) => h.copy(red = num) "green" ^^ (_ => (num: Int) => (h: Hand) => h.copy(green = num)) |
case "green" => (h: Hand) => h.copy(green = num) "blue" ^^ (_ => (num: Int) => (h: Hand) => h.copy(blue = num))
case "blue" => (h: Hand) => h.copy(blue = num) ) ^^ { case (num ~ f) => f(num) }
}
val hand = rep1sep(update, ",").map(_.foldRight(Hand(0, 0, 0))(_(_))) val hand = rep1sep(update, ",").map(_.foldRight(Hand(0, 0, 0))(_(_)))
val game = "Game" ~ num ~ ":" ~ repsep(hand, ";") ^^ { val game = "Game" ~ num ~ ":" ~ repsep(hand, ";") ^^ {