1{-# LANGUAGE UndecidableInstances #-}
4-- Module : Streamly.Internal.Data.Stream.Zip
5-- Copyright : (c) 2017 Composewell Technologies
8-- Maintainer : streamly@composewell.com
9-- Stability : experimental
12-- To run examples in this module:
14-- >>> import qualified Streamly.Prelude as Stream
16module Streamly.Internal.Data.Stream.Zip
35import Control.Applicative (liftA2)
36import Control.DeepSeq (NFData(..))
37#if MIN_VERSION_deepseq(1,4,3)
38import Control.DeepSeq (NFData1(..))
40import Data.Foldable (Foldable(foldl'), fold)
41import Data.Functor.Identity (Identity(..), runIdentity)
42import Data.Maybe (fromMaybe)
43import Data.Semigroup (Endo(..))
44#if __GLASGOW_HASKELL__ < 808
45import Data.Semigroup (Semigroup(..))
47import GHC.Exts (IsList(..), IsString(..))
49 ( Lexeme(Ident), lexP, parens, prec, readPrec, readListPrec
50 , readListPrecDefault)
51import Streamly.Internal.BaseCompat ((#.), errorWithoutStackTrace, oneShot)
52import Streamly.Internal.Control.Concurrent (MonadAsync)
53import Streamly.Internal.Data.Maybe.Strict (Maybe'(..), toMaybe)
54import Streamly.Internal.Data.Stream.Serial (SerialT(..))
55import Streamly.Internal.Data.Stream.StreamK.Type (Stream)
57import qualified Streamly.Internal.Data.Stream.Parallel as Par
58import qualified Streamly.Internal.Data.Stream.Prelude as P
59 (cmpBy, eqBy, foldl', foldr, fromList, toList)
60import qualified Streamly.Internal.Data.Stream.StreamK.Type as K
61import qualified Streamly.Internal.Data.Stream.StreamD as D
62import qualified Streamly.Internal.Data.Stream.Serial as Serial
64import Prelude hiding (map, repeat, zipWith, errorWithoutStackTrace)
66#include "Instances.hs"
69-- >>> import qualified Streamly.Prelude as Stream
70-- >>> import Control.Concurrent (threadDelay)
73-- threadDelay (n * 1000000) -- sleep for n seconds
74-- putStrLn (show n ++ " sec") -- print "n sec"
78{-# INLINE zipWithMK #-}
79zipWithMK :: Monad m =>
80 (a -> b -> m c) -> Stream m a -> Stream m b -> Stream m c
82 D.toStreamK $ D.zipWithM f (D.fromStreamK m1) (D.fromStreamK m2)
84{-# INLINE zipWithK #-}
86 => (a -> b -> c) -> Stream m a -> Stream m b -> Stream m c
87zipWithK f = zipWithMK (\a b -> return (f a b))
89------------------------------------------------------------------------------
91------------------------------------------------------------------------------
93-- | Like 'zipAsyncWith' but with a monadic zipping function.
96{-# INLINE zipAsyncWithMK #-}
97zipAsyncWithMK :: MonadAsync m
98 => (a -> b -> m c) -> Stream m a -> Stream m b -> Stream m c
99zipAsyncWithMK f m1 m2 = D.toStreamK $
100 D.zipWithM f (Par.mkParallelD $ D.fromStreamK m1)
101 (Par.mkParallelD $ D.fromStreamK m2)
103-- XXX Should we rename this to zipParWith or zipParallelWith? This can happen
104-- along with the change of behvaior to end the stream concurrently.
106-- | Like 'zipWith' but zips concurrently i.e. both the streams being zipped
107-- are evaluated concurrently using the 'ParallelT' concurrent evaluation
108-- style. The maximum number of elements of each stream evaluated in advance
109-- can be controlled by 'maxBuffer'.
111-- The stream ends if stream @a@ or stream @b@ ends. However, if stream @b@
112-- ends while we are still evaluating stream @a@ and waiting for a result then
113-- stream will not end until after the evaluation of stream @a@ finishes. This
114-- behavior can potentially be changed in future to end the stream immediately
115-- as soon as any of the stream end is detected.
118{-# INLINE zipAsyncWithK #-}
119zipAsyncWithK :: MonadAsync m
120 => (a -> b -> c) -> Stream m a -> Stream m b -> Stream m c
121zipAsyncWithK f = zipAsyncWithMK (\a b -> return (f a b))
123------------------------------------------------------------------------------
124-- Serially Zipping Streams
125------------------------------------------------------------------------------
127-- | For 'ZipSerialM' streams:
130-- (<>) = 'Streamly.Prelude.serial'
131-- (<*>) = 'Streamly.Prelude.serial.zipWith' id
134-- Applicative evaluates the streams being zipped serially:
136-- >>> s1 = Stream.fromFoldable [1, 2]
137-- >>> s2 = Stream.fromFoldable [3, 4]
138-- >>> s3 = Stream.fromFoldable [5, 6]
139-- >>> Stream.toList $ Stream.fromZipSerial $ (,,) <$> s1 <*> s2 <*> s3
142-- /Since: 0.2.0 ("Streamly")/
145newtype ZipSerialM m a = ZipSerialM {getZipSerialM :: Stream m a}
146 deriving (Semigroup, Monoid)
150{-# DEPRECATED ZipStream "Please use 'ZipSerialM' instead." #-}
151type ZipStream = ZipSerialM
153-- | An IO stream whose applicative instance zips streams serially.
155-- /Since: 0.2.0 ("Streamly")/
158type ZipSerial = ZipSerialM IO
160consMZip :: Monad m => m a -> ZipSerialM m a -> ZipSerialM m a
161consMZip m (ZipSerialM r) = ZipSerialM $ K.consM m r
163LIST_INSTANCES(ZipSerialM)
164NFDATA1_INSTANCE(ZipSerialM)
166instance Monad m => Functor (ZipSerialM m) where
168 fmap f (ZipSerialM m) = ZipSerialM $ getSerialT $ fmap f (SerialT m)
170instance Monad m => Applicative (ZipSerialM m) where
171 pure = ZipSerialM . getSerialT . Serial.repeat
174 ZipSerialM m1 <*> ZipSerialM m2 = ZipSerialM $ zipWithK id m1 m2
176FOLDABLE_INSTANCE(ZipSerialM)
177TRAVERSABLE_INSTANCE(ZipSerialM)
179------------------------------------------------------------------------------
180-- Parallely Zipping Streams
181------------------------------------------------------------------------------
183-- | For 'ZipAsyncM' streams:
186-- (<>) = 'Streamly.Prelude.serial'
187-- (<*>) = 'Streamly.Prelude.serial.zipAsyncWith' id
190-- Applicative evaluates the streams being zipped concurrently, the following
191-- would take half the time that it would take in serial zipping:
193-- >>> s = Stream.fromFoldableM $ Prelude.map delay [1, 1, 1]
194-- >>> Stream.toList $ Stream.fromZipAsync $ (,) <$> s <*> s
196-- [(1,1),(1,1),(1,1)]
198-- /Since: 0.2.0 ("Streamly")/
201newtype ZipAsyncM m a = ZipAsyncM {getZipAsyncM :: Stream m a}
202 deriving (Semigroup, Monoid)
204-- | An IO stream whose applicative instance zips streams wAsyncly.
206-- /Since: 0.2.0 ("Streamly")/
209type ZipAsync = ZipAsyncM IO
211consMZipAsync :: Monad m => m a -> ZipAsyncM m a -> ZipAsyncM m a
212consMZipAsync m (ZipAsyncM r) = ZipAsyncM $ K.consM m r
214instance Monad m => Functor (ZipAsyncM m) where
216 fmap f (ZipAsyncM m) = ZipAsyncM $ getSerialT $ fmap f (SerialT m)
218instance MonadAsync m => Applicative (ZipAsyncM m) where
219 pure = ZipAsyncM . getSerialT . Serial.repeat
222 ZipAsyncM m1 <*> ZipAsyncM m2 = ZipAsyncM $ zipAsyncWithK id m1 m2
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list