on master help sign in
Use explicit state types instead of Maybe

So that we can easily identify the constructors in the core.

commitbf152c89bc8b31e9aad62af95d4fe3a649f3b9f8 authorHarendra Kumar <harendra@composewell.com> dateMon 30 Nov 2020, 15:11 (6 years ago) parent6d16a0c
1 file changed+19−14
src/Streamly/Internal/Data/Stream/StreamD.hs+19−14
src/Streamly/Internal/Data/Stream/StreamD.hs+19−14
@@ -446,19 +446,21 @@ unfoldrM next state = Stream step state
446446unfoldr :: Monad m => (s -> Maybe (a, s)) -> s -> Stream m a
447447unfoldr f = unfoldrM (return . f)
448448
449+data UnfoldState s = UnfoldNothing | UnfoldJust s
450+
449451-- | Convert an 'Unfold' into a 'Stream' by supplying it a seed.
450452--
451453{-# INLINE_NORMAL unfold #-}
452454unfold :: Monad m => Unfold m a b -> a -> Stream m b
453−unfold (Unfold ustep inject) seed = Stream step Nothing
455+unfold (Unfold ustep inject) seed = Stream step UnfoldNothing
454456 where
455457 {-# INLINE_LATE step #-}
456− step _ Nothing = inject seed >>= return . Skip . Just
457− step _ (Just st) = do
458+ step _ UnfoldNothing = inject seed >>= return . Skip . UnfoldJust
459+ step _ (UnfoldJust st) = do
458460 r <- ustep st
459461 return $ case r of
460− Yield x s -> Yield x (Just s)
461− Skip s -> Skip (Just s)
462+ Yield x s -> Yield x (UnfoldJust s)
463+ Skip s -> Skip (UnfoldJust s)
462464 Stop -> Stop
463465
464466------------------------------------------------------------------------------
@@ -1657,27 +1659,30 @@ splitSuffixBy :: Monad m
16571659 => (a -> Bool) -> Fold m a b -> Stream m a -> Stream m b
16581660splitSuffixBy predicate f = foldMany1 (FL.sliceSepBy predicate f)
16591661
1662+data WordsByState s = WordsByJust s | WordsByNothing
1663+
16601664{-# INLINE_NORMAL wordsBy #-}
16611665wordsBy :: Monad m => (a -> Bool) -> Fold m a b -> Stream m a -> Stream m b
1662−wordsBy predicate f (Stream step state) = Stream (stepOuter f) (Just state)
1666+wordsBy predicate f (Stream step state) =
1667+ Stream (stepOuter f) (WordsByJust state)
16631668
16641669 where
16651670
16661671 {-# INLINE_LATE stepOuter #-}
1667− stepOuter (Fold fstep initial done) gst (Just st) = do
1672+ stepOuter (Fold fstep initial done) gst (WordsByJust st) = do
16681673 res <- step (adaptState gst) st
16691674 case res of
16701675 Yield x s -> do
16711676 if predicate x
1672− then return $ Skip (Just s)
1677+ then return $ Skip (WordsByJust s)
16731678 else do
16741679 fs <- initial
16751680 sfs <- fstep fs x
16761681 case sfs of
16771682 FL.Partial fs1 -> go SPEC s fs1
1678− FL.Done fb -> return $ Yield fb (Just s)
1683+ FL.Done fb -> return $ Yield fb (WordsByJust s)
16791684
1680− Skip s -> return $ Skip $ Just s
1685+ Skip s -> return $ Skip $ WordsByJust s
16811686 Stop -> return Stop
16821687
16831688 where
@@ -1687,16 +1692,16 @@ wordsBy predicate f (Stream step state) = Stream (stepOuter f) (Just state)
16871692 case res of
16881693 Yield x s -> do
16891694 if predicate x
1690− then done acc >>= \r -> return $ Yield r (Just s)
1695+ then done acc >>= \r -> return $ Yield r (WordsByJust s)
16911696 else do
16921697 sfs <- fstep acc x
16931698 case sfs of
16941699 FL.Partial fs1 -> go SPEC s fs1
1695− FL.Done fb -> return $ Yield fb (Just s)
1700+ FL.Done fb -> return $ Yield fb (WordsByJust s)
16961701 Skip s -> go SPEC s acc
1697− Stop -> done acc >>= \r -> return $ Yield r Nothing
1702+ Stop -> done acc >>= \r -> return $ Yield r WordsByNothing
16981703
1699− stepOuter _ _ Nothing = return Stop
1704+ stepOuter _ _ WordsByNothing = return Stop
17001705
17011706-- String search algorithms:
17021707-- http://www-igm.univ-mlv.fr/~lecroq/string/index.html