@@ -245,12 +245,13 @@ Stream stepa ta ++ Stream stepb tb = Stream step (Left ta)
245
245
r <- stepa sa
246
246
case r of
247
247
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
254
255
255
256
-- Accessing elements
256
257
-- ------------------
@@ -339,14 +340,16 @@ init (Stream step t) = Stream step' (Nothing, t)
339
340
step' (Nothing , s) = do
340
341
r <- step s
341
342
case r of
342
- Yield x s' -> step' ( Just x, s')
343
+ Yield x s' -> step'' x s'
343
344
Done -> return (error emptyStream)
344
345
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)
350
353
351
354
-- | All but the first element
352
355
tail :: (HasCallStack , Monad m ) => Stream m a -> Stream m a
@@ -357,14 +360,16 @@ tail (Stream step t) = Stream step' (Left t)
357
360
step' (Left s) = do
358
361
r <- step s
359
362
case r of
360
- Yield _ s' -> step' ( Right s')
363
+ Yield _ s' -> step'' s'
361
364
Done -> return (error emptyStream)
362
365
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)
368
373
369
374
-- | The first @n@ elements
370
375
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)
382
387
-- | All but the first @n@ elements
383
388
drop :: Monad m => Int -> Stream m a -> Stream m a
384
389
{-# INLINE_FUSED drop #-}
385
- drop n (Stream step t) = Stream step' (t, Just n)
390
+ drop n (Stream step t) = Stream ( step' n) t
386
391
where
387
392
{-# 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
+
400
400
401
401
-- Mapping
402
402
-- -------
@@ -497,17 +497,19 @@ zipWithM f (Stream stepa ta) (Stream stepb tb) = Stream step (ta, tb, Nothing)
497
497
step (sa, sb, Nothing ) = do
498
498
r <- stepa sa
499
499
case r of
500
- Yield x sa' -> step ( sa', sb, Just x)
500
+ Yield x sa' -> step' sa' sb x
501
501
Done -> return Done
502
502
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
511
513
512
514
zipWithM_ :: Monad m => (a -> b -> m c ) -> Stream m a -> Stream m b -> m ()
513
515
{-# INLINE zipWithM_ #-}
0 commit comments