@@ -128,6 +128,9 @@ module Streamly.Streams.StreamD
128128 -- * Inserting
129129 , insertBy
130130
131+ -- * Deleting
132+ , deleteBy
133+
131134 -- ** Map and Filter
132135 , mapMaybe
133136 , mapMaybeM
@@ -867,22 +870,50 @@ sequence (Stream step state) = Stream step' state
867870
868871{-# INLINE_NORMAL insertBy #-}
869872insertBy :: Monad m => (a -> a -> Ordering) -> a -> Stream m a -> Stream m a
870−insertBy cmp a (Stream step state) = Stream step' (state, False)
873+insertBy cmp a (Stream step state) = Stream step' (state, False, Nothing)
871874 where
872− step' gst (st, False) = do
875+ {-# INLINE_LATE step' #-}
876+ step' gst (st, False, _) = do
873877 r <- step (rstState gst) st
874878 case r of
875879 Yield x s -> case cmp a x of
876− GT -> return $ Yield x (s, False)
877− _ -> return $ Yield a (st, True)
880+ GT -> return $ Yield x (s, False, Nothing)
881+ _ -> return $ Yield a (s, True, Just x)
882+ Skip s -> return $ Skip (s, False, Nothing)
883+ Stop -> return $ Yield a (st, True, Nothing)
884+
885+ step' _ (_, True, Nothing) = return Stop
886+
887+ step' gst (st, True, Just prev) = do
888+ r <- step (rstState gst) st
889+ case r of
890+ Yield x s -> return $ Yield prev (s, True, Just x)
891+ Skip s -> return $ Skip (s, True, Just prev)
892+ Stop -> return $ Yield prev (st, True, Nothing)
893+
894+------------------------------------------------------------------------------
895+-- Deleting
896+------------------------------------------------------------------------------
897+
898+{-# INLINE_NORMAL deleteBy #-}
899+deleteBy :: Monad m => (a -> a -> Bool) -> a -> Stream m a -> Stream m a
900+deleteBy eq x (Stream step state) = Stream step' (state, False)
901+ where
902+ {-# INLINE_LATE step' #-}
903+ step' gst (st, False) = do
904+ r <- step (rstState gst) st
905+ case r of
906+ Yield y s -> return $
907+ if eq x y then Skip (s, True) else Yield y (s, False)
878908 Skip s -> return $ Skip (s, False)
879− Stop -> return $ Yield a (st, True)
909+ Stop -> return Stop
910+
880911 step' gst (st, True) = do
881912 r <- step (rstState gst) st
882913 case r of
883− Yield x s -> return $ Yield x (s, True)
884− Skip s -> return $ Skip (s, True)
885− Stop -> return Stop
914+ Yield y s -> return $ Yield y (s, True)
915+ Skip s -> return $ Skip (s, True)
916+ Stop -> return Stop
886917
887918------------------------------------------------------------------------------
888919-- Transformation by Map and Filter