Skip to content

Commit 0936362

Browse files
author
Jaro Reinders
committed
Small streaming optimizations
1 parent f4a28a6 commit 0936362

File tree

1 file changed

+42
-40
lines changed

1 file changed

+42
-40
lines changed

vector-stream/src/Data/Stream/Monadic.hs

Lines changed: 42 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -245,12 +245,13 @@ Stream stepa ta ++ Stream stepb tb = Stream step (Left ta)
245245
r <- stepa sa
246246
case r of
247247
Yield x sa' -> return $ Yield x (Left sa')
248-
Done -> step (Right tb)
249-
step (Right sb) = do
250-
r <- stepb sb
251-
case r of
252-
Yield x sb' -> return $ Yield x (Right sb')
253-
Done -> return $ Done
248+
Done -> step' tb
249+
step (Right sb) = step' sb
250+
step' sb = do
251+
r <- stepb sb
252+
case r of
253+
Yield x sb' -> return $ Yield x (Right sb')
254+
Done -> return $ Done
254255

255256
-- Accessing elements
256257
-- ------------------
@@ -339,14 +340,16 @@ init (Stream step t) = Stream step' (Nothing, t)
339340
step' (Nothing, s) = do
340341
r <- step s
341342
case r of
342-
Yield x s' -> step' (Just x, s')
343+
Yield x s' -> step'' x s'
343344
Done -> return (error emptyStream)
344345

345-
step' (Just x, s) = liftM (\r ->
346-
case r of
347-
Yield y s' -> Yield x (Just y, s')
348-
Done -> Done
349-
) (step s)
346+
step' (Just x, s) = step'' x s
347+
{-# INLINE_INNER step'' #-}
348+
step'' x s = liftM (\r ->
349+
case r of
350+
Yield y s' -> Yield x (Just y, s')
351+
Done -> Done
352+
) (step s)
350353

351354
-- | All but the first element
352355
tail :: (HasCallStack, Monad m) => Stream m a -> Stream m a
@@ -357,14 +360,16 @@ tail (Stream step t) = Stream step' (Left t)
357360
step' (Left s) = do
358361
r <- step s
359362
case r of
360-
Yield _ s' -> step' (Right s')
363+
Yield _ s' -> step'' s'
361364
Done -> return (error emptyStream)
362365

363-
step' (Right s) = liftM (\r ->
364-
case r of
365-
Yield x s' -> Yield x (Right s')
366-
Done -> Done
367-
) (step s)
366+
step' (Right s) = step'' s
367+
{-# INLINE_INNER step'' #-}
368+
step'' s = liftM (\r ->
369+
case r of
370+
Yield x s' -> Yield x (Right s')
371+
Done -> Done
372+
) (step s)
368373

369374
-- | The first @n@ elements
370375
take :: Monad m => Int -> Stream m a -> Stream m a
@@ -382,21 +387,16 @@ take n (Stream step t) = n `seq` Stream step' (t, 0)
382387
-- | All but the first @n@ elements
383388
drop :: Monad m => Int -> Stream m a -> Stream m a
384389
{-# INLINE_FUSED drop #-}
385-
drop n (Stream step t) = Stream step' (t, Just n)
390+
drop n (Stream step t) = Stream (step' n) t
386391
where
387392
{-# INLINE_INNER step' #-}
388-
step' (s, Just i) | i > 0 = do
389-
r <- step s
390-
case r of
391-
Yield _ s' -> step' (s', Just (i-1))
392-
Done -> return Done
393-
| otherwise = step' (s, Nothing)
394-
395-
step' (s, Nothing) = liftM (\r ->
396-
case r of
397-
Yield x s' -> Yield x (s', Nothing)
398-
Done -> Done
399-
) (step s)
393+
step' i s | i > 0 = do
394+
r <- step s
395+
case r of
396+
Yield _ s' -> step' (i - 1) s'
397+
Done -> return Done
398+
| otherwise = step s
399+
400400

401401
-- Mapping
402402
-- -------
@@ -497,17 +497,19 @@ zipWithM f (Stream stepa ta) (Stream stepb tb) = Stream step (ta, tb, Nothing)
497497
step (sa, sb, Nothing) = do
498498
r <- stepa sa
499499
case r of
500-
Yield x sa' -> step (sa', sb, Just x)
500+
Yield x sa' -> step' sa' sb x
501501
Done -> return Done
502502

503-
step (sa, sb, Just x) = do
504-
r <- stepb sb
505-
case r of
506-
Yield y sb' ->
507-
do
508-
z <- f x y
509-
return $ Yield z (sa, sb', Nothing)
510-
Done -> return Done
503+
step (sa, sb, Just x) = step' sa sb x
504+
{-# INLINE_INNER step' #-}
505+
step' sa sb x = do
506+
r <- stepb sb
507+
case r of
508+
Yield y sb' ->
509+
do
510+
z <- f x y
511+
return $ Yield z (sa, sb', Nothing)
512+
Done -> return Done
511513

512514
zipWithM_ :: Monad m => (a -> b -> m c) -> Stream m a -> Stream m b -> m ()
513515
{-# INLINE zipWithM_ #-}

0 commit comments

Comments
 (0)