@@ -90,6 +90,11 @@ module Streamly.Streams.StreamD
93 -- ** Map and Fold
98 -- ** Map and Fold
@@ -580,6 +585,73 @@ findIndices p (Stream step state) = Stream step' (state, 0)
580 Skip s -> Skip (s, i+1)
585 Skip s -> Skip (s, i+1)
581 Stop -> Stop
586 Stop -> Stop
588+------------------------------------------------------------------------------
590+------------------------------------------------------------------------------
592+{-# INLINE_NORMAL isPrefixOf #-}
593+isPrefixOf :: (Eq a, Monad m) => Stream m a -> Stream m a -> m Bool
594+isPrefixOf (Stream stepa ta) (Stream stepb tb) = go (ta, tb, Nothing)
596+ go (sa, sb, Nothing) = do
597+ r <- stepa defState sa
599+ Yield x sa' -> go (sa', sb, Just x)
600+ Skip sa' -> go (sa', sb, Nothing)
603+ go (sa, sb, Just x) = do
604+ r <- stepb defState sb
608+ then go (sa, sb', Nothing)
610+ Skip sb' -> go (sa, sb', Just x)
611+ Stop -> return False
613+{-# INLINE_NORMAL isSubsequenceOf #-}
614+isSubsequenceOf :: (Eq a, Monad m) => Stream m a -> Stream m a -> m Bool
615+isSubsequenceOf (Stream stepa ta) (Stream stepb tb) = go (ta, tb, Nothing)
617+ go (sa, sb, Nothing) = do
618+ r <- stepa defState sa
620+ Yield x sa' -> go (sa', sb, Just x)
621+ Skip sa' -> go (sa', sb, Nothing)
624+ go (sa, sb, Just x) = do
625+ r <- stepb defState sb
629+ then go (sa, sb', Nothing)
630+ else go (sa, sb', Just x)
631+ Skip sb' -> go (sa, sb', Just x)
632+ Stop -> return False
634+{-# INLINE_NORMAL stripPrefix #-}
635+stripPrefix :: (Eq a, Monad m) => Stream m a -> Stream m a -> m (Maybe (Stream m a))
636+stripPrefix (Stream stepa ta) (Stream stepb tb) = go (ta, tb, Nothing)
638+ go (sa, sb, Nothing) = do
639+ r <- stepa defState sa
641+ Yield x sa' -> go (sa', sb, Just x)
642+ Skip sa' -> go (sa', sb, Nothing)
643+ Stop -> return $ Just (Stream stepb sb)
645+ go (sa, sb, Just x) = do
646+ r <- stepb defState sb
650+ then go (sa, sb', Nothing)
652+ Skip sb' -> go (sa, sb', Just x)
653+ Stop -> return Nothing
583------------------------------------------------------------------------------
655------------------------------------------------------------------------------
584-- Map and Fold
656-- Map and Fold
585------------------------------------------------------------------------------
657------------------------------------------------------------------------------