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
26import Control.Applicative (liftA2)
27import Control.DeepSeq (NFData(..), NFData1(..))
28import Data.Foldable (Foldable(foldl'), fold)
29import Data.Functor.Identity (Identity(..), runIdentity)
30import Data.Maybe (fromMaybe)
31import Data.Semigroup (Endo(..))
32#if __GLASGOW_HASKELL__ < 808
33import Data.Semigroup (Semigroup(..))
35import GHC.Exts (IsList(..), IsString(..), oneShot)
37 ( Lexeme(Ident), lexP, parens, prec, readPrec, readListPrec
38 , readListPrecDefault)
39import Streamly.Internal.BaseCompat ((#.))
40import Streamly.Internal.Data.Maybe.Strict (Maybe'(..), toMaybe)
41import Streamly.Internal.Data.Stream.Serial (SerialT(..))
42import Streamly.Internal.Data.Stream.StreamK.Type (Stream)
44import qualified Streamly.Internal.Data.Stream.Common as P
45 (cmpBy, eqBy, foldl', foldr, fromList, toList)
46import qualified Streamly.Internal.Data.Stream.StreamK.Type as K
47import qualified Streamly.Internal.Data.Stream.StreamD as D
48import qualified Streamly.Internal.Data.Stream.Serial as Serial
50import Prelude hiding (map, repeat, zipWith)
52#include "Instances.hs"
55-- >>> import qualified Streamly.Prelude as Stream
56-- >>> import Control.Concurrent (threadDelay)
59-- threadDelay (n * 1000000) -- sleep for n seconds
60-- putStrLn (show n ++ " sec") -- print "n sec"
64{-# INLINE zipWithMK #-}
65zipWithMK :: Monad m =>
66 (a -> b -> m c) -> Stream m a -> Stream m b -> Stream m c
68 D.toStreamK $ D.zipWithM f (D.fromStreamK m1) (D.fromStreamK m2)
70{-# INLINE zipWithK #-}
72 => (a -> b -> c) -> Stream m a -> Stream m b -> Stream m c
73zipWithK f = zipWithMK (\a b -> return (f a b))
75------------------------------------------------------------------------------
76-- Serially Zipping Streams
77------------------------------------------------------------------------------
79-- | For 'ZipSerialM' streams:
82-- (<>) = 'Streamly.Prelude.serial'
83-- (<*>) = 'Streamly.Prelude.serial.zipWith' id
86-- Applicative evaluates the streams being zipped serially:
88-- >>> s1 = Stream.fromFoldable [1, 2]
89-- >>> s2 = Stream.fromFoldable [3, 4]
90-- >>> s3 = Stream.fromFoldable [5, 6]
91-- >>> Stream.toList $ Stream.fromZipSerial $ (,,) <$> s1 <*> s2 <*> s3
94-- /Since: 0.2.0 ("Streamly")/
97newtype ZipSerialM m a = ZipSerialM {getZipSerialM :: Stream m a}
98 deriving (Semigroup, Monoid)
100-- | An IO stream whose applicative instance zips streams serially.
102-- /Since: 0.2.0 ("Streamly")/
105type ZipSerial = ZipSerialM IO
107consMZip :: Monad m => m a -> ZipSerialM m a -> ZipSerialM m a
108consMZip m (ZipSerialM r) = ZipSerialM $ K.consM m r
110LIST_INSTANCES(ZipSerialM)
111NFDATA1_INSTANCE(ZipSerialM)
113instance Monad m => Functor (ZipSerialM m) where
115 fmap f (ZipSerialM m) = ZipSerialM $ getSerialT $ fmap f (SerialT m)
117instance Monad m => Applicative (ZipSerialM m) where
118 pure = ZipSerialM . getSerialT . Serial.repeat
121 ZipSerialM m1 <*> ZipSerialM m2 = ZipSerialM $ zipWithK id m1 m2
123FOLDABLE_INSTANCE(ZipSerialM)
124TRAVERSABLE_INSTANCE(ZipSerialM)
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