on master help sign in
simplify the implementation of drop
d524076Harendra Kumar
src/Streamly/Streams/StreamD.hs+6−13
@@ -538,22 +538,15 @@ takeWhile f = takeWhileM (return . f)
538
538
539{-# INLINE_NORMAL drop #-}
539{-# INLINE_NORMAL drop #-}
540drop :: Monad m => Int -> Stream m a -> Stream m a
540drop :: Monad m => Int -> Stream m a -> Stream m a
541−drop n (Stream step state) = Stream step' (state, Just n)
541+drop n (Stream step state) = Stream step' (state, n)
542 where
542 where
543 {-# INLINE_LATE step' #-}
543 {-# INLINE_LATE step' #-}
544− step' gst (st, Just i)
544+ step' gst (st, i) = do
545− | i > 0 = do
546− r <- step (rstState gst) st
547− case r of
548− Yield _ s -> step' (rstState gst) (s, Just (i - 1))
549− Stop -> return Stop
550− | otherwise = step' gst (st, Nothing)
551−
552− step' gst (st, Nothing) = do
553 r <- step (rstState gst) st
545 r <- step (rstState gst) st
554− return $ case r of
546+ case r of
547+ Yield _ s | i > 0 -> step' gst (s, i - 1)
555− Yield x s -> Yield x (s, Nothing)
548+ Yield x s -> return $ Yield x (s, 0)
556− Stop -> Stop
549+ Stop -> return Stop
557
550
558data DropWhileState s a
551data DropWhileState s a
559 = DropWhileDrop s
552 = DropWhileDrop s