on master help sign in
Factor out "pure" as "wrap"
31e269aHarendra Kumar
src/Streamly/Internal/Data/Parser/Types.hs+12−3
@@ -115,20 +115,29 @@ instance Functor m => Functor (Parser m a) where
115
115
116 fmap3 g = fmap (fmap (fmap g))
116 fmap3 g = fmap (fmap (fmap g))
117
117
118+{-# INLINE wrap #-}
119+wrap :: Monad m => b -> Parser m a b
120+wrap b = Parser (\_ _ -> pure $ Stop 0 ()) -- step
121+ (pure ()) -- initial
122+ (\_ -> pure $ Right b) -- extract
123+
118{-# ANN type SeqParseState Fuse #-}
124{-# ANN type SeqParseState Fuse #-}
119data SeqParseState sl f sr =
125data SeqParseState sl f sr =
120 SeqParseL sl | SeqParseR f sr | SeqParseLErr String
126 SeqParseL sl | SeqParseR f sr | SeqParseLErr String
121
127
128+-- XXX implement interleaved variant, parse using the first parser and then
129+-- using the second parser alternately.
130+--
122-- | Apply two parsing folds sequentially to an input. The input is provided to
131-- | Apply two parsing folds sequentially to an input. The input is provided to
123-- the first parser, if the parser fails then whole composition fails. If the
132-- the first parser, if the parser fails then whole composition fails. If the
124-- parser succeeds, the remaining input is supplied to the second parser. If
133-- parser succeeds, the remaining input is supplied to the second parser. If
125-- the second parser succeeds the composed parser succeeds otherwise it fails.
134-- the second parser succeeds the composed parser succeeds otherwise it fails.
126--
135--
136+-- This is the opposite of the "append" operation on streams.
137+--
127instance Monad m => Applicative (Parser m a) where
138instance Monad m => Applicative (Parser m a) where
128 {-# INLINE pure #-}
139 {-# INLINE pure #-}
129− pure b = Parser (\_ _ -> return $ Stop 0 ())
140+ pure = wrap
130− (return ())
131− (\_ -> return $ Right b)
132
141
133 {-# INLINE (<*>) #-}
142 {-# INLINE (<*>) #-}
134 (Parser stepL initialL extractL) <*> (Parser stepR initialR extractR) =
143 (Parser stepL initialL extractL) <*> (Parser stepR initialR extractR) =