on master help sign in
Add Data.Stream.Zip.Concurrent module (#1954)
commite5f17d0627777698a39d929e668c754f0c503138 authorRanjeet Ranjan <ranjeetr@composewell.com> dateTue 18 Oct 2022, 22:54 (4 years ago) parentf20360b
2 files changed+48
src/Streamly/Internal/Data/Stream/Zip/Concurrent.hs+47 streamly.cabal+1
src/Streamly/Internal/Data/Stream/Zip/Concurrent.hsnew file+47
@@ -0,0 +1,47 @@
1+{-# LANGUAGE UndecidableInstances #-}
2+-- |
3+-- Module : Streamly.Internal.Data.Stream.Concurrent
4+-- Copyright : (c) 2017 Composewell Technologies
5+-- License : BSD-3-Clause
6+-- Maintainer : streamly@composewell.com
7+-- Stability : experimental
8+-- Portability : GHC
9+--
10+-- To run examples in this module:
11+--
12+-- >>> import qualified Streamly.Prelude as Stream
13+--
14+module Streamly.Internal.Data.Stream.Zip.Concurrent
15+ (
16+ ZipConcurrent (..)
17+ )
18+where
19+
20+import Streamly.Internal.Data.Stream (Stream)
21+import Streamly.Internal.Data.Stream.Concurrent (MonadAsync, zipWith)
22+
23+import qualified Streamly.Internal.Data.Stream as Stream (repeat)
24+import Prelude hiding (map, repeat, zipWith)
25+
26+-- $setup
27+-- >>> import qualified Streamly.Internal.Data.Stream as Stream
28+-- >>> import qualified Streamly.Data.Fold as Fold
29+--
30+
31+newtype ZipConcurrent m a = ZipConcurrent {getZipConcurrent :: Stream m a}
32+ deriving (Functor)
33+
34+-- | An IO stream whose applicative instance zips streams concurrently.
35+--
36+-- >>> s = ZipConcurrent $ Stream.fromList [1, 2, 3]
37+-- >>> x = (,,) <$> s <*> s <*> s
38+-- >>> Stream.fold Fold.toList (getZipConcurrent x)
39+-- [(1,1,1),(2,2,2),(3,3,3)]
40+--
41+-- @since 0.9.0
42+
43+instance MonadAsync m => Applicative (ZipConcurrent m) where
44+ pure = ZipConcurrent . Stream.repeat
45+
46+ {-# INLINE (<*>) #-}
47+ ZipConcurrent m1 <*> ZipConcurrent m2 = ZipConcurrent $ zipWith id m1 m2
streamly.cabal+1
@@ -345,6 +345,7 @@ library
345345 , Streamly.Internal.Data.Stream.Concurrent.Channel.Operations
346346 , Streamly.Internal.Data.Stream.Concurrent.Channel
347347 , Streamly.Internal.Data.Stream.Concurrent
348+ , Streamly.Internal.Data.Stream.Zip.Concurrent
348349
349350 -- streamly-unicode
350351 , Streamly.Internal.Unicode.Utf8