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
40import Control.Applicative (liftA2)
41import Control.DeepSeq (NFData(..))
42#if MIN_VERSION_deepseq(1,4,3)
43import Control.DeepSeq (NFData1(..))
45import Data.Foldable (Foldable(foldl'), fold)
46import Data.Functor.Identity (Identity(..), runIdentity)
47import Data.Maybe (fromMaybe)
48import Data.Semigroup (Endo(..))
49#if __GLASGOW_HASKELL__ < 808
50import Data.Semigroup (Semigroup(..))
52import GHC.Exts (IsList(..), IsString(..))
54 ( Lexeme(Ident), lexP, parens, prec, readPrec, readListPrec
55 , readListPrecDefault)
56import Streamly.Internal.BaseCompat ((#.), errorWithoutStackTrace)
57import Streamly.Internal.Data.Stream.StreamK (IsStream(..), Stream)
58import Streamly.Internal.Data.Maybe.Strict (Maybe'(..), toMaybe)
59import Streamly.Internal.Data.SVar (MonadAsync)
61import Streamly.Internal.Data.Stream.Serial (SerialT)
63import qualified Streamly.Internal.Data.Stream.Parallel as Par
64import qualified Streamly.Internal.Data.Stream.Prelude as P
65 (cmpBy, eqBy, foldl', foldr, fromList, toList)
66import qualified Streamly.Internal.Data.Stream.StreamK as K
67import qualified Streamly.Internal.Data.Stream.StreamD as D
69import Prelude hiding (map, repeat, zipWith, errorWithoutStackTrace)
71#include "Instances.hs"
74-- >>> import Control.Concurrent (threadDelay)
77-- threadDelay (n * 1000000) -- sleep for n seconds
78-- putStrLn (show n ++ " sec") -- print "n sec"
82-- | Like 'zipWith' but using a monadic zipping function.
85{-# INLINE_EARLY zipWithM #-}
87 (IsStream t, MonadAsync m) => (a -> b -> m c) -> t m a -> t m b -> t m c
89 K.fromStream $ K.zipWithM f (K.toStream m1) (K.toStream m2)
91{-# RULES "zipWithM serial" zipWithM = zipWithMSerial #-}
92{-# INLINE zipWithMSerial #-}
94 Monad m => (a -> b -> m c) -> SerialT m a -> SerialT m b -> SerialT m c
95zipWithMSerial f m1 m2 =
96 D.fromStreamD $ D.zipWithM f (D.toStreamD m1) (D.toStreamD m2)
98-- | Zip two streams serially using a pure zipping function. The zipping
99-- function is applied concurrently for concurrent streams.
102-- > S.toList $ S.zipWith (+) (S.fromList [1,2,3]) (S.fromList [4,5,6])
107{-# INLINE_EARLY zipWith #-}
109 (IsStream t, MonadAsync m) => (a -> b -> c) -> t m a -> t m b -> t m c
110zipWith f m1 m2 = K.fromStream $ K.zipWith f (K.toStream m1) (K.toStream m2)
112{-# RULES "zipWith serial" zipWith = zipWithSerial #-}
113{-# INLINE zipWithSerial #-}
115 Monad m => (a -> b -> c) -> SerialT m a -> SerialT m b -> SerialT m c
116zipWithSerial f m1 m2 =
117 D.fromStreamD $ D.zipWith f (D.toStreamD m1) (D.toStreamD m2)
119------------------------------------------------------------------------------
121------------------------------------------------------------------------------
123-- | Like 'zipWithM' but zips concurrently i.e. both the streams being zipped
124-- are generated concurrently.
127{-# INLINE zipAsyncWithM #-}
128zipAsyncWithM :: (IsStream t, MonadAsync m)
129 => (a -> b -> m c) -> t m a -> t m b -> t m c
130zipAsyncWithM f m1 m2 = D.fromStreamD $
131 D.zipWithM f (Par.mkParallelD $ D.toStreamD m1)
132 (Par.mkParallelD $ D.toStreamD m2)
134-- | Like 'zipWith' but zips concurrently i.e. both the streams being zipped
135-- are generated concurrently.
138{-# INLINE zipAsyncWith #-}
139zipAsyncWith :: (IsStream t, MonadAsync m)
140 => (a -> b -> c) -> t m a -> t m b -> t m c
141zipAsyncWith f = zipAsyncWithM (\a b -> return (f a b))
143------------------------------------------------------------------------------
144-- Serially Zipping Streams
145------------------------------------------------------------------------------
147-- | For 'ZipSerialM' streams:
150-- (<>) = 'Streamly.Prelude.serial'
151-- (<*>) = 'Streamly.Prelude.serial.zipWith' id
154-- Applicative evaluates the streams being zipped serially:
156-- >>> s1 = Stream.fromFoldable [1, 2]
157-- >>> s2 = Stream.fromFoldable [3, 4]
158-- >>> s3 = Stream.fromFoldable [5, 6]
159-- >>> Stream.toList $ Stream.fromZipSerial $ (,,) <$> s1 <*> s2 <*> s3
162-- /Since: 0.2.0 ("Streamly")/
165newtype ZipSerialM m a = ZipSerialM {getZipSerialM :: Stream m a}
166 deriving (Semigroup, Monoid)
170{-# DEPRECATED ZipStream "Please use 'ZipSerialM' instead." #-}
171type ZipStream = ZipSerialM
173-- | An IO stream whose applicative instance zips streams serially.
175-- /Since: 0.2.0 ("Streamly")/
178type ZipSerial = ZipSerialM IO
180-- | Fix the type of a polymorphic stream as 'ZipSerialM'.
182-- /Since: 0.2.0 ("Streamly")/
185fromZipSerial :: IsStream t => ZipSerialM m a -> t m a
186fromZipSerial = K.adapt
188-- | Same as 'fromZipSerial'.
191{-# DEPRECATED zipping "Please use fromZipSerial instead." #-}
192zipping :: IsStream t => ZipSerialM m a -> t m a
193zipping = fromZipSerial
195consMZip :: Monad m => m a -> ZipSerialM m a -> ZipSerialM m a
196consMZip m ms = fromStream $ K.consMStream m (toStream ms)
198instance IsStream ZipSerialM where
199 toStream = getZipSerialM
200 fromStream = ZipSerialM
203 {-# SPECIALIZE consM :: IO a -> ZipSerialM IO a -> ZipSerialM IO a #-}
204 consM :: Monad m => m a -> ZipSerialM m a -> ZipSerialM m a
208 {-# SPECIALIZE (|:) :: IO a -> ZipSerialM IO a -> ZipSerialM IO a #-}
209 (|:) :: Monad m => m a -> ZipSerialM m a -> ZipSerialM m a
212LIST_INSTANCES(ZipSerialM)
213NFDATA1_INSTANCE(ZipSerialM)
215instance Monad m => Functor (ZipSerialM m) where
217 fmap f (ZipSerialM m) = D.fromStreamD $ D.mapM (return . f) $ D.toStreamD m
219instance Monad m => Applicative (ZipSerialM m) where
220 pure = ZipSerialM . K.repeat
222 m1 <*> m2 = fromStream $ toStream $ zipWithSerial id m1_ m2_
224 m1_ = fromStream (toStream m1)
225 m2_ = fromStream (toStream m2)
227FOLDABLE_INSTANCE(ZipSerialM)
228TRAVERSABLE_INSTANCE(ZipSerialM)
230------------------------------------------------------------------------------
231-- Parallely Zipping Streams
232------------------------------------------------------------------------------
234-- | For 'ZipAsyncM' streams:
237-- (<>) = 'Streamly.Prelude.serial'
238-- (<*>) = 'Streamly.Prelude.serial.zipAsyncWith' id
241-- Applicative evaluates the streams being zipped concurrently, the following
242-- would take half the time that it would take in serial zipping:
244-- >>> s = Stream.fromFoldableM $ Prelude.map delay [1, 1, 1]
245-- >>> Stream.toList $ Stream.fromZipAsync $ (,) <$> s <*> s
247-- [(1,1),(1,1),(1,1)]
249-- /Since: 0.2.0 ("Streamly")/
252newtype ZipAsyncM m a = ZipAsyncM {getZipAsyncM :: Stream m a}
253 deriving (Semigroup, Monoid)
255-- | An IO stream whose applicative instance zips streams wAsyncly.
257-- /Since: 0.2.0 ("Streamly")/
260type ZipAsync = ZipAsyncM IO
262-- | Fix the type of a polymorphic stream as 'ZipAsyncM'.
264-- /Since: 0.2.0 ("Streamly")/
267fromZipAsync :: IsStream t => ZipAsyncM m a -> t m a
268fromZipAsync = K.adapt
270-- | Same as 'fromZipAsync'.
273{-# DEPRECATED zippingAsync "Please use fromZipAsync instead." #-}
274zippingAsync :: IsStream t => ZipAsyncM m a -> t m a
275zippingAsync = fromZipAsync
277consMZipAsync :: Monad m => m a -> ZipAsyncM m a -> ZipAsyncM m a
278consMZipAsync m ms = fromStream $ K.consMStream m (toStream ms)
280instance IsStream ZipAsyncM where
281 toStream = getZipAsyncM
282 fromStream = ZipAsyncM
285 {-# SPECIALIZE consM :: IO a -> ZipAsyncM IO a -> ZipAsyncM IO a #-}
286 consM :: Monad m => m a -> ZipAsyncM m a -> ZipAsyncM m a
287 consM = consMZipAsync
290 {-# SPECIALIZE (|:) :: IO a -> ZipAsyncM IO a -> ZipAsyncM IO a #-}
291 (|:) :: Monad m => m a -> ZipAsyncM m a -> ZipAsyncM m a
294instance Monad m => Functor (ZipAsyncM m) where
296 fmap f (ZipAsyncM m) = D.fromStreamD $ D.mapM (return . f) $ D.toStreamD m
298instance MonadAsync m => Applicative (ZipAsyncM m) where
299 pure = ZipAsyncM . K.repeat
301 m1 <*> m2 = zipAsyncWith 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