From f75904ca93c1cef4cffe10989e8aeb91fb19b89f Mon Sep 17 00:00:00 2001 From: Brandon Dyck Date: Mon, 9 Sep 2024 10:33:46 -0600 Subject: [PATCH] Added Map combinator --- gigaparsec.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/gigaparsec.go b/gigaparsec.go index 19e20de..2f734fb 100644 --- a/gigaparsec.go +++ b/gigaparsec.go @@ -191,3 +191,18 @@ func Try[In, Out any](p Parser[In, Out]) Parser[In, Out] { return consumed, reply, err } } + +func Map[In, Out1, Out2 any](p Parser[In, Out1], f func(Out1) Out2) Parser[In, Out2] { + return func(input State[In]) (bool, Result[In, Out2], error) { + consumed, reply, err := p(input) + if err != nil { + return consumed, Result[In, Out2]{}, err + } + reply2 := Result[In, Out2]{ + Value: f(reply.Value), + State: reply.State, + Message: reply.Message, + } + return consumed, reply2, nil + } +}