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.Control.Concurrent (MonadAsync)
55import Streamly.Internal.BaseCompat ((#.), errorWithoutStackTrace, oneShot)
56import Streamly.Internal.Data.Stream.StreamK (IsStream(..), Stream)
57import Streamly.Internal.Data.Maybe.Strict (Maybe'(..), toMaybe)
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-- | Stream @a@ is evaluated first, followed by stream @b@, the resulting
94-- elements @a@ and @b@ are then zipped using the supplied zip function and the
95-- result @c@ is yielded to the consumer.
97-- If stream @a@ or stream @b@ ends, the zipped stream ends. If stream @b@ ends
98-- first, the element @a@ from previous evaluation of stream @a@ is discarded.
101-- > S.toList $ S.zipWith (+) (S.fromList [1,2,3]) (S.fromList [4,5,6])
106{-# INLINABLE zipWith #-}
107zipWith :: (IsStream t, Monad m) => (a -> b -> c) -> t m a -> t m b -> t m c
108zipWith f m1 m2 = P.fromStreamS $ S.zipWith f (P.toStreamS m1) (P.toStreamS m2)
110------------------------------------------------------------------------------
112------------------------------------------------------------------------------
114-- | Like 'zipAsyncWith' but with a monadic zipping function.
117{-# INLINE zipAsyncWithM #-}
118zipAsyncWithM :: (IsStream t, MonadAsync m)
119 => (a -> b -> m c) -> t m a -> t m b -> t m c
120zipAsyncWithM f m1 m2 = D.fromStreamD $
121 D.zipWithM f (Par.mkParallelD $ D.toStreamD m1)
122 (Par.mkParallelD $ D.toStreamD m2)
124-- XXX Should we rename this to zipParWith or zipParallelWith? This can happen
125-- along with the change of behvaior to end the stream concurrently.
127-- | Like 'zipWith' but zips concurrently i.e. both the streams being zipped
128-- are evaluated concurrently using the 'ParallelT' concurrent evaluation
129-- style. The maximum number of elements of each stream evaluated in advance
130-- can be controlled by 'maxBuffer'.
132-- The stream ends if stream @a@ or stream @b@ ends. However, if stream @b@
133-- ends while we are still evaluating stream @a@ and waiting for a result then
134-- stream will not end until after the evaluation of stream @a@ finishes. This
135-- behavior can potentially be changed in future to end the stream immediately
136-- as soon as any of the stream end is detected.
139{-# INLINE zipAsyncWith #-}
140zipAsyncWith :: (IsStream t, MonadAsync m)
141 => (a -> b -> c) -> t m a -> t m b -> t m c
142zipAsyncWith f = zipAsyncWithM (\a b -> return (f a b))
144------------------------------------------------------------------------------
145-- Serially Zipping Streams
146------------------------------------------------------------------------------
148-- | For 'ZipSerialM' streams:
151-- (<>) = 'Streamly.Prelude.serial'
152-- (<*>) = 'Streamly.Prelude.serial.zipWith' id
155-- Applicative evaluates the streams being zipped serially:
157-- >>> s1 = Stream.fromFoldable [1, 2]
158-- >>> s2 = Stream.fromFoldable [3, 4]
159-- >>> s3 = Stream.fromFoldable [5, 6]
160-- >>> Stream.toList $ Stream.fromZipSerial $ (,,) <$> s1 <*> s2 <*> s3
163-- /Since: 0.2.0 ("Streamly")/
166newtype ZipSerialM m a = ZipSerialM {getZipSerialM :: Stream m a}
167 deriving (Semigroup, Monoid)
171{-# DEPRECATED ZipStream "Please use 'ZipSerialM' instead." #-}
172type ZipStream = ZipSerialM
174-- | An IO stream whose applicative instance zips streams serially.
176-- /Since: 0.2.0 ("Streamly")/
179type ZipSerial = ZipSerialM IO
181-- | Fix the type of a polymorphic stream as 'ZipSerialM'.
183-- /Since: 0.2.0 ("Streamly")/
186fromZipSerial :: IsStream t => ZipSerialM m a -> t m a
187fromZipSerial = K.adapt
189-- | Same as 'fromZipSerial'.
192{-# DEPRECATED zipping "Please use fromZipSerial instead." #-}
193zipping :: IsStream t => ZipSerialM m a -> t m a
194zipping = fromZipSerial
196consMZip :: Monad m => m a -> ZipSerialM m a -> ZipSerialM m a
197consMZip m ms = fromStream $ K.consMStream m (toStream ms)
199instance IsStream ZipSerialM where
200 toStream = getZipSerialM
201 fromStream = ZipSerialM
204 {-# SPECIALIZE consM :: IO a -> ZipSerialM IO a -> ZipSerialM IO a #-}
205 consM :: Monad m => m a -> ZipSerialM m a -> ZipSerialM m a
209 {-# SPECIALIZE (|:) :: IO a -> ZipSerialM IO a -> ZipSerialM IO a #-}
210 (|:) :: Monad m => m a -> ZipSerialM m a -> ZipSerialM m a
213LIST_INSTANCES(ZipSerialM)
214NFDATA1_INSTANCE(ZipSerialM)
216instance Monad m => Functor (ZipSerialM m) where
218 fmap f (ZipSerialM m) = D.fromStreamD $ D.mapM (return . f) $ D.toStreamD m
220instance Monad m => Applicative (ZipSerialM m) where
221 pure = ZipSerialM . K.repeat
225FOLDABLE_INSTANCE(ZipSerialM)
226TRAVERSABLE_INSTANCE(ZipSerialM)
228------------------------------------------------------------------------------
229-- Parallely Zipping Streams
230------------------------------------------------------------------------------
232-- | For 'ZipAsyncM' streams:
235-- (<>) = 'Streamly.Prelude.serial'
236-- (<*>) = 'Streamly.Prelude.serial.zipAsyncWith' id
239-- Applicative evaluates the streams being zipped concurrently, the following
240-- would take half the time that it would take in serial zipping:
242-- >>> s = Stream.fromFoldableM $ Prelude.map delay [1, 1, 1]
243-- >>> Stream.toList $ Stream.fromZipAsync $ (,) <$> s <*> s
245-- [(1,1),(1,1),(1,1)]
247-- /Since: 0.2.0 ("Streamly")/
250newtype ZipAsyncM m a = ZipAsyncM {getZipAsyncM :: Stream m a}
251 deriving (Semigroup, Monoid)
253-- | An IO stream whose applicative instance zips streams wAsyncly.
255-- /Since: 0.2.0 ("Streamly")/
258type ZipAsync = ZipAsyncM IO
260-- | Fix the type of a polymorphic stream as 'ZipAsyncM'.
262-- /Since: 0.2.0 ("Streamly")/
265fromZipAsync :: IsStream t => ZipAsyncM m a -> t m a
266fromZipAsync = K.adapt
268-- | Same as 'fromZipAsync'.
271{-# DEPRECATED zippingAsync "Please use fromZipAsync instead." #-}
272zippingAsync :: IsStream t => ZipAsyncM m a -> t m a
273zippingAsync = fromZipAsync
275consMZipAsync :: Monad m => m a -> ZipAsyncM m a -> ZipAsyncM m a
276consMZipAsync m ms = fromStream $ K.consMStream m (toStream ms)
278instance IsStream ZipAsyncM where
279 toStream = getZipAsyncM
280 fromStream = ZipAsyncM
283 {-# SPECIALIZE consM :: IO a -> ZipAsyncM IO a -> ZipAsyncM IO a #-}
284 consM :: Monad m => m a -> ZipAsyncM m a -> ZipAsyncM m a
285 consM = consMZipAsync
288 {-# SPECIALIZE (|:) :: IO a -> ZipAsyncM IO a -> ZipAsyncM IO a #-}
289 (|:) :: Monad m => m a -> ZipAsyncM m a -> ZipAsyncM m a
292instance Monad m => Functor (ZipAsyncM m) where
294 fmap f (ZipAsyncM m) = D.fromStreamD $ D.mapM (return . f) $ D.toStreamD m
296instance MonadAsync m => Applicative (ZipAsyncM m) where
297 pure = ZipAsyncM . K.repeat
299 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