1{-# LANGUAGE UndecidableInstances #-}
2{-# OPTIONS_GHC -Wno-deprecations #-}
5-- Module : Streamly.Internal.Data.Stream.Zip
6-- Copyright : (c) 2017 Composewell Technologies
9-- Maintainer : streamly@composewell.com
10-- Stability : experimental
13-- To run examples in this module:
15-- >>> import qualified Streamly.Prelude as Stream
17module Streamly.Internal.Data.Stream.Zip
30import Control.Applicative (liftA2)
31import Control.DeepSeq (NFData(..))
32#if MIN_VERSION_deepseq(1,4,3)
33import Control.DeepSeq (NFData1(..))
35import Data.Foldable (Foldable(foldl'), fold)
36import Data.Functor.Identity (Identity(..), runIdentity)
37import Data.Maybe (fromMaybe)
38import Data.Semigroup (Endo(..))
39#if __GLASGOW_HASKELL__ < 808
40import Data.Semigroup (Semigroup(..))
42import GHC.Exts (IsList(..), IsString(..), oneShot)
44 ( Lexeme(Ident), lexP, parens, prec, readPrec, readListPrec
45 , readListPrecDefault)
46import Streamly.Internal.BaseCompat ((#.))
47import Streamly.Internal.Data.Maybe.Strict (Maybe'(..), toMaybe)
48import Streamly.Internal.Data.Stream.Serial (SerialT(..))
49import Streamly.Internal.Data.StreamK (Stream)
51import qualified Streamly.Internal.Data.Stream.Common as P
52import qualified Streamly.Internal.Data.StreamK as K
53import qualified Streamly.Internal.Data.Stream as D
54import qualified Streamly.Internal.Data.Stream.Serial as Serial
56import Prelude hiding (map, repeat, zipWith)
58#include "Instances.hs"
61-- >>> import qualified Streamly.Prelude as Stream
62-- >>> import Control.Concurrent (threadDelay)
65-- threadDelay (n * 1000000) -- sleep for n seconds
66-- putStrLn (show n ++ " sec") -- print "n sec"
70{-# INLINE zipWithMK #-}
71zipWithMK :: Monad m =>
72 (a -> b -> m c) -> Stream m a -> Stream m b -> Stream m c
74 D.toStreamK $ D.zipWithM f (D.fromStreamK m1) (D.fromStreamK m2)
76{-# INLINE zipWithK #-}
78 => (a -> b -> c) -> Stream m a -> Stream m b -> Stream m c
79zipWithK f = zipWithMK (\a b -> return (f a b))
81------------------------------------------------------------------------------
82-- Serially Zipping Streams
83------------------------------------------------------------------------------
85-- | For 'ZipSerialM' streams:
88-- (<>) = 'Streamly.Prelude.serial'
89-- (<*>) = 'Streamly.Prelude.serial.zipWith' id
92-- Applicative evaluates the streams being zipped serially:
94-- >>> s1 = Stream.fromFoldable [1, 2]
95-- >>> s2 = Stream.fromFoldable [3, 4]
96-- >>> s3 = Stream.fromFoldable [5, 6]
97-- >>> Stream.toList $ Stream.fromZipSerial $ (,,) <$> s1 <*> s2 <*> s3
100-- /Since: 0.2.0 ("Streamly")/
103newtype ZipSerialM m a = ZipSerialM {getZipSerialM :: Stream m a}
104 deriving (Semigroup, Monoid)
108{-# DEPRECATED ZipStream "Please use 'ZipSerialM' instead." #-}
109type ZipStream = ZipSerialM
111-- | An IO stream whose applicative instance zips streams serially.
113-- /Since: 0.2.0 ("Streamly")/
116type ZipSerial = ZipSerialM IO
118consMZip :: Monad m => m a -> ZipSerialM m a -> ZipSerialM m a
119consMZip m (ZipSerialM r) = ZipSerialM $ K.consM m r
121LIST_INSTANCES(ZipSerialM)
122NFDATA1_INSTANCE(ZipSerialM)
124instance Monad m => Functor (ZipSerialM m) where
126 fmap f (ZipSerialM m) = ZipSerialM $ getSerialT $ fmap f (SerialT m)
128instance Monad m => Applicative (ZipSerialM m) where
129 pure = ZipSerialM . getSerialT . Serial.repeat
132 ZipSerialM m1 <*> ZipSerialM m2 = ZipSerialM $ zipWithK id m1 m2
134FOLDABLE_INSTANCE(ZipSerialM)
135TRAVERSABLE_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