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
38import Control.Applicative (liftA2)
39import Control.DeepSeq (NFData(..))
40#if MIN_VERSION_deepseq(1,4,3)
41import Control.DeepSeq (NFData1(..))
43import Data.Foldable (Foldable(foldl'), fold)
44import Data.Functor.Identity (Identity(..), runIdentity)
45import Data.Maybe (fromMaybe)
46import Data.Semigroup (Endo(..))
47#if __GLASGOW_HASKELL__ < 808
48import Data.Semigroup (Semigroup(..))
50import GHC.Exts (IsList(..), IsString(..))
52 ( Lexeme(Ident), lexP, parens, prec, readPrec, readListPrec
53 , readListPrecDefault)
54import Streamly.Internal.BaseCompat ((#.), errorWithoutStackTrace, oneShot)
55import Streamly.Internal.Data.Stream.StreamK (IsStream(..), Stream)
56import Streamly.Internal.Data.Maybe.Strict (Maybe'(..), toMaybe)
57import Streamly.Internal.Data.SVar (MonadAsync)
59import qualified Streamly.Internal.Data.Stream.Parallel as Par
60import qualified Streamly.Internal.Data.Stream.Prelude as P
61 (cmpBy, eqBy, foldl', foldr, fromList, toList, fromStreamS, toStreamS)
62import qualified Streamly.Internal.Data.Stream.StreamK as K (repeat)
63import qualified Streamly.Internal.Data.Stream.StreamK.Type as K
64import qualified Streamly.Internal.Data.Stream.StreamD as D (zipWithM)
65import qualified Streamly.Internal.Data.Stream.StreamD.Type as D
66#ifdef USE_STREAMK_ONLY
67import qualified Streamly.Internal.Data.Stream.StreamK as S (zipWith, zipWithM)
69import qualified Streamly.Internal.Data.Stream.StreamD as S (zipWith, zipWithM)
72import Prelude hiding (map, repeat, zipWith, errorWithoutStackTrace)
74#include "Instances.hs"
77-- >>> import qualified Streamly.Prelude as Stream
78-- >>> import Control.Concurrent (threadDelay)
81-- threadDelay (n * 1000000) -- sleep for n seconds
82-- putStrLn (show n ++ " sec") -- print "n sec"
86-- | Like 'zipWith' but using a monadic zipping function.
89{-# INLINABLE zipWithM #-}
90zipWithM :: (IsStream t, Monad m) => (a -> b -> m c) -> t m a -> t m b -> t m c
91zipWithM f m1 m2 = P.fromStreamS $ S.zipWithM f (P.toStreamS m1) (P.toStreamS m2)
93-- | Zip two streams serially using a pure zipping function.
96-- > S.toList $ S.zipWith (+) (S.fromList [1,2,3]) (S.fromList [4,5,6])
101{-# INLINABLE zipWith #-}
102zipWith :: (IsStream t, Monad m) => (a -> b -> c) -> t m a -> t m b -> t m c
103zipWith f m1 m2 = P.fromStreamS $ S.zipWith f (P.toStreamS m1) (P.toStreamS m2)
105------------------------------------------------------------------------------
107------------------------------------------------------------------------------
109-- | Like 'zipWithM' but zips concurrently i.e. both the streams being zipped
110-- are generated concurrently.
113{-# INLINE zipAsyncWithM #-}
114zipAsyncWithM :: (IsStream t, MonadAsync m)
115 => (a -> b -> m c) -> t m a -> t m b -> t m c
116zipAsyncWithM f m1 m2 = D.fromStreamD $
117 D.zipWithM f (Par.mkParallelD $ D.toStreamD m1)
118 (Par.mkParallelD $ D.toStreamD m2)
120-- | Like 'zipWith' but zips concurrently i.e. both the streams being zipped
121-- are generated concurrently.
124{-# INLINE zipAsyncWith #-}
125zipAsyncWith :: (IsStream t, MonadAsync m)
126 => (a -> b -> c) -> t m a -> t m b -> t m c
127zipAsyncWith f = zipAsyncWithM (\a b -> return (f a b))
129------------------------------------------------------------------------------
130-- Serially Zipping Streams
131------------------------------------------------------------------------------
133-- | For 'ZipSerialM' streams:
136-- (<>) = 'Streamly.Prelude.serial'
137-- (<*>) = 'Streamly.Prelude.serial.zipWith' id
140-- Applicative evaluates the streams being zipped serially:
142-- >>> s1 = Stream.fromFoldable [1, 2]
143-- >>> s2 = Stream.fromFoldable [3, 4]
144-- >>> s3 = Stream.fromFoldable [5, 6]
145-- >>> Stream.toList $ Stream.fromZipSerial $ (,,) <$> s1 <*> s2 <*> s3
148-- /Since: 0.2.0 ("Streamly")/
151newtype ZipSerialM m a = ZipSerialM {getZipSerialM :: Stream m a}
152 deriving (Semigroup, Monoid)
156{-# DEPRECATED ZipStream "Please use 'ZipSerialM' instead." #-}
157type ZipStream = ZipSerialM
159-- | An IO stream whose applicative instance zips streams serially.
161-- /Since: 0.2.0 ("Streamly")/
164type ZipSerial = ZipSerialM IO
166-- | Fix the type of a polymorphic stream as 'ZipSerialM'.
168-- /Since: 0.2.0 ("Streamly")/
171fromZipSerial :: IsStream t => ZipSerialM m a -> t m a
172fromZipSerial = K.adapt
174-- | Same as 'fromZipSerial'.
177{-# DEPRECATED zipping "Please use fromZipSerial instead." #-}
178zipping :: IsStream t => ZipSerialM m a -> t m a
179zipping = fromZipSerial
181consMZip :: Monad m => m a -> ZipSerialM m a -> ZipSerialM m a
182consMZip m ms = fromStream $ K.consMStream m (toStream ms)
184instance IsStream ZipSerialM where
185 toStream = getZipSerialM
186 fromStream = ZipSerialM
189 {-# SPECIALIZE consM :: IO a -> ZipSerialM IO a -> ZipSerialM IO a #-}
190 consM :: Monad m => m a -> ZipSerialM m a -> ZipSerialM m a
194 {-# SPECIALIZE (|:) :: IO a -> ZipSerialM IO a -> ZipSerialM IO a #-}
195 (|:) :: Monad m => m a -> ZipSerialM m a -> ZipSerialM m a
198LIST_INSTANCES(ZipSerialM)
199NFDATA1_INSTANCE(ZipSerialM)
201instance Monad m => Functor (ZipSerialM m) where
203 fmap f (ZipSerialM m) = D.fromStreamD $ D.mapM (return . f) $ D.toStreamD m
205instance Monad m => Applicative (ZipSerialM m) where
206 pure = ZipSerialM . K.repeat
210FOLDABLE_INSTANCE(ZipSerialM)
211TRAVERSABLE_INSTANCE(ZipSerialM)
213------------------------------------------------------------------------------
214-- Parallely Zipping Streams
215------------------------------------------------------------------------------
217-- | For 'ZipAsyncM' streams:
220-- (<>) = 'Streamly.Prelude.serial'
221-- (<*>) = 'Streamly.Prelude.serial.zipAsyncWith' id
224-- Applicative evaluates the streams being zipped concurrently, the following
225-- would take half the time that it would take in serial zipping:
227-- >>> s = Stream.fromFoldableM $ Prelude.map delay [1, 1, 1]
228-- >>> Stream.toList $ Stream.fromZipAsync $ (,) <$> s <*> s
230-- [(1,1),(1,1),(1,1)]
232-- /Since: 0.2.0 ("Streamly")/
235newtype ZipAsyncM m a = ZipAsyncM {getZipAsyncM :: Stream m a}
236 deriving (Semigroup, Monoid)
238-- | An IO stream whose applicative instance zips streams wAsyncly.
240-- /Since: 0.2.0 ("Streamly")/
243type ZipAsync = ZipAsyncM IO
245-- | Fix the type of a polymorphic stream as 'ZipAsyncM'.
247-- /Since: 0.2.0 ("Streamly")/
250fromZipAsync :: IsStream t => ZipAsyncM m a -> t m a
251fromZipAsync = K.adapt
253-- | Same as 'fromZipAsync'.
256{-# DEPRECATED zippingAsync "Please use fromZipAsync instead." #-}
257zippingAsync :: IsStream t => ZipAsyncM m a -> t m a
258zippingAsync = fromZipAsync
260consMZipAsync :: Monad m => m a -> ZipAsyncM m a -> ZipAsyncM m a
261consMZipAsync m ms = fromStream $ K.consMStream m (toStream ms)
263instance IsStream ZipAsyncM where
264 toStream = getZipAsyncM
265 fromStream = ZipAsyncM
268 {-# SPECIALIZE consM :: IO a -> ZipAsyncM IO a -> ZipAsyncM IO a #-}
269 consM :: Monad m => m a -> ZipAsyncM m a -> ZipAsyncM m a
270 consM = consMZipAsync
273 {-# SPECIALIZE (|:) :: IO a -> ZipAsyncM IO a -> ZipAsyncM IO a #-}
274 (|:) :: Monad m => m a -> ZipAsyncM m a -> ZipAsyncM m a
277instance Monad m => Functor (ZipAsyncM m) where
279 fmap f (ZipAsyncM m) = D.fromStreamD $ D.mapM (return . f) $ D.toStreamD m
281instance MonadAsync m => Applicative (ZipAsyncM m) where
282 pure = ZipAsyncM . K.repeat
284 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