on master help sign in
Add fromFoldMaybe in Parser module
commitd23907410aa4294e0a67028b49c8123182e50def authorHarendra Kumar <harendra@composewell.com> dateSun 29 May 2022, 16:19 (4 years ago) parenteb43f56
1 file changed+13
core/src/Streamly/Internal/Data/Parser.hs+13
core/src/Streamly/Internal/Data/Parser.hs+13
@@ -57,6 +57,7 @@ module Streamly.Internal.Data.Parser
5757 -- First order parsers
5858 -- * Accumulators
5959 , fromFold
60+ , fromFoldMaybe
6061 , fromPure
6162 , fromEffect
6263 , die
@@ -294,6 +295,16 @@ toFold p = D.toFold $ D.fromParserK p
294295fromFold :: MonadCatch m => Fold m a b -> Parser m a b
295296fromFold = D.toParserK . D.fromFold
296297
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+
297308-------------------------------------------------------------------------------
298309-- Terminating but not failing folds
299310-------------------------------------------------------------------------------
@@ -499,6 +510,8 @@ next = D.toParserK D.next
499510-- >>> toEither = Maybe.maybe (Left "maybe: predicate failed") Right
500511-- >>> maybe f = Parser.either (toEither . f)
501512--
513+-- >>> maybe f = Parser.fromFoldMaybe "maybe: predicate failed" (Fold.maybe f)
514+--
502515-- /Pre-release/
503516--
504517{-# INLINE maybe #-}