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.Prelude as P
58 (cmpBy, eqBy, foldl', foldr, fromList, toList)
59import qualified Streamly.Internal.Data.Stream.StreamK.Type as K
60import qualified Streamly.Internal.Data.Stream.StreamK as K
61import qualified Streamly.Internal.Data.Stream.StreamD as D
62import qualified Streamly.Internal.Data.Stream.Serial as Serial
63import qualified Streamly.Internal.Data.Stream.SVar.Eliminate as SVar
64import qualified Streamly.Internal.Data.Stream.SVar.Generate as SVar
66import Prelude hiding (map, repeat, zipWith, errorWithoutStackTrace)
67import Streamly.Internal.Data.SVar
69#include "Instances.hs"
72-- >>> import qualified Streamly.Prelude as Stream
73-- >>> import Control.Concurrent (threadDelay)
76-- threadDelay (n * 1000000) -- sleep for n seconds
77-- putStrLn (show n ++ " sec") -- print "n sec"
81{-# INLINE zipWithMK #-}
82zipWithMK :: Monad m =>
83 (a -> b -> m c) -> Stream m a -> Stream m b -> Stream m c
85 D.toStreamK $ D.zipWithM f (D.fromStreamK m1) (D.fromStreamK m2)
87{-# INLINE zipWithK #-}
89 => (a -> b -> c) -> Stream m a -> Stream m b -> Stream m c
90zipWithK f = zipWithMK (\a b -> return (f a b))
92------------------------------------------------------------------------------
94------------------------------------------------------------------------------
96-- | Like 'zipAsyncWith' but with a monadic zipping function.
99{-# INLINE zipAsyncWithMK #-}
100zipAsyncWithMK :: MonadAsync m
101 => (a -> b -> m c) -> Stream m a -> Stream m b -> Stream m c
102zipAsyncWithMK f m1 m2 = K.mkStream $ \st yld sng stp -> do
103 sv <- newParallelVar StopNone (adaptState st)
104 SVar.toSVarParallel (adaptState st) sv $ D.fromStreamK m2
105 K.foldStream st yld sng stp $ K.zipWithM f m1 (getSerialT (SVar.fromSVar sv))
107-- XXX Should we rename this to zipParWith or zipParallelWith? This can happen
108-- along with the change of behvaior to end the stream concurrently.
110-- | Like 'zipWith' but zips concurrently i.e. both the streams being zipped
111-- are evaluated concurrently using the 'ParallelT' concurrent evaluation
112-- style. The maximum number of elements of each stream evaluated in advance
113-- can be controlled by 'maxBuffer'.
115-- The stream ends if stream @a@ or stream @b@ ends. However, if stream @b@
116-- ends while we are still evaluating stream @a@ and waiting for a result then
117-- stream will not end until after the evaluation of stream @a@ finishes. This
118-- behavior can potentially be changed in future to end the stream immediately
119-- as soon as any of the stream end is detected.
122{-# INLINE zipAsyncWithK #-}
123zipAsyncWithK :: MonadAsync m
124 => (a -> b -> c) -> Stream m a -> Stream m b -> Stream m c
125zipAsyncWithK f = zipAsyncWithMK (\a b -> return (f a b))
127------------------------------------------------------------------------------
128-- Serially Zipping Streams
129------------------------------------------------------------------------------
131-- | For 'ZipSerialM' streams:
134-- (<>) = 'Streamly.Prelude.serial'
135-- (<*>) = 'Streamly.Prelude.serial.zipWith' id
138-- Applicative evaluates the streams being zipped serially:
140-- >>> s1 = Stream.fromFoldable [1, 2]
141-- >>> s2 = Stream.fromFoldable [3, 4]
142-- >>> s3 = Stream.fromFoldable [5, 6]
143-- >>> Stream.toList $ Stream.fromZipSerial $ (,,) <$> s1 <*> s2 <*> s3
146-- /Since: 0.2.0 ("Streamly")/
149newtype ZipSerialM m a = ZipSerialM {getZipSerialM :: Stream m a}
150 deriving (Semigroup, Monoid)
154{-# DEPRECATED ZipStream "Please use 'ZipSerialM' instead." #-}
155type ZipStream = ZipSerialM
157-- | An IO stream whose applicative instance zips streams serially.
159-- /Since: 0.2.0 ("Streamly")/
162type ZipSerial = ZipSerialM IO
164consMZip :: Monad m => m a -> ZipSerialM m a -> ZipSerialM m a
165consMZip m (ZipSerialM r) = ZipSerialM $ K.consM m r
167LIST_INSTANCES(ZipSerialM)
168NFDATA1_INSTANCE(ZipSerialM)
170instance Monad m => Functor (ZipSerialM m) where
172 fmap f (ZipSerialM m) = ZipSerialM $ getSerialT $ fmap f (SerialT m)
174instance Monad m => Applicative (ZipSerialM m) where
175 pure = ZipSerialM . getSerialT . Serial.repeat
178 ZipSerialM m1 <*> ZipSerialM m2 = ZipSerialM $ zipWithK id m1 m2
180FOLDABLE_INSTANCE(ZipSerialM)
181TRAVERSABLE_INSTANCE(ZipSerialM)
183------------------------------------------------------------------------------
184-- Parallely Zipping Streams
185------------------------------------------------------------------------------
187-- | For 'ZipAsyncM' streams:
190-- (<>) = 'Streamly.Prelude.serial'
191-- (<*>) = 'Streamly.Prelude.serial.zipAsyncWith' id
194-- Applicative evaluates the streams being zipped concurrently, the following
195-- would take half the time that it would take in serial zipping:
197-- >>> s = Stream.fromFoldableM $ Prelude.map delay [1, 1, 1]
198-- >>> Stream.toList $ Stream.fromZipAsync $ (,) <$> s <*> s
200-- [(1,1),(1,1),(1,1)]
202-- /Since: 0.2.0 ("Streamly")/
205newtype ZipAsyncM m a = ZipAsyncM {getZipAsyncM :: Stream m a}
206 deriving (Semigroup, Monoid)
208-- | An IO stream whose applicative instance zips streams wAsyncly.
210-- /Since: 0.2.0 ("Streamly")/
213type ZipAsync = ZipAsyncM IO
215consMZipAsync :: Monad m => m a -> ZipAsyncM m a -> ZipAsyncM m a
216consMZipAsync m (ZipAsyncM r) = ZipAsyncM $ K.consM m r
218instance Monad m => Functor (ZipAsyncM m) where
220 fmap f (ZipAsyncM m) = ZipAsyncM $ getSerialT $ fmap f (SerialT m)
222instance MonadAsync m => Applicative (ZipAsyncM m) where
223 pure = ZipAsyncM . getSerialT . Serial.repeat
226 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