2{-# LANGUAGE ConstraintKinds #-}
3{-# LANGUAGE FlexibleContexts #-}
4{-# LANGUAGE FlexibleInstances #-}
5{-# LANGUAGE GeneralizedNewtypeDeriving#-}
6{-# LANGUAGE InstanceSigs #-}
7{-# LANGUAGE MultiParamTypeClasses #-}
8{-# LANGUAGE UndecidableInstances #-} -- XXX
11-- Module : Streamly.Streams.Zip
12-- Copyright : (c) 2017 Harendra Kumar
15-- Maintainer : harendra.kumar@gmail.com
16-- Stability : experimental
20module Streamly.Streams.Zip
29 , ZipStream -- deprecated
31 , zipping -- deprecated
36 , zippingAsync -- deprecated
40import Data.Semigroup (Semigroup(..))
41import Prelude hiding (map, repeat, zipWith)
43import Streamly.Streams.StreamK (IsStream(..), Stream(..))
44import Streamly.Streams.Async (mkAsync')
45import Streamly.Streams.Serial (map)
46import Streamly.SVar (MonadAsync, rstState)
48import qualified Streamly.Streams.StreamK as K
50#include "Instances.hs"
52------------------------------------------------------------------------------
53-- Serially Zipping Streams
54------------------------------------------------------------------------------
56-- | The applicative instance of 'ZipSerialM' zips a number of streams serially
57-- i.e. it produces one element from each stream serially and then zips all
61-- main = (toList . 'zipSerially' $ (,,) \<$\> s1 \<*\> s2 \<*\> s3) >>= print
62-- where s1 = fromFoldable [1, 2]
63-- s2 = fromFoldable [3, 4]
64-- s3 = fromFoldable [5, 6]
70-- The 'Semigroup' instance of this type works the same way as that of
74newtype ZipSerialM m a = ZipSerialM {getZipSerialM :: Stream m a}
75 deriving (Semigroup, Monoid)
79{-# DEPRECATED ZipStream "Please use 'ZipSerialM' instead." #-}
80type ZipStream = ZipSerialM
82-- | An IO stream whose applicative instance zips streams serially.
85type ZipSerial = ZipSerialM IO
87-- | Fix the type of a polymorphic stream as 'ZipSerialM'.
90zipSerially :: IsStream t => ZipSerialM m a -> t m a
93-- | Same as 'zipSerially'.
96{-# DEPRECATED zipping "Please use zipSerially instead." #-}
97zipping :: IsStream t => ZipSerialM m a -> t m a
100instance IsStream ZipSerialM where
101 toStream = getZipSerialM
102 fromStream = ZipSerialM
105 {-# SPECIALIZE consM :: IO a -> ZipSerialM IO a -> ZipSerialM IO a #-}
106 consM :: Monad m => m a -> ZipSerialM m a -> ZipSerialM m a
107 consM m r = fromStream $ K.consMSerial m (toStream r)
110 {-# SPECIALIZE (|:) :: IO a -> ZipSerialM IO a -> ZipSerialM IO a #-}
111 (|:) :: Monad m => m a -> ZipSerialM m a -> ZipSerialM m a
112 m |: r = fromStream $ K.consMSerial m (toStream r)
114instance Monad m => Functor (ZipSerialM m) where
117instance Monad m => Applicative (ZipSerialM m) where
118 pure = ZipSerialM . K.repeat
119 m1 <*> m2 = fromStream $ K.zipWith id (toStream m1) (toStream m2)
121------------------------------------------------------------------------------
123------------------------------------------------------------------------------
125-- | Zip two streams concurrently (i.e. both the elements being zipped are
126-- generated concurrently) using a pure zipping function.
129zipAsyncWith :: (IsStream t, MonadAsync m)
130 => (a -> b -> c) -> t m a -> t m b -> t m c
131zipAsyncWith f m1 m2 = fromStream $ Stream $ \st stp sng yld -> do
132 ma <- mkAsync' (rstState st) m1
133 mb <- mkAsync' (rstState st) m2
134 unStream (toStream (K.zipWith f ma mb)) (rstState st) stp sng yld
136-- | Zip two streams asyncly (i.e. both the elements being zipped are generated
137-- concurrently) using a monadic zipping function.
140zipAsyncWithM :: (IsStream t, MonadAsync m)
141 => (a -> b -> m c) -> t m a -> t m b -> t m c
142zipAsyncWithM f m1 m2 = fromStream $ Stream $ \st stp sng yld -> do
143 ma <- mkAsync' (rstState st) m1
144 mb <- mkAsync' (rstState st) m2
145 unStream (toStream (K.zipWithM f ma mb)) (rstState st) stp sng yld
147------------------------------------------------------------------------------
148-- Parallely Zipping Streams
149------------------------------------------------------------------------------
151-- | Like 'ZipSerialM' but zips in parallel, it generates all the elements to
152-- be zipped concurrently.
155-- main = (toList . 'zipAsyncly' $ (,,) \<$\> s1 \<*\> s2 \<*\> s3) >>= print
156-- where s1 = fromFoldable [1, 2]
157-- s2 = fromFoldable [3, 4]
158-- s3 = fromFoldable [5, 6]
164-- The 'Semigroup' instance of this type works the same way as that of
168newtype ZipAsyncM m a = ZipAsyncM {getZipAsyncM :: Stream m a}
169 deriving (Semigroup, Monoid)
171-- | An IO stream whose applicative instance zips streams wAsyncly.
174type ZipAsync = ZipAsyncM IO
176-- | Fix the type of a polymorphic stream as 'ZipAsyncM'.
179zipAsyncly :: IsStream t => ZipAsyncM m a -> t m a
182-- | Same as 'zipAsyncly'.
185{-# DEPRECATED zippingAsync "Please use zipAsyncly instead." #-}
186zippingAsync :: IsStream t => ZipAsyncM m a -> t m a
187zippingAsync = zipAsyncly
188instance IsStream ZipAsyncM where
189 toStream = getZipAsyncM
190 fromStream = ZipAsyncM
193 {-# SPECIALIZE consM :: IO a -> ZipAsyncM IO a -> ZipAsyncM IO a #-}
194 consM :: Monad m => m a -> ZipAsyncM m a -> ZipAsyncM m a
195 consM m r = fromStream $ K.consMSerial m (toStream r)
198 {-# SPECIALIZE (|:) :: IO a -> ZipAsyncM IO a -> ZipAsyncM IO a #-}
199 (|:) :: Monad m => m a -> ZipAsyncM m a -> ZipAsyncM m a
200 m |: r = fromStream $ K.consMSerial m (toStream r)
202instance Monad m => Functor (ZipAsyncM m) where
205instance MonadAsync m => Applicative (ZipAsyncM m) where
206 pure = ZipAsyncM . K.repeat
207 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