Skip to content

Commit 47916cd

Browse files
Add "bind" style looping functions to StreamK
1 parent 20fb611 commit 47916cd

2 files changed

Lines changed: 339 additions & 43 deletions

File tree

  • benchmark/Streamly/Benchmark/Data
  • core/src/Streamly/Internal/Data/StreamK

benchmark/Streamly/Benchmark/Data/StreamK.hs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,20 @@ drainMonad s = drain $ do
473473
y <- s
474474
return $ x + y
475475

476+
{-# INLINE drainConcatFor #-}
477+
drainConcatFor :: Monad m => StreamK m Int -> m ()
478+
drainConcatFor s = drain $ do
479+
StreamK.concatFor s $ \x ->
480+
StreamK.concatFor s $ \y ->
481+
StreamK.fromPure $ x + y
482+
483+
{-# INLINE drainConcatForM #-}
484+
drainConcatForM :: Monad m => StreamK m Int -> m ()
485+
drainConcatForM s = drain $ do
486+
StreamK.concatForM s $ \x ->
487+
pure $ StreamK.concatForM s $ \y ->
488+
pure $ StreamK.fromPure $ x + y
489+
476490
{-# INLINE drainMonad3 #-}
477491
drainMonad3 :: Monad m => StreamK m Int -> m ()
478492
drainMonad3 s = drain $ do
@@ -481,6 +495,22 @@ drainMonad3 s = drain $ do
481495
z <- s
482496
return $ x + y + z
483497

498+
{-# INLINE drainConcatFor3 #-}
499+
drainConcatFor3 :: Monad m => StreamK m Int -> m ()
500+
drainConcatFor3 s = drain $ do
501+
StreamK.concatFor s $ \x ->
502+
StreamK.concatFor s $ \y ->
503+
StreamK.concatFor s $ \z ->
504+
StreamK.fromPure $ x + y + z
505+
506+
{-# INLINE drainConcatFor3M #-}
507+
drainConcatFor3M :: Monad m => StreamK m Int -> m ()
508+
drainConcatFor3M s = drain $ do
509+
StreamK.concatForM s $ \x ->
510+
pure $ StreamK.concatForM s $ \y ->
511+
pure $ StreamK.concatForM s $ \z ->
512+
pure $ StreamK.fromPure $ x + y + z
513+
484514
{-# INLINE filterAllOutMonad #-}
485515
filterAllOutMonad
486516
:: Monad m
@@ -493,6 +523,18 @@ filterAllOutMonad str = drain $ do
493523
then return s
494524
else StreamK.nil
495525

526+
{-# INLINE filterAllOutConcatFor #-}
527+
filterAllOutConcatFor
528+
:: Monad m
529+
=> StreamK m Int -> m ()
530+
filterAllOutConcatFor s = drain $ do
531+
StreamK.concatFor s $ \x ->
532+
StreamK.concatFor s $ \y ->
533+
let s1 = x + y
534+
in if s1 < 0
535+
then StreamK.fromPure s1
536+
else StreamK.nil
537+
496538
{-# INLINE filterAllInMonad #-}
497539
filterAllInMonad
498540
:: Monad m
@@ -505,6 +547,18 @@ filterAllInMonad str = drain $ do
505547
then return s
506548
else StreamK.nil
507549

550+
{-# INLINE filterAllInConcatFor #-}
551+
filterAllInConcatFor
552+
:: Monad m
553+
=> StreamK m Int -> m ()
554+
filterAllInConcatFor s = drain $ do
555+
StreamK.concatFor s $ \x ->
556+
StreamK.concatFor s $ \y ->
557+
let s1 = x + y
558+
in if s1 > 0
559+
then StreamK.fromPure s1
560+
else StreamK.nil
561+
508562
-------------------------------------------------------------------------------
509563
-- Nested Composition Pure lists
510564
-------------------------------------------------------------------------------
@@ -606,9 +660,15 @@ o_1_space_nested streamLen =
606660
bgroup "nested"
607661
[ benchFold "drainApplicative" drainApplicative (unfoldrM streamLen2)
608662
, benchFold "drainMonad" drainMonad (unfoldrM streamLen2)
663+
, benchFold "drainConcatFor" drainConcatFor (unfoldrM streamLen2)
664+
, benchFold "drainConcatForM" drainConcatForM (unfoldrM streamLen2)
609665
, benchFold "drainMonad3" drainMonad3 (unfoldrM streamLen3)
666+
, benchFold "drainConcatFor3" drainConcatFor3 (unfoldrM streamLen3)
667+
, benchFold "drainConcatFor3M" drainConcatFor3M (unfoldrM streamLen3)
610668
, benchFold "filterAllInMonad" filterAllInMonad (unfoldrM streamLen2)
669+
, benchFold "filterAllInConcatFor" filterAllInConcatFor (unfoldrM streamLen2)
611670
, benchFold "filterAllOutMonad" filterAllOutMonad (unfoldrM streamLen2)
671+
, benchFold "filterAllOutConcatFor" filterAllOutConcatFor (unfoldrM streamLen2)
612672
, benchFold "drainApplicative (pure)" drainApplicative (unfoldr streamLen2)
613673
, benchFold "drainMonad (pure)" drainMonad (unfoldr streamLen2)
614674
, benchFold "drainMonad3 (pure)" drainMonad3 (unfoldr streamLen3)

0 commit comments

Comments
 (0)