@@ -13,6 +13,8 @@
13-- To run examples in this module:
13-- To run examples in this module:
15-- >>> import qualified Streamly.Prelude as Stream
15-- >>> import qualified Streamly.Prelude as Stream
16+-- >>> import qualified Streamly.Internal.Data.Stream as D
17+-- >>> import qualified Streamly.Data.Fold as Fold
17module Streamly.Internal.Data.Stream.Zip
19module Streamly.Internal.Data.Stream.Zip
18 {-# DEPRECATED "Use \"Streamly.Data.Stream.MkType\" instead." #-}
20 {-# DEPRECATED "Use \"Streamly.Data.Stream.MkType\" instead." #-}
@@ -23,6 +25,8 @@ module Streamly.Internal.Data.Stream.Zip
23 , zipWithK
25 , zipWithK
24 , zipWithMK
26 , zipWithMK
26 -- * Deprecated
30 -- * Deprecated
27 , ZipStream
31 , ZipStream
@@ -50,6 +54,7 @@ import Streamly.Internal.BaseCompat ((#.))
50import Streamly.Internal.Data.Maybe.Strict (Maybe'(..), toMaybe)
54import Streamly.Internal.Data.Maybe.Strict (Maybe'(..), toMaybe)
51import Streamly.Internal.Data.Stream.Serial (SerialT(..))
55import Streamly.Internal.Data.Stream.Serial (SerialT(..))
52import Streamly.Internal.Data.StreamK (Stream)
56import Streamly.Internal.Data.StreamK (Stream)
57+import Streamly.Internal.Data.Stream.Concurrent (MonadAsync, parZipWith)
54import qualified Streamly.Internal.Data.Stream.Common as P
59import qualified Streamly.Internal.Data.Stream.Common as P
55import qualified Streamly.Internal.Data.StreamK as K
60import qualified Streamly.Internal.Data.StreamK as K
@@ -62,6 +67,8 @@ import Prelude hiding (map, repeat, zipWith)
64-- >>> import qualified Streamly.Prelude as Stream
69-- >>> import qualified Streamly.Prelude as Stream
70+-- >>> import qualified Streamly.Internal.Data.Stream as D
71+-- >>> import qualified Streamly.Data.Fold as Fold
65-- >>> import Control.Concurrent (threadDelay)
72-- >>> import Control.Concurrent (threadDelay)
67-- delay n = do
74-- delay n = do
@@ -136,3 +143,27 @@ instance Monad m => Applicative (ZipSerialM m) where
137FOLDABLE_INSTANCE(ZipSerialM)
144FOLDABLE_INSTANCE(ZipSerialM)
138TRAVERSABLE_INSTANCE(ZipSerialM)
145TRAVERSABLE_INSTANCE(ZipSerialM)
147+-------------------------------------------------------------------------------
149+-------------------------------------------------------------------------------
151+newtype ZipConcurrent m a = ZipConcurrent {getZipConcurrent :: D.Stream m a}
154+-- | An IO stream whose applicative instance zips streams concurrently. Note
155+-- that it uses the default concurrency options.
157+-- >>> s = ZipConcurrent $ D.fromList [1, 2, 3]
158+-- >>> x = (,,) <$> s <*> s <*> s
159+-- >>> D.fold Fold.toList (getZipConcurrent x)
160+-- [(1,1,1),(2,2,2),(3,3,3)]
164+instance MonadAsync m => Applicative (ZipConcurrent m) where
165+ pure = ZipConcurrent . D.repeat
167+ {-# INLINE (<*>) #-}
168+ ZipConcurrent m1 <*> ZipConcurrent m2 =
169+ ZipConcurrent $ parZipWith id id m1 m2