on master help sign in
Add fromFoldMaybe in Parser module
d239074Harendra Kumar
core/src/Streamly/Internal/Data/Parser.hs+13
@@ -57,6 +57,7 @@ module Streamly.Internal.Data.Parser
57 -- First order parsers
57 -- First order parsers
58 -- * Accumulators
58 -- * Accumulators
59 , fromFold
59 , fromFold
60+ , fromFoldMaybe
60 , fromPure
61 , fromPure
61 , fromEffect
62 , fromEffect
62 , die
63 , die
@@ -294,6 +295,16 @@ toFold p = D.toFold $ D.fromParserK p
294fromFold :: MonadCatch m => Fold m a b -> Parser m a b
295fromFold :: MonadCatch m => Fold m a b -> Parser m a b
295fromFold = D.toParserK . D.fromFold
296fromFold = D.toParserK . D.fromFold
296
297
298+-- | Convert a Maybe returning fold to an error returning parser. The first
299+-- argument is the error message that the parser would return when the fold
300+-- returns Nothing.
301+--
302+-- /Pre-release/
303+--
304+{-# INLINE fromFoldMaybe #-}
305+fromFoldMaybe :: MonadCatch m => String -> Fold m a (Maybe b) -> Parser m a b
306+fromFoldMaybe err = D.toParserK . D.fromFoldMaybe err
307+
297-------------------------------------------------------------------------------
308-------------------------------------------------------------------------------
298-- Terminating but not failing folds
309-- Terminating but not failing folds
299-------------------------------------------------------------------------------
310-------------------------------------------------------------------------------
@@ -499,6 +510,8 @@ next = D.toParserK D.next
499-- >>> toEither = Maybe.maybe (Left "maybe: predicate failed") Right
510-- >>> toEither = Maybe.maybe (Left "maybe: predicate failed") Right
500-- >>> maybe f = Parser.either (toEither . f)
511-- >>> maybe f = Parser.either (toEither . f)
501--
512--
513+-- >>> maybe f = Parser.fromFoldMaybe "maybe: predicate failed" (Fold.maybe f)
514+--
502-- /Pre-release/
515-- /Pre-release/
503--
516--
504{-# INLINE maybe #-}
517{-# INLINE maybe #-}