@@ -62,6 +62,11 @@ module Streamly.Internal.Data.Parser
6262 , die
6363 , dieM
6464
65+ -- * Map on input
66+ , lmap
67+ , lmapM
68+ , filter
69+
6570 -- * Element parsers
6671 , peek
6772 , eof
@@ -202,8 +207,8 @@ module Streamly.Internal.Data.Parser
202207where
203208
204209import Control.Monad.Catch (MonadCatch, MonadThrow)
205−import Prelude
206− hiding (any, all, take, takeWhile, sequence, concatMap, maybe, either)
210+import Prelude hiding
211+ (any, all, take, takeWhile, sequence, concatMap, maybe, either, filter)
207212
208213import Streamly.Internal.Data.Fold.Type (Fold(..))
209214import Streamly.Internal.Data.Parser.ParserK.Type (Parser)
@@ -302,6 +307,39 @@ die = D.toParserK . D.die
302307dieM :: MonadCatch m => m String -> Parser m a b
303308dieM = D.toParserK . D.dieM
304309
310+-------------------------------------------------------------------------------
311+-- Map on input
312+-------------------------------------------------------------------------------
313+
314+-- | @lmap f parser@ maps the function @f@ on the input of the parser.
315+--
316+-- >>> Stream.parse (Parser.lmap (\x -> x * x) (Parser.fromFold Fold.sum)) (Stream.enumerateFromTo 1 100)
317+-- 338350
318+--
319+-- > lmap = Parser.lmapM return
320+--
321+-- /Internal/
322+{-# INLINE lmap #-}
323+lmap :: MonadCatch m => (a -> b) -> Parser m b r -> Parser m a r
324+lmap f p = D.toParserK $ D.lmap f $ D.fromParserK p
325+
326+-- | @lmapM f parser@ maps the monadic function @f@ on the input of the parser.
327+--
328+-- /Internal/
329+{-# INLINE lmapM #-}
330+lmapM :: MonadCatch m => (a -> m b) -> Parser m b r -> Parser m a r
331+lmapM f p = D.toParserK $ D.lmapM f $ D.fromParserK p
332+
333+-- | Include only those elements that pass a predicate.
334+--
335+-- >>> Stream.parse (Parser.filter (> 5) (Parser.fromFold Fold.sum)) $ Stream.fromList [1..10]
336+-- 40
337+--
338+-- /Internal/
339+{-# INLINE filter #-}
340+filter :: MonadCatch m => (a -> Bool) -> Parser m a b -> Parser m a b
341+filter f p = D.toParserK $ D.filter f $ D.fromParserK p
342+
305343-------------------------------------------------------------------------------
306344-- Failing Parsers
307345-------------------------------------------------------------------------------