mirror of
https://github.com/composewell/streamly.git
synced 2024-10-26 19:50:19 +03:00
Rename Parser.yield, yieldM to fromPure, fromEffect.
This commit is contained in:
parent
90be38762b
commit
7d9256e283
@ -388,7 +388,7 @@ listDir dir =
|
||||
main :: IO ()
|
||||
main = do
|
||||
hSetBuffering stdout LineBuffering
|
||||
let start = Stream.yield (Left ".")
|
||||
let start = Stream.fromPure (Left ".")
|
||||
Stream.iterateMapLeftsWith Stream.ahead listDir start
|
||||
& Stream.mapM_ print
|
||||
```
|
||||
|
@ -206,7 +206,7 @@ rmapM f (Fold p) = Fold $ ParserD.rmapM f p
|
||||
--
|
||||
{-# INLINE fromPure #-}
|
||||
fromPure :: Monad m => b -> Fold m a b
|
||||
fromPure = Fold . ParserD.yield
|
||||
fromPure = Fold . ParserD.fromPure
|
||||
|
||||
-- | A fold that always yields the result of an effectful action without
|
||||
-- consuming any input.
|
||||
@ -215,7 +215,7 @@ fromPure = Fold . ParserD.yield
|
||||
--
|
||||
{-# INLINE fromEffect #-}
|
||||
fromEffect :: Monad m => m b -> Fold m a b
|
||||
fromEffect = Fold . ParserD.yieldM
|
||||
fromEffect = Fold . ParserD.fromEffect
|
||||
|
||||
-- | Applies two folds sequentially on the input stream and combines their
|
||||
-- results using the supplied function.
|
||||
|
@ -47,7 +47,7 @@ import qualified Streamly.Internal.Data.Parser.ParserK.Type as PRK
|
||||
--
|
||||
{-# INLINE unit #-}
|
||||
unit :: MonadCatch m => Parser m Word8 ()
|
||||
unit = eqWord8 0 *> PR.yield ()
|
||||
unit = eqWord8 0 *> PR.fromPure ()
|
||||
|
||||
{-# INLINE word8ToBool #-}
|
||||
word8ToBool :: Word8 -> Either String Bool
|
||||
|
@ -54,8 +54,8 @@ module Streamly.Internal.Data.Parser
|
||||
-- First order parsers
|
||||
-- * Accumulators
|
||||
, fromFold
|
||||
, yield
|
||||
, yieldM
|
||||
, fromPure
|
||||
, fromEffect
|
||||
, die
|
||||
, dieM
|
||||
|
||||
@ -231,28 +231,28 @@ fromFold = K.toParserK . D.fromFold
|
||||
-- Terminating but not failing folds
|
||||
-------------------------------------------------------------------------------
|
||||
--
|
||||
-- This is the dual of stream "yield".
|
||||
-- This is the dual of stream "fromPure".
|
||||
--
|
||||
-- | A parser that always yields a pure value without consuming any input.
|
||||
--
|
||||
-- /Pre-release/
|
||||
--
|
||||
{-# INLINE [3] yield #-}
|
||||
yield :: MonadCatch m => b -> Parser m a b
|
||||
yield = K.toParserK . D.yield
|
||||
{-# RULES "yield fallback to CPS" [2]
|
||||
forall a. K.toParserK (D.yield a) = K.yield a #-}
|
||||
{-# INLINE [3] fromPure #-}
|
||||
fromPure :: MonadCatch m => b -> Parser m a b
|
||||
fromPure = K.toParserK . D.fromPure
|
||||
{-# RULES "fromPure fallback to CPS" [2]
|
||||
forall a. K.toParserK (D.fromPure a) = K.fromPure a #-}
|
||||
|
||||
-- This is the dual of stream "yieldM".
|
||||
-- This is the dual of stream "fromEffect".
|
||||
--
|
||||
-- | A parser that always yields the result of an effectful action without
|
||||
-- consuming any input.
|
||||
--
|
||||
-- /Pre-release/
|
||||
--
|
||||
{-# INLINE yieldM #-}
|
||||
yieldM :: MonadCatch m => m b -> Parser m a b
|
||||
yieldM = K.yieldM -- K.toParserK . D.yieldM
|
||||
{-# INLINE fromEffect #-}
|
||||
fromEffect :: MonadCatch m => m b -> Parser m a b
|
||||
fromEffect = K.fromEffect -- K.toParserK . D.fromEffect
|
||||
|
||||
-- This is the dual of "nil".
|
||||
--
|
||||
|
@ -21,8 +21,8 @@ module Streamly.Internal.Data.Parser.ParserD
|
||||
-- First order parsers
|
||||
-- * Accumulators
|
||||
, fromFold
|
||||
, yield
|
||||
, yieldM
|
||||
, fromPure
|
||||
, fromEffect
|
||||
, die
|
||||
, dieM
|
||||
|
||||
|
@ -117,8 +117,8 @@ module Streamly.Internal.Data.Parser.ParserD.Type
|
||||
, ParseError (..)
|
||||
, rmapM
|
||||
|
||||
, yield
|
||||
, yieldM
|
||||
, fromPure
|
||||
, fromEffect
|
||||
, serialWith
|
||||
, split_
|
||||
|
||||
@ -339,21 +339,21 @@ rmapM f (Parser step initial extract) = Parser step1 initial1 (extract >=> f)
|
||||
IError err -> return $ IError err
|
||||
step1 s a = step s a >>= mapMStep f
|
||||
|
||||
-- | See 'Streamly.Internal.Data.Parser.yield'.
|
||||
-- | See 'Streamly.Internal.Data.Parser.fromPure'.
|
||||
--
|
||||
-- /Pre-release/
|
||||
--
|
||||
{-# INLINE_NORMAL yield #-}
|
||||
yield :: Monad m => b -> Parser m a b
|
||||
yield b = Parser undefined (pure $ IDone b) undefined
|
||||
{-# INLINE_NORMAL fromPure #-}
|
||||
fromPure :: Monad m => b -> Parser m a b
|
||||
fromPure b = Parser undefined (pure $ IDone b) undefined
|
||||
|
||||
-- | See 'Streamly.Internal.Data.Parser.yieldM'.
|
||||
-- | See 'Streamly.Internal.Data.Parser.fromEffect'.
|
||||
--
|
||||
-- /Pre-release/
|
||||
--
|
||||
{-# INLINE yieldM #-}
|
||||
yieldM :: Monad m => m b -> Parser m a b
|
||||
yieldM b = Parser undefined (IDone <$> b) undefined
|
||||
{-# INLINE fromEffect #-}
|
||||
fromEffect :: Monad m => m b -> Parser m a b
|
||||
fromEffect b = Parser undefined (IDone <$> b) undefined
|
||||
|
||||
-------------------------------------------------------------------------------
|
||||
-- Sequential applicative
|
||||
@ -608,7 +608,7 @@ noErrorUnsafeSplit_ (Parser stepL initialL extractL) (Parser stepR initialR extr
|
||||
-- | 'Applicative' form of 'serialWith'.
|
||||
instance MonadThrow m => Applicative (Parser m a) where
|
||||
{-# INLINE pure #-}
|
||||
pure = yield
|
||||
pure = fromPure
|
||||
|
||||
{-# INLINE (<*>) #-}
|
||||
(<*>) = serialWith id
|
||||
|
@ -21,8 +21,8 @@
|
||||
module Streamly.Internal.Data.Parser.ParserK.Type
|
||||
(
|
||||
Parser (..)
|
||||
, yield
|
||||
, yieldM
|
||||
, fromPure
|
||||
, fromEffect
|
||||
, die
|
||||
|
||||
-- * Conversion
|
||||
@ -279,23 +279,23 @@ instance Functor m => Functor (Parser m a) where
|
||||
-- Sequential applicative
|
||||
-------------------------------------------------------------------------------
|
||||
|
||||
-- This is the dual of stream "yield".
|
||||
-- This is the dual of stream "fromPure".
|
||||
--
|
||||
-- | A parser that always yields a pure value without consuming any input.
|
||||
--
|
||||
-- /Pre-release/
|
||||
--
|
||||
{-# INLINE yield #-}
|
||||
yield :: b -> Parser m a b
|
||||
yield b = MkParser $ \lo st yieldk -> yieldk st (Done lo b)
|
||||
{-# INLINE fromPure #-}
|
||||
fromPure :: b -> Parser m a b
|
||||
fromPure b = MkParser $ \lo st yieldk -> yieldk st (Done lo b)
|
||||
|
||||
-- | See 'Streamly.Internal.Data.Parser.yieldM'.
|
||||
-- | See 'Streamly.Internal.Data.Parser.fromEffect'.
|
||||
--
|
||||
-- /Pre-release/
|
||||
--
|
||||
{-# INLINE yieldM #-}
|
||||
yieldM :: Monad m => m b -> Parser m a b
|
||||
yieldM eff = MkParser $ \lo st yieldk -> eff >>= \b -> yieldk st (Done lo b)
|
||||
{-# INLINE fromEffect #-}
|
||||
fromEffect :: Monad m => m b -> Parser m a b
|
||||
fromEffect eff = MkParser $ \lo st yieldk -> eff >>= \b -> yieldk st (Done lo b)
|
||||
|
||||
-- | 'Applicative' form of 'Streamly.Internal.Data.Parser.serialWith'. Note that
|
||||
-- this operation does not fuse, use 'Streamly.Internal.Data.Parser.serialWith'
|
||||
@ -303,7 +303,7 @@ yieldM eff = MkParser $ \lo st yieldk -> eff >>= \b -> yieldk st (Done lo b)
|
||||
--
|
||||
instance Monad m => Applicative (Parser m a) where
|
||||
{-# INLINE pure #-}
|
||||
pure = yield
|
||||
pure = fromPure
|
||||
|
||||
{-# INLINE (<*>) #-}
|
||||
(<*>) = ap
|
||||
|
@ -774,7 +774,7 @@ readOneEvent :: Config -> Watch -> Parser IO Word8 Event
|
||||
readOneEvent cfg wt@(Watch _ wdMap) = do
|
||||
let headerLen = (sizeOf (undefined :: CInt)) + 12
|
||||
arr <- PR.takeEQ headerLen (A.writeN headerLen)
|
||||
(ewd, eflags, cookie, pathLen) <- PR.yieldM $ A.asPtr arr readHeader
|
||||
(ewd, eflags, cookie, pathLen) <- PR.fromEffect $ A.asPtr arr readHeader
|
||||
-- XXX need the "initial" in parsers to return a step type so that "take 0"
|
||||
-- can return without an input. otherwise if pathLen is 0 we will keep
|
||||
-- waiting to read one more char before we return this event.
|
||||
@ -792,7 +792,7 @@ readOneEvent cfg wt@(Watch _ wdMap) = do
|
||||
when (remaining /= 0) $ PR.takeEQ remaining FL.drain
|
||||
return pth
|
||||
else return $ A.fromList []
|
||||
wdm <- PR.yieldM $ readIORef wdMap
|
||||
wdm <- PR.fromEffect $ readIORef wdMap
|
||||
let (root, sub) =
|
||||
case Map.lookup (fromIntegral ewd) wdm of
|
||||
Just pair -> pair
|
||||
@ -805,7 +805,7 @@ readOneEvent cfg wt@(Watch _ wdMap) = do
|
||||
-- Check for "ISDIR" first because it is less likely
|
||||
isDirCreate = eflags .&. iN_ISDIR /= 0 && eflags .&. iN_CREATE /= 0
|
||||
when (watchRec cfg && isDirCreate)
|
||||
$ PR.yieldM $ addToWatch cfg wt root sub1
|
||||
$ PR.fromEffect $ addToWatch cfg wt root sub1
|
||||
-- XXX Handle IN_DELETE, IN_DELETE_SELF, IN_MOVE_SELF, IN_MOVED_FROM,
|
||||
-- IN_MOVED_TO
|
||||
-- What if a large dir tree gets moved in to our hierarchy? Do we get a
|
||||
|
@ -61,17 +61,17 @@ fromFold =
|
||||
Right is_equal -> is_equal
|
||||
Left _ -> False
|
||||
|
||||
yield :: Property
|
||||
yield =
|
||||
fromPure :: Property
|
||||
fromPure =
|
||||
forAll (chooseInt (min_value, max_value)) $ \x ->
|
||||
case S.parse (P.yield x) (S.fromList [1 :: Int]) of
|
||||
case S.parse (P.fromPure x) (S.fromList [1 :: Int]) of
|
||||
Right r -> r == x
|
||||
Left _ -> False
|
||||
|
||||
yieldM :: Property
|
||||
yieldM =
|
||||
fromEffect :: Property
|
||||
fromEffect =
|
||||
forAll (chooseInt (min_value, max_value)) $ \x ->
|
||||
case S.parse (P.yieldM $ return x) (S.fromList [1 :: Int]) of
|
||||
case S.parse (P.fromEffect $ return x) (S.fromList [1 :: Int]) of
|
||||
Right r -> r == x
|
||||
Left _ -> False
|
||||
|
||||
@ -418,13 +418,13 @@ wordBy =
|
||||
|
||||
-- splitWithFailLeft :: Property
|
||||
-- splitWithFailLeft =
|
||||
-- property (case S.parse (P.serialWith (,) (P.die "die") (P.yield (1 :: Int))) (S.fromList [1 :: Int]) of
|
||||
-- property (case S.parse (P.serialWith (,) (P.die "die") (P.fromPure (1 :: Int))) (S.fromList [1 :: Int]) of
|
||||
-- Right _ -> False
|
||||
-- Left _ -> True)
|
||||
|
||||
-- splitWithFailRight :: Property
|
||||
-- splitWithFailRight =
|
||||
-- property (case S.parse (P.serialWith (,) (P.yield (1 :: Int)) (P.die "die")) (S.fromList [1 :: Int]) of
|
||||
-- property (case S.parse (P.serialWith (,) (P.fromPure (1 :: Int)) (P.die "die")) (S.fromList [1 :: Int]) of
|
||||
-- Right _ -> False
|
||||
-- Left _ -> True)
|
||||
|
||||
@ -447,13 +447,13 @@ wordBy =
|
||||
|
||||
-- teeWithFailLeft :: Property
|
||||
-- teeWithFailLeft =
|
||||
-- property (case S.parse (P.teeWith (,) (P.die "die") (P.yield (1 :: Int))) (S.fromList [1 :: Int]) of
|
||||
-- property (case S.parse (P.teeWith (,) (P.die "die") (P.fromPure (1 :: Int))) (S.fromList [1 :: Int]) of
|
||||
-- Right _ -> False
|
||||
-- Left _ -> True)
|
||||
|
||||
-- teeWithFailRight :: Property
|
||||
-- teeWithFailRight =
|
||||
-- property (case S.parse (P.teeWith (,) (P.yield (1 :: Int)) (P.die "die")) (S.fromList [1 :: Int]) of
|
||||
-- property (case S.parse (P.teeWith (,) (P.fromPure (1 :: Int)) (P.die "die")) (S.fromList [1 :: Int]) of
|
||||
-- Right _ -> False
|
||||
-- Left _ -> True)
|
||||
|
||||
@ -489,13 +489,13 @@ wordBy =
|
||||
|
||||
-- shortestFailLeft :: Property
|
||||
-- shortestFailLeft =
|
||||
-- property (case S.parse (P.shortest (P.die "die") (P.yield (1 :: Int))) (S.fromList [1 :: Int]) of
|
||||
-- property (case S.parse (P.shortest (P.die "die") (P.fromPure (1 :: Int))) (S.fromList [1 :: Int]) of
|
||||
-- Right r -> r == 1
|
||||
-- Left _ -> False)
|
||||
|
||||
-- shortestFailRight :: Property
|
||||
-- shortestFailRight =
|
||||
-- property (case S.parse (P.shortest (P.yield (1 :: Int)) (P.die "die")) (S.fromList [1 :: Int]) of
|
||||
-- property (case S.parse (P.shortest (P.fromPure (1 :: Int)) (P.die "die")) (S.fromList [1 :: Int]) of
|
||||
-- Right r -> r == 1
|
||||
-- Left _ -> False)
|
||||
|
||||
@ -705,8 +705,8 @@ main =
|
||||
|
||||
describe "test for accumulator" $ do
|
||||
prop "P.fromFold FL.sum = FL.sum" fromFold
|
||||
prop "yield value provided" yield
|
||||
prop "yield monadic value provided" yieldM
|
||||
prop "fromPure value provided" fromPure
|
||||
prop "fromPure monadic value provided" fromEffect
|
||||
prop "fail err = Left (SomeException (ParseError err))" parserFail
|
||||
prop "always fail" die
|
||||
prop "always fail but monadic" dieM
|
||||
|
@ -64,17 +64,17 @@ fromFold =
|
||||
Right is_equal -> is_equal
|
||||
Left _ -> False
|
||||
|
||||
yield :: Property
|
||||
yield =
|
||||
fromPure :: Property
|
||||
fromPure =
|
||||
forAll (chooseInt (min_value, max_value)) $ \x ->
|
||||
case S.parseD (P.yield x) (S.fromList [1 :: Int]) of
|
||||
case S.parseD (P.fromPure x) (S.fromList [1 :: Int]) of
|
||||
Right r -> r == x
|
||||
Left _ -> False
|
||||
|
||||
yieldM :: Property
|
||||
yieldM =
|
||||
fromEffect :: Property
|
||||
fromEffect =
|
||||
forAll (chooseInt (min_value, max_value)) $ \x ->
|
||||
case S.parseD (P.yieldM $ return x) (S.fromList [1 :: Int]) of
|
||||
case S.parseD (P.fromEffect $ return x) (S.fromList [1 :: Int]) of
|
||||
Right r -> r == x
|
||||
Left _ -> False
|
||||
|
||||
@ -396,13 +396,13 @@ serialWith =
|
||||
|
||||
splitWithFailLeft :: Property
|
||||
splitWithFailLeft =
|
||||
property (case S.parseD (P.serialWith (,) (P.die "die") (P.yield (1 :: Int))) (S.fromList [1 :: Int]) of
|
||||
property (case S.parseD (P.serialWith (,) (P.die "die") (P.fromPure (1 :: Int))) (S.fromList [1 :: Int]) of
|
||||
Right _ -> False
|
||||
Left _ -> True)
|
||||
|
||||
splitWithFailRight :: Property
|
||||
splitWithFailRight =
|
||||
property (case S.parseD (P.serialWith (,) (P.yield (1 :: Int)) (P.die "die")) (S.fromList [1 :: Int]) of
|
||||
property (case S.parseD (P.serialWith (,) (P.fromPure (1 :: Int)) (P.die "die")) (S.fromList [1 :: Int]) of
|
||||
Right _ -> False
|
||||
Left _ -> True)
|
||||
|
||||
@ -425,13 +425,13 @@ teeWithPass =
|
||||
|
||||
teeWithFailLeft :: Property
|
||||
teeWithFailLeft =
|
||||
property (case S.parseD (P.teeWith (,) (P.die "die") (P.yield (1 :: Int))) (S.fromList [1 :: Int]) of
|
||||
property (case S.parseD (P.teeWith (,) (P.die "die") (P.fromPure (1 :: Int))) (S.fromList [1 :: Int]) of
|
||||
Right _ -> False
|
||||
Left _ -> True)
|
||||
|
||||
teeWithFailRight :: Property
|
||||
teeWithFailRight =
|
||||
property (case S.parseD (P.teeWith (,) (P.yield (1 :: Int)) (P.die "die")) (S.fromList [1 :: Int]) of
|
||||
property (case S.parseD (P.teeWith (,) (P.fromPure (1 :: Int)) (P.die "die")) (S.fromList [1 :: Int]) of
|
||||
Right _ -> False
|
||||
Left _ -> True)
|
||||
|
||||
@ -456,13 +456,13 @@ shortestPass =
|
||||
|
||||
shortestPassLeft :: Property
|
||||
shortestPassLeft =
|
||||
property (case S.parseD (P.shortest (P.die "die") (P.yield (1 :: Int))) (S.fromList [1 :: Int]) of
|
||||
property (case S.parseD (P.shortest (P.die "die") (P.fromPure (1 :: Int))) (S.fromList [1 :: Int]) of
|
||||
Right r -> r == 1
|
||||
Left _ -> False)
|
||||
|
||||
shortestPassRight :: Property
|
||||
shortestPassRight =
|
||||
property (case S.parseD (P.shortest (P.yield (1 :: Int)) (P.die "die")) (S.fromList [1 :: Int]) of
|
||||
property (case S.parseD (P.shortest (P.fromPure (1 :: Int)) (P.die "die")) (S.fromList [1 :: Int]) of
|
||||
Right r -> r == 1
|
||||
Left _ -> False)
|
||||
|
||||
@ -490,13 +490,13 @@ longestPass =
|
||||
|
||||
longestPassLeft :: Property
|
||||
longestPassLeft =
|
||||
property (case S.parseD (P.shortest (P.die "die") (P.yield (1 :: Int))) (S.fromList [1 :: Int]) of
|
||||
property (case S.parseD (P.shortest (P.die "die") (P.fromPure (1 :: Int))) (S.fromList [1 :: Int]) of
|
||||
Right r -> r == 1
|
||||
Left _ -> False)
|
||||
|
||||
longestPassRight :: Property
|
||||
longestPassRight =
|
||||
property (case S.parseD (P.shortest (P.yield (1 :: Int)) (P.die "die")) (S.fromList [1 :: Int]) of
|
||||
property (case S.parseD (P.shortest (P.fromPure (1 :: Int)) (P.die "die")) (S.fromList [1 :: Int]) of
|
||||
Right r -> r == 1
|
||||
Left _ -> False)
|
||||
|
||||
@ -723,8 +723,8 @@ main =
|
||||
|
||||
describe "test for accumulator" $ do
|
||||
prop "P.fromFold FL.sum = FL.sum" fromFold
|
||||
prop "yield value provided" yield
|
||||
prop "yield monadic value provided" yieldM
|
||||
prop "fromPure value provided" fromPure
|
||||
prop "fromPure monadic value provided" fromEffect
|
||||
prop "always fail" die
|
||||
prop "always fail but monadic" dieM
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user