on master help sign in
Add noErrorUnsafeSplitWith
commitde514774713a4294abe211621836d6a3f3239891 authorAdithya Kumar <adithya@composewell.com> dateTue 9 Feb 2021, 22:53 (6 years ago) parentd42276a
1 file changed+37
src/Streamly/Internal/Data/Parser/ParserD/Types.hs+37
src/Streamly/Internal/Data/Parser/ParserD/Types.hs+37
@@ -126,6 +126,8 @@ module Streamly.Internal.Data.Parser.ParserD.Types
126126 , splitMany -- parseMany?
127127 , alt
128128 , concatMap
129+
130+ , noErrorUnsafeSplitWith
129131 )
130132where
131133
@@ -325,6 +327,41 @@ splitWith func (Parser stepL initialL extractL)
325327 rR <- extractR sR
326328 return $ func rL rR
327329
330+-- Only use this if you know what you're doing
331+{-# INLINE noErrorUnsafeSplitWith #-}
332+noErrorUnsafeSplitWith :: Monad m
333+ => (a -> b -> c) -> Parser m x a -> Parser m x b -> Parser m x c
334+noErrorUnsafeSplitWith func (Parser stepL initialL extractL)
335+ (Parser stepR initialR extractR) =
336+ Parser step initial extract
337+
338+ where
339+
340+ initial = SeqParseL <$> initialL
341+
342+ step (SeqParseL st) a = do
343+ r <- stepL st a
344+ case r of
345+ Partial n s -> return $ Partial n (SeqParseL s)
346+ Continue n s -> return $ Partial n (SeqParseL s)
347+ Done n b -> Partial n <$> (SeqParseR (func b) <$> initialR)
348+ Error err -> return $ Error err
349+
350+ step (SeqParseR f st) a = do
351+ r <- stepR st a
352+ return $ case r of
353+ Partial n s -> Partial n (SeqParseR f s)
354+ Continue n s -> Partial n (SeqParseR f s)
355+ Done n b -> Done n (f b)
356+ Error err -> Error err
357+
358+ extract (SeqParseR f sR) = fmap f (extractR sR)
359+ extract (SeqParseL sL) = do
360+ rL <- extractL sL
361+ sR <- initialR
362+ rR <- extractR sR
363+ return $ func rL rR
364+
328365{-# ANN type SeqAState Fuse #-}
329366data SeqAState sl sr = SeqAL sl | SeqAR sr
330367