@@ -123,6 +123,7 @@ module Streamly.Streams.StreamD
123 -- * Filtering
123 -- * Filtering
125 , filterM
125 , filterM
127 , takeWhile
128 , takeWhile
128 , takeWhileM
129 , takeWhileM
@@ -146,6 +147,8 @@ module Streamly.Streams.StreamD
146 , mapMaybeM
147 , mapMaybeM
148 -- * Zipping
149 -- * Zipping
149 , zipWith
152 , zipWith
150 , zipWithM
153 , zipWithM
@@ -954,6 +957,25 @@ filterM f (Stream step state) = Stream step' state
954filter :: Monad m => (a -> Bool) -> Stream m a -> Stream m a
957filter :: Monad m => (a -> Bool) -> Stream m a -> Stream m a
955filter f = filterM (return . f)
958filter f = filterM (return . f)
960+{-# INLINE_NORMAL uniq #-}
961+uniq :: (Eq a, Monad m) => Stream m a -> Stream m a
962+uniq (Stream step state) = Stream step' (Nothing, state)
964+ {-# INLINE_LATE step' #-}
965+ step' gst (Nothing, st) = do
966+ r <- step (rstState gst) st
968+ Yield x s -> return $ Yield x (Just x, s)
969+ Skip s -> return $ Skip (Nothing, s)
971+ step' gst (Just x, st) = do
972+ r <- step (rstState gst) st
974+ Yield y s | x == y -> return $ Skip (Just x, s)
975+ | otherwise -> return $ Yield x (Just y, s)
976+ Skip s -> return $ Skip (Just x, s)
957------------------------------------------------------------------------------
979------------------------------------------------------------------------------
958-- Transformation by Mapping
980-- Transformation by Mapping
959------------------------------------------------------------------------------
981------------------------------------------------------------------------------
@@ -1055,6 +1077,32 @@ mapMaybeM f = fmap fromJust . filter isJust . mapM f
1055-- Zipping
1077-- Zipping
1056------------------------------------------------------------------------------
1078------------------------------------------------------------------------------
1080+{-# INLINE_NORMAL indexed #-}
1081+indexed :: Monad m => Stream m a -> Stream m (Int, a)
1082+indexed (Stream step state) = Stream step' (state, 0)
1084+ {-# INLINE_LATE step' #-}
1085+ step' gst (st, i) = i `seq` do
1086+ r <- step (rstState gst) st
1088+ Yield x s -> return $ Yield (i, x) (s, i+1)
1089+ Skip s -> return $ Skip (s, i)
1090+ Stop -> return Stop
1092+{-# INLINE_NORMAL indexedR #-}
1093+indexedR :: Monad m => Int -> Stream m a -> Stream m (Int, a)
1094+indexedR m (Stream step state) = Stream step' (state, m)
1096+ {-# INLINE_LATE step' #-}
1097+ step' gst (st, i) = i `seq` do
1098+ r <- step (rstState gst) st
1100+ Yield x s -> let i' = i - 1
1102+ return $ Yield (i', x) (s, i')
1103+ Skip s -> return $ Skip (s, i)
1104+ Stop -> return Stop
1058{-# INLINE_NORMAL zipWithM #-}
1106{-# INLINE_NORMAL zipWithM #-}
1059zipWithM :: Monad m
1107zipWithM :: Monad m
1060 => (a -> b -> m c) -> Stream m a -> Stream m b -> Stream m c
1108 => (a -> b -> m c) -> Stream m a -> Stream m b -> Stream m c