wren romano avatar wren romano committed 4c3b941

Added maybeL and maybeR, as variations on eitherP.

Comments (0)

Files changed (1)

Data/Attoparsec/Combinator.hs

     , skipMany
     , skipMany1
     , eitherP
+    , maybeL
+    , maybeR
 
     -- * Inlined implementations of existing functions
     --
 count n p = sequence (replicate n p)
 {-# INLINE count #-}
 
--- | Combine two alternatives.
+-- | Combine two alternatives. The left parser is tried before the
+-- right parser.
 eitherP :: (Alternative f) => f a -> f b -> f (Either a b)
 eitherP a b = (Left <$> a) <|> (Right <$> b)
 {-# INLINE eitherP #-}
 
+-- | Combine two alternatives, discarding the result of the second.
+-- The left parser is tried before the right parser.
+maybeL :: Alternative f => f a -> f b -> f (Maybe a)
+maybeL p q = (Just <$> p) <|> (q *> pure Nothing)
+{-# INLINE maybeL #-}
+
+-- | Combine two alternatives, discarding the result of the first.
+-- The left parser is tried before the right parser.
+maybeR :: Alternative f => f a -> f b -> f (Maybe b)
+maybeR p q = (p *> pure Nothing) <|> (Just <$> q)
+{-# INLINE maybeR #-}
+
 -- | Zero or more.
 many :: (Alternative f) => f a -> f [a]
 many v = many_v
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.