on master help sign in
Use explicit state types instead of Maybe
bf152c8Harendra Kumar
src/Streamly/Internal/Data/Stream/StreamD.hs+19−14
@@ -446,19 +446,21 @@ unfoldrM next state = Stream step state
446unfoldr :: Monad m => (s -> Maybe (a, s)) -> s -> Stream m a
446unfoldr :: Monad m => (s -> Maybe (a, s)) -> s -> Stream m a
447unfoldr f = unfoldrM (return . f)
447unfoldr f = unfoldrM (return . f)
448
448
449+data UnfoldState s = UnfoldNothing | UnfoldJust s
450+
449-- | Convert an 'Unfold' into a 'Stream' by supplying it a seed.
451-- | Convert an 'Unfold' into a 'Stream' by supplying it a seed.
450--
452--
451{-# INLINE_NORMAL unfold #-}
453{-# INLINE_NORMAL unfold #-}
452unfold :: Monad m => Unfold m a b -> a -> Stream m b
454unfold :: 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
454 where
456 where
455 {-# INLINE_LATE step #-}
457 {-# INLINE_LATE step #-}
456− step _ Nothing = inject seed >>= return . Skip . Just
458+ step _ UnfoldNothing = inject seed >>= return . Skip . UnfoldJust
457− step _ (Just st) = do
459+ step _ (UnfoldJust st) = do
458 r <- ustep st
460 r <- ustep st
459 return $ case r of
461 return $ case r of
460− Yield x s -> Yield x (Just s)
462+ Yield x s -> Yield x (UnfoldJust s)
461− Skip s -> Skip (Just s)
463+ Skip s -> Skip (UnfoldJust s)
462 Stop -> Stop
464 Stop -> Stop
463
465
464------------------------------------------------------------------------------
466------------------------------------------------------------------------------
@@ -1657,27 +1659,30 @@ splitSuffixBy :: Monad m
1657 => (a -> Bool) -> Fold m a b -> Stream m a -> Stream m b
1659 => (a -> Bool) -> Fold m a b -> Stream m a -> Stream m b
1658splitSuffixBy predicate f = foldMany1 (FL.sliceSepBy predicate f)
1660splitSuffixBy predicate f = foldMany1 (FL.sliceSepBy predicate f)
1659
1661
1662+data WordsByState s = WordsByJust s | WordsByNothing
1663+
1660{-# INLINE_NORMAL wordsBy #-}
1664{-# INLINE_NORMAL wordsBy #-}
1661wordsBy :: Monad m => (a -> Bool) -> Fold m a b -> Stream m a -> Stream m b
1665wordsBy :: 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)
1663
1668
1664 where
1669 where
1665
1670
1666 {-# INLINE_LATE stepOuter #-}
1671 {-# INLINE_LATE stepOuter #-}
1667− stepOuter (Fold fstep initial done) gst (Just st) = do
1672+ stepOuter (Fold fstep initial done) gst (WordsByJust st) = do
1668 res <- step (adaptState gst) st
1673 res <- step (adaptState gst) st
1669 case res of
1674 case res of
1670 Yield x s -> do
1675 Yield x s -> do
1671 if predicate x
1676 if predicate x
1672− then return $ Skip (Just s)
1677+ then return $ Skip (WordsByJust s)
1673 else do
1678 else do
1674 fs <- initial
1679 fs <- initial
1675 sfs <- fstep fs x
1680 sfs <- fstep fs x
1676 case sfs of
1681 case sfs of
1677 FL.Partial fs1 -> go SPEC s fs1
1682 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)
1679
1684
1680− Skip s -> return $ Skip $ Just s
1685+ Skip s -> return $ Skip $ WordsByJust s
1681 Stop -> return Stop
1686 Stop -> return Stop
1682
1687
1683 where
1688 where
@@ -1687,16 +1692,16 @@ wordsBy predicate f (Stream step state) = Stream (stepOuter f) (Just state)
1687 case res of
1692 case res of
1688 Yield x s -> do
1693 Yield x s -> do
1689 if predicate x
1694 if predicate x
1690− then done acc >>= \r -> return $ Yield r (Just s)
1695+ then done acc >>= \r -> return $ Yield r (WordsByJust s)
1691 else do
1696 else do
1692 sfs <- fstep acc x
1697 sfs <- fstep acc x
1693 case sfs of
1698 case sfs of
1694 FL.Partial fs1 -> go SPEC s fs1
1699 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)
1696 Skip s -> go SPEC s acc
1701 Skip s -> go SPEC s acc
1697− Stop -> done acc >>= \r -> return $ Yield r Nothing
1702+ Stop -> done acc >>= \r -> return $ Yield r WordsByNothing
1698
1703
1699− stepOuter _ _ Nothing = return Stop
1704+ stepOuter _ _ WordsByNothing = return Stop
1700
1705
1701-- String search algorithms:
1706-- String search algorithms:
1702-- http://www-igm.univ-mlv.fr/~lecroq/string/index.html
1707-- http://www-igm.univ-mlv.fr/~lecroq/string/index.html