2{-# LANGUAGE ConstraintKinds #-}
3{-# LANGUAGE FlexibleContexts #-}
4{-# LANGUAGE FlexibleInstances #-}
5{-# LANGUAGE GeneralizedNewtypeDeriving#-}
6{-# LANGUAGE InstanceSigs #-}
7{-# LANGUAGE MultiParamTypeClasses #-}
8{-# LANGUAGE TypeFamilies #-}
9{-# LANGUAGE UndecidableInstances #-} -- XXX
12-- Module : Streamly.Streams.Zip
13-- Copyright : (c) 2017 Harendra Kumar
16-- Maintainer : harendra.kumar@gmail.com
17-- Stability : experimental
21module Streamly.Streams.Zip
30 , ZipStream -- deprecated
32 , zipping -- deprecated
37 , zippingAsync -- deprecated
41import Control.Applicative (liftA2)
42import Control.DeepSeq (NFData(..), NFData1(..), rnf1)
43import Data.Functor.Identity (Identity, runIdentity)
44import Data.Foldable (fold)
45import Data.Semigroup (Semigroup(..))
46import GHC.Exts (IsList(..), IsString(..))
47import Text.Read (Lexeme(Ident), lexP, parens, prec, readPrec, readListPrec,
49import Prelude hiding (map, repeat, zipWith)
51import Streamly.Streams.StreamK (IsStream(..), Stream, mkStream, foldStream)
52import Streamly.Streams.Async (mkAsync')
53import Streamly.Streams.Serial (map)
54import Streamly.SVar (MonadAsync, adaptState)
56import qualified Streamly.Streams.Prelude as P
57import qualified Streamly.Streams.StreamK as K
59#include "Instances.hs"
61------------------------------------------------------------------------------
62-- Serially Zipping Streams
63------------------------------------------------------------------------------
65-- | The applicative instance of 'ZipSerialM' zips a number of streams serially
66-- i.e. it produces one element from each stream serially and then zips all
70-- main = (toList . 'zipSerially' $ (,,) \<$\> s1 \<*\> s2 \<*\> s3) >>= print
71-- where s1 = fromFoldable [1, 2]
72-- s2 = fromFoldable [3, 4]
73-- s3 = fromFoldable [5, 6]
79-- The 'Semigroup' instance of this type works the same way as that of
83newtype ZipSerialM m a = ZipSerialM {getZipSerialM :: Stream m a}
84 deriving (Semigroup, Monoid)
88{-# DEPRECATED ZipStream "Please use 'ZipSerialM' instead." #-}
89type ZipStream = ZipSerialM
91-- | An IO stream whose applicative instance zips streams serially.
94type ZipSerial = ZipSerialM IO
96-- | Fix the type of a polymorphic stream as 'ZipSerialM'.
99zipSerially :: IsStream t => ZipSerialM m a -> t m a
102-- | Same as 'zipSerially'.
105{-# DEPRECATED zipping "Please use zipSerially instead." #-}
106zipping :: IsStream t => ZipSerialM m a -> t m a
109consMZip :: Monad m => m a -> ZipSerialM m a -> ZipSerialM m a
110consMZip m ms = fromStream $ K.consMStream m (toStream ms)
112instance IsStream ZipSerialM where
113 toStream = getZipSerialM
114 fromStream = ZipSerialM
117 {-# SPECIALIZE consM :: IO a -> ZipSerialM IO a -> ZipSerialM IO a #-}
118 consM :: Monad m => m a -> ZipSerialM m a -> ZipSerialM m a
122 {-# SPECIALIZE (|:) :: IO a -> ZipSerialM IO a -> ZipSerialM IO a #-}
123 (|:) :: Monad m => m a -> ZipSerialM m a -> ZipSerialM m a
126LIST_INSTANCES(ZipSerialM)
128instance Monad m => Functor (ZipSerialM m) where
131instance Monad m => Applicative (ZipSerialM m) where
132 pure = ZipSerialM . K.repeat
135FOLDABLE_INSTANCE(ZipSerialM)
136TRAVERSABLE_INSTANCE(ZipSerialM)
138------------------------------------------------------------------------------
140------------------------------------------------------------------------------
142-- | Like 'zipWith' but zips concurrently i.e. both the streams being zipped
143-- are generated concurrently.
146{-# INLINABLE zipAsyncWith #-}
147zipAsyncWith :: (IsStream t, MonadAsync m)
148 => (a -> b -> c) -> t m a -> t m b -> t m c
149zipAsyncWith f m1 m2 = mkStream $ \st stp sng yld -> do
150 ma <- mkAsync' (adaptState st) m1
151 mb <- mkAsync' (adaptState st) m2
152 foldStream st stp sng yld (K.zipWith f ma mb)
154-- | Like 'zipWithM' but zips concurrently i.e. both the streams being zipped
155-- are generated concurrently.
158{-# INLINABLE zipAsyncWithM #-}
159zipAsyncWithM :: (IsStream t, MonadAsync m)
160 => (a -> b -> m c) -> t m a -> t m b -> t m c
161zipAsyncWithM f m1 m2 = mkStream $ \st stp sng yld -> do
162 ma <- mkAsync' (adaptState st) m1
163 mb <- mkAsync' (adaptState st) m2
164 foldStream st stp sng yld (K.zipWithM f ma mb)
166------------------------------------------------------------------------------
167-- Parallely Zipping Streams
168------------------------------------------------------------------------------
170-- | Like 'ZipSerialM' but zips in parallel, it generates all the elements to
171-- be zipped concurrently.
174-- main = (toList . 'zipAsyncly' $ (,,) \<$\> s1 \<*\> s2 \<*\> s3) >>= print
175-- where s1 = fromFoldable [1, 2]
176-- s2 = fromFoldable [3, 4]
177-- s3 = fromFoldable [5, 6]
183-- The 'Semigroup' instance of this type works the same way as that of
187newtype ZipAsyncM m a = ZipAsyncM {getZipAsyncM :: Stream m a}
188 deriving (Semigroup, Monoid)
190-- | An IO stream whose applicative instance zips streams wAsyncly.
193type ZipAsync = ZipAsyncM IO
195-- | Fix the type of a polymorphic stream as 'ZipAsyncM'.
198zipAsyncly :: IsStream t => ZipAsyncM m a -> t m a
201-- | Same as 'zipAsyncly'.
204{-# DEPRECATED zippingAsync "Please use zipAsyncly instead." #-}
205zippingAsync :: IsStream t => ZipAsyncM m a -> t m a
206zippingAsync = zipAsyncly
208consMZipAsync :: Monad m => m a -> ZipAsyncM m a -> ZipAsyncM m a
209consMZipAsync m ms = fromStream $ K.consMStream m (toStream ms)
211instance IsStream ZipAsyncM where
212 toStream = getZipAsyncM
213 fromStream = ZipAsyncM
216 {-# SPECIALIZE consM :: IO a -> ZipAsyncM IO a -> ZipAsyncM IO a #-}
217 consM :: Monad m => m a -> ZipAsyncM m a -> ZipAsyncM m a
218 consM = consMZipAsync
221 {-# SPECIALIZE (|:) :: IO a -> ZipAsyncM IO a -> ZipAsyncM IO a #-}
222 (|:) :: Monad m => m a -> ZipAsyncM m a -> ZipAsyncM m a
225instance Monad m => Functor (ZipAsyncM m) where
228instance MonadAsync m => Applicative (ZipAsyncM m) where
229 pure = ZipAsyncM . K.repeat
230 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