Commits

Bryan O'Sullivan committed 29c7c2a Merge

Merge pull request #46 from whittle/master

Add the optionMaybe combinator

  • Participants
  • Parent commits 7199799, 349d606

Comments (0)

Files changed (1)

File Data/Attoparsec/Combinator.hs

       choice
     , count
     , option
+    , optionMaybe
     , many'
     , many1
     , many1'
 {-# SPECIALIZE option :: a -> Z.Parser a -> Z.Parser a #-}
 #endif
 
+-- | @option@ specialized to @Maybe@. @optionMaybe p@ tries to apply
+-- action @p@. If @p@ fails without consuming input, it returns
+-- @Nothing@, otherwise @Just@ the value returned by @p@.
+--
+-- > mDecimal :: Integral a => Parser (Maybe a)
+-- > mDecimal = optionMaybe decimal
+optionMaybe :: Alternative f => f a -> f (Maybe a)
+optionMaybe = option Nothing . fmap Just
+{-# SPECIALIZE optionMaybe :: Parser ByteString a -> Parser ByteString (Maybe a) #-}
+{-# SPECIALIZE optionMaybe :: Parser Text a -> Parser Text (Maybe a) #-}
+{-# SPECIALIZE optionMaybe :: Z.Parser a -> Z.Parser (Maybe a) #-}
+
 -- | A version of 'liftM2' that is strict in the result of its first
 -- action.
 liftM2' :: (Monad m) => (a -> b -> c) -> m a -> m b -> m c