Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Applicative notation for parsers whose result is discarded

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?

like image 308
Sridhar Ratnakumar Avatar asked Mar 10 '23 19:03

Sridhar Ratnakumar


1 Answers

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
like image 101
Cactus Avatar answered Apr 07 '23 09:04

Cactus