I have a question on refactoring Parsec code to use the Applicative
interface. Suppose I have a parser using the monadic interface like this:
filePath0 :: GenParser Char st Info
filePath0 = do
optional (string "./")
r <- artist
slash
l <- album
slash
t <- track
return $ Song r l t
I'd like to turn it into something like this:
filePath :: GenParser Char st Info
filePath = Song <$> artist <*> album <*> track
But as you can see it is not complete. My question: where, in this refactored version, would I insert optional (string "./")
and slash
parsers?
You can use *>
and <*
to include actions whose result is discarded (but their effect is run):
(*>) :: f a -> f b -> f b
(<*) :: f a -> f b -> f a
giving
filePath :: GenParser Char st Info
filePath = optional "./" *> Song <$> artist <*> slash *> album <*> slash *> track
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With