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)
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 Control.Concurrent (threadDelay)
80-- threadDelay (n * 1000000) -- sleep for n seconds
81-- putStrLn (show n ++ " sec") -- print "n sec"
85-- | Like 'zipWith' but using a monadic zipping function.
88{-# INLINABLE zipWithM #-}
89zipWithM :: (IsStream t, Monad m) => (a -> b -> m c) -> t m a -> t m b -> t m c
90zipWithM f m1 m2 = P.fromStreamS $ S.zipWithM f (P.toStreamS m1) (P.toStreamS m2)
92-- | Zip two streams serially using a pure zipping function.
95-- > S.toList $ S.zipWith (+) (S.fromList [1,2,3]) (S.fromList [4,5,6])
100{-# INLINABLE zipWith #-}
101zipWith :: (IsStream t, Monad m) => (a -> b -> c) -> t m a -> t m b -> t m c
102zipWith f m1 m2 = P.fromStreamS $ S.zipWith f (P.toStreamS m1) (P.toStreamS m2)
104------------------------------------------------------------------------------
106------------------------------------------------------------------------------
108-- | Like 'zipWithM' but zips concurrently i.e. both the streams being zipped
109-- are generated concurrently.
112{-# INLINE zipAsyncWithM #-}
113zipAsyncWithM :: (IsStream t, MonadAsync m)
114 => (a -> b -> m c) -> t m a -> t m b -> t m c
115zipAsyncWithM f m1 m2 = D.fromStreamD $
116 D.zipWithM f (Par.mkParallelD $ D.toStreamD m1)
117 (Par.mkParallelD $ D.toStreamD m2)
119-- | Like 'zipWith' but zips concurrently i.e. both the streams being zipped
120-- are generated concurrently.
123{-# INLINE zipAsyncWith #-}
124zipAsyncWith :: (IsStream t, MonadAsync m)
125 => (a -> b -> c) -> t m a -> t m b -> t m c
126zipAsyncWith f = zipAsyncWithM (\a b -> return (f a b))
128------------------------------------------------------------------------------
129-- Serially Zipping Streams
130------------------------------------------------------------------------------
132-- | For 'ZipSerialM' streams:
135-- (<>) = 'Streamly.Prelude.serial'
136-- (<*>) = 'Streamly.Prelude.serial.zipWith' id
139-- Applicative evaluates the streams being zipped serially:
141-- >>> s1 = Stream.fromFoldable [1, 2]
142-- >>> s2 = Stream.fromFoldable [3, 4]
143-- >>> s3 = Stream.fromFoldable [5, 6]
144-- >>> Stream.toList $ Stream.zipSerially $ (,,) <$> s1 <*> s2 <*> s3
147-- /Since: 0.2.0 ("Streamly")/
150newtype ZipSerialM m a = ZipSerialM {getZipSerialM :: Stream m a}
151 deriving (Semigroup, Monoid)
155{-# DEPRECATED ZipStream "Please use 'ZipSerialM' instead." #-}
156type ZipStream = ZipSerialM
158-- | An IO stream whose applicative instance zips streams serially.
160-- /Since: 0.2.0 ("Streamly")/
163type ZipSerial = ZipSerialM IO
165-- | Fix the type of a polymorphic stream as 'ZipSerialM'.
167-- /Since: 0.2.0 ("Streamly")/
170zipSerially :: IsStream t => ZipSerialM m a -> t m a
173-- | Same as 'zipSerially'.
176{-# DEPRECATED zipping "Please use zipSerially instead." #-}
177zipping :: IsStream t => ZipSerialM m a -> t m a
180consMZip :: Monad m => m a -> ZipSerialM m a -> ZipSerialM m a
181consMZip m ms = fromStream $ K.consMStream m (toStream ms)
183instance IsStream ZipSerialM where
184 toStream = getZipSerialM
185 fromStream = ZipSerialM
188 {-# SPECIALIZE consM :: IO a -> ZipSerialM IO a -> ZipSerialM IO a #-}
189 consM :: Monad m => m a -> ZipSerialM m a -> ZipSerialM m a
193 {-# SPECIALIZE (|:) :: IO a -> ZipSerialM IO a -> ZipSerialM IO a #-}
194 (|:) :: Monad m => m a -> ZipSerialM m a -> ZipSerialM m a
197LIST_INSTANCES(ZipSerialM)
198NFDATA1_INSTANCE(ZipSerialM)
200instance Monad m => Functor (ZipSerialM m) where
202 fmap f (ZipSerialM m) = D.fromStreamD $ D.mapM (return . f) $ D.toStreamD m
204instance Monad m => Applicative (ZipSerialM m) where
205 pure = ZipSerialM . K.repeat
209FOLDABLE_INSTANCE(ZipSerialM)
210TRAVERSABLE_INSTANCE(ZipSerialM)
212------------------------------------------------------------------------------
213-- Parallely Zipping Streams
214------------------------------------------------------------------------------
216-- | For 'ZipAsyncM' streams:
219-- (<>) = 'Streamly.Prelude.serial'
220-- (<*>) = 'Streamly.Prelude.serial.zipAsyncWith' id
223-- Applicative evaluates the streams being zipped concurrently, the following
224-- would take half the time that it would take in serial zipping:
226-- >>> s = Stream.fromFoldableM $ Prelude.map delay [1, 1, 1]
227-- >>> Stream.toList $ Stream.zipAsyncly $ (,) <$> s <*> s
229-- [(1,1),(1,1),(1,1)]
231-- /Since: 0.2.0 ("Streamly")/
234newtype ZipAsyncM m a = ZipAsyncM {getZipAsyncM :: Stream m a}
235 deriving (Semigroup, Monoid)
237-- | An IO stream whose applicative instance zips streams wAsyncly.
239-- /Since: 0.2.0 ("Streamly")/
242type ZipAsync = ZipAsyncM IO
244-- | Fix the type of a polymorphic stream as 'ZipAsyncM'.
246-- /Since: 0.2.0 ("Streamly")/
249zipAsyncly :: IsStream t => ZipAsyncM m a -> t m a
252-- | Same as 'zipAsyncly'.
255{-# DEPRECATED zippingAsync "Please use zipAsyncly instead." #-}
256zippingAsync :: IsStream t => ZipAsyncM m a -> t m a
257zippingAsync = zipAsyncly
259consMZipAsync :: Monad m => m a -> ZipAsyncM m a -> ZipAsyncM m a
260consMZipAsync m ms = fromStream $ K.consMStream m (toStream ms)
262instance IsStream ZipAsyncM where
263 toStream = getZipAsyncM
264 fromStream = ZipAsyncM
267 {-# SPECIALIZE consM :: IO a -> ZipAsyncM IO a -> ZipAsyncM IO a #-}
268 consM :: Monad m => m a -> ZipAsyncM m a -> ZipAsyncM m a
269 consM = consMZipAsync
272 {-# SPECIALIZE (|:) :: IO a -> ZipAsyncM IO a -> ZipAsyncM IO a #-}
273 (|:) :: Monad m => m a -> ZipAsyncM m a -> ZipAsyncM m a
276instance Monad m => Functor (ZipAsyncM m) where
278 fmap f (ZipAsyncM m) = D.fromStreamD $ D.mapM (return . f) $ D.toStreamD m
280instance MonadAsync m => Applicative (ZipAsyncM m) where
281 pure = ZipAsyncM . K.repeat
283 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