2{-# LANGUAGE ConstraintKinds #-}
3{-# LANGUAGE FlexibleContexts #-}
4{-# LANGUAGE FlexibleInstances #-}
5{-# LANGUAGE GeneralizedNewtypeDeriving#-}
6{-# LANGUAGE InstanceSigs #-}
7{-# LANGUAGE MultiParamTypeClasses #-}
8{-# LANGUAGE StandaloneDeriving #-}
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 Data.Semigroup (Semigroup(..))
42import Prelude hiding (map, repeat, zipWith)
44import Streamly.Streams.StreamK (IsStream(..), Stream(..))
45import Streamly.Streams.Async (mkAsync')
46import Streamly.Streams.Serial (map)
47import Streamly.SVar (MonadAsync, rstState)
49import qualified Streamly.Streams.StreamK as K
51#include "Instances.hs"
53------------------------------------------------------------------------------
55------------------------------------------------------------------------------
57{-# INLINE zipWithS #-}
58zipWithS :: (a -> b -> c) -> Stream m a -> Stream m b -> Stream m c
59zipWithS f m1 m2 = go m1 m2
61 go mx my = Stream $ \st stp sng yld -> do
63 let single2 b = sng (f a b)
64 yield2 b rb = yld (f a b) (go ra rb)
65 in unStream my (rstState st) stp single2 yield2
66 let single1 a = merge a K.nil
67 yield1 a ra = merge a ra
68 unStream mx (rstState st) stp single1 yield1
70-- | Zip two streams serially using a pure zipping function.
73{-# INLINABLE zipWith #-}
74zipWith :: IsStream t => (a -> b -> c) -> t m a -> t m b -> t m c
75zipWith f m1 m2 = fromStream $ zipWithS f (toStream m1) (toStream m2)
77-- | Zip two streams serially using a monadic zipping function.
80zipWithM :: (IsStream t, Monad m) => (a -> b -> m c) -> t m a -> t m b -> t m c
81zipWithM f m1 m2 = fromStream $ go (toStream m1) (toStream m2)
83 go mx my = Stream $ \st stp sng yld -> do
85 let runIt x = unStream x (rstState st) stp sng yld
86 single2 b = f a b >>= sng
87 yield2 b rb = f a b >>= \x -> runIt (x `K.cons` go ra rb)
88 in unStream my (rstState st) stp single2 yield2
89 let single1 a = merge a K.nil
90 yield1 a ra = merge a ra
91 unStream mx (rstState st) stp single1 yield1
93------------------------------------------------------------------------------
94-- Serially Zipping Streams
95------------------------------------------------------------------------------
97-- | The applicative instance of 'ZipSerialM' zips a number of streams serially
98-- i.e. it produces one element from each stream serially and then zips all
102-- main = (toList . 'zipSerially' $ (,,) \<$\> s1 \<*\> s2 \<*\> s3) >>= print
103-- where s1 = fromFoldable [1, 2]
104-- s2 = fromFoldable [3, 4]
105-- s3 = fromFoldable [5, 6]
111-- The 'Semigroup' instance of this type works the same way as that of
115newtype ZipSerialM m a = ZipSerialM {getZipSerialM :: Stream m a}
116 deriving (Semigroup, Monoid)
120{-# DEPRECATED ZipStream "Please use 'ZipSerialM' instead." #-}
121type ZipStream = ZipSerialM
123-- | An IO stream whose applicative instance zips streams serially.
126type ZipSerial a = ZipSerialM IO a
128-- | Fix the type of a polymorphic stream as 'ZipSerialM'.
131zipSerially :: IsStream t => ZipSerialM m a -> t m a
134-- | Same as 'zipSerially'.
137{-# DEPRECATED zipping "Please use zipSerially instead." #-}
138zipping :: IsStream t => ZipSerialM m a -> t m a
141instance IsStream ZipSerialM where
142 toStream = getZipSerialM
143 fromStream = ZipSerialM
146 {-# SPECIALIZE consM :: IO a -> ZipSerialM IO a -> ZipSerialM IO a #-}
147 consM :: Monad m => m a -> ZipSerialM m a -> ZipSerialM m a
148 consM m r = fromStream $ K.consMSerial m (toStream r)
151 {-# SPECIALIZE (|:) :: IO a -> ZipSerialM IO a -> ZipSerialM IO a #-}
152 (|:) :: Monad m => m a -> ZipSerialM m a -> ZipSerialM m a
153 m |: r = fromStream $ K.consMSerial m (toStream r)
155instance Monad m => Functor (ZipSerialM m) where
158instance Monad m => Applicative (ZipSerialM m) where
159 pure = ZipSerialM . K.repeat
160 m1 <*> m2 = fromStream $ zipWith id (toStream m1) (toStream m2)
162------------------------------------------------------------------------------
164------------------------------------------------------------------------------
166-- | Zip two streams concurrently (i.e. both the elements being zipped are
167-- generated concurrently) using a pure zipping function.
170zipAsyncWith :: (IsStream t, MonadAsync m)
171 => (a -> b -> c) -> t m a -> t m b -> t m c
172zipAsyncWith f m1 m2 = fromStream $ Stream $ \st stp sng yld -> do
173 ma <- mkAsync' (rstState st) m1
174 mb <- mkAsync' (rstState st) m2
175 unStream (toStream (zipWith f ma mb)) (rstState st) stp sng yld
177-- | Zip two streams asyncly (i.e. both the elements being zipped are generated
178-- concurrently) using a monadic zipping function.
181zipAsyncWithM :: (IsStream t, MonadAsync m)
182 => (a -> b -> m c) -> t m a -> t m b -> t m c
183zipAsyncWithM f m1 m2 = fromStream $ Stream $ \st stp sng yld -> do
184 ma <- mkAsync' (rstState st) m1
185 mb <- mkAsync' (rstState st) m2
186 unStream (toStream (zipWithM f ma mb)) (rstState st) stp sng yld
188------------------------------------------------------------------------------
189-- Parallely Zipping Streams
190------------------------------------------------------------------------------
192-- | Like 'ZipSerialM' but zips in parallel, it generates all the elements to
193-- be zipped concurrently.
196-- main = (toList . 'zipAsyncly' $ (,,) \<$\> s1 \<*\> s2 \<*\> s3) >>= print
197-- where s1 = fromFoldable [1, 2]
198-- s2 = fromFoldable [3, 4]
199-- s3 = fromFoldable [5, 6]
205-- The 'Semigroup' instance of this type works the same way as that of
209newtype ZipAsyncM m a = ZipAsyncM {getZipAsyncM :: Stream m a}
210 deriving (Semigroup, Monoid)
212-- | An IO stream whose applicative instance zips streams wAsyncly.
215type ZipAsync a = ZipAsyncM IO a
217-- | Fix the type of a polymorphic stream as 'ZipAsyncM'.
220zipAsyncly :: IsStream t => ZipAsyncM m a -> t m a
223-- | Same as 'zipAsyncly'.
226{-# DEPRECATED zippingAsync "Please use zipAsyncly instead." #-}
227zippingAsync :: IsStream t => ZipAsyncM m a -> t m a
228zippingAsync = zipAsyncly
229instance IsStream ZipAsyncM where
230 toStream = getZipAsyncM
231 fromStream = ZipAsyncM
234 {-# SPECIALIZE consM :: IO a -> ZipAsyncM IO a -> ZipAsyncM IO a #-}
235 consM :: Monad m => m a -> ZipAsyncM m a -> ZipAsyncM m a
236 consM m r = fromStream $ K.consMSerial m (toStream r)
239 {-# SPECIALIZE (|:) :: IO a -> ZipAsyncM IO a -> ZipAsyncM IO a #-}
240 (|:) :: Monad m => m a -> ZipAsyncM m a -> ZipAsyncM m a
241 m |: r = fromStream $ K.consMSerial m (toStream r)
243instance Monad m => Functor (ZipAsyncM m) where
246instance MonadAsync m => Applicative (ZipAsyncM m) where
247 pure = ZipAsyncM . K.repeat
248 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