on 313d178 help sign in
135 lines 3.9 kB history raw
1{-# LANGUAGE UndecidableInstances #-}
2{-# OPTIONS_GHC -Wno-deprecations #-}
4-- |
5-- Module : Streamly.Internal.Data.Stream.Zip
6-- Copyright : (c) 2017 Composewell Technologies
7--
8-- License : BSD3
9-- Maintainer : streamly@composewell.com
10-- Stability : experimental
11-- Portability : GHC
12--
13-- To run examples in this module:
14--
15-- >>> import qualified Streamly.Prelude as Stream
16--
17module Streamly.Internal.Data.Stream.Zip
18 (
19 ZipSerialM (..)
20 , ZipSerial
21 , consMZip
22 , zipWithK
23 , zipWithMK
25 -- * Deprecated
26 , ZipStream
27 )
28where
30import Control.Applicative (liftA2)
31import Control.DeepSeq (NFData(..))
32#if MIN_VERSION_deepseq(1,4,3)
33import Control.DeepSeq (NFData1(..))
34#endif
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(..))
41#endif
42import GHC.Exts (IsList(..), IsString(..), oneShot)
43import Text.Read
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"
60-- $setup
61-- >>> import qualified Streamly.Prelude as Stream
62-- >>> import Control.Concurrent (threadDelay)
63-- >>> :{
64-- delay n = do
65-- threadDelay (n * 1000000) -- sleep for n seconds
66-- putStrLn (show n ++ " sec") -- print "n sec"
67-- return n -- IO Int
68-- :}
70{-# INLINE zipWithMK #-}
71zipWithMK :: Monad m =>
72 (a -> b -> m c) -> Stream m a -> Stream m b -> Stream m c
73zipWithMK f m1 m2 =
74 D.toStreamK $ D.zipWithM f (D.fromStreamK m1) (D.fromStreamK m2)
76{-# INLINE zipWithK #-}
77zipWithK :: Monad m
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:
86--
87-- @
88-- (<>) = 'Streamly.Prelude.serial'
89-- (<*>) = 'Streamly.Prelude.serial.zipWith' id
90-- @
91--
92-- Applicative evaluates the streams being zipped serially:
93--
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
98-- [(1,3,5),(2,4,6)]
99--
100-- /Since: 0.2.0 ("Streamly")/
101--
102-- @since 0.8.0
103newtype ZipSerialM m a = ZipSerialM {getZipSerialM :: Stream m a}
104 deriving (Semigroup, Monoid)
106-- |
107-- @since 0.1.0
108{-# DEPRECATED ZipStream "Please use 'ZipSerialM' instead." #-}
109type ZipStream = ZipSerialM
111-- | An IO stream whose applicative instance zips streams serially.
112--
113-- /Since: 0.2.0 ("Streamly")/
114--
115-- @since 0.8.0
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
125 {-# INLINE fmap #-}
126 fmap f (ZipSerialM m) = ZipSerialM $ getSerialT $ fmap f (SerialT m)
128instance Monad m => Applicative (ZipSerialM m) where
129 pure = ZipSerialM . getSerialT . Serial.repeat
131 {-# INLINE (<*>) #-}
132 ZipSerialM m1 <*> ZipSerialM m2 = ZipSerialM $ zipWithK id m1 m2
134FOLDABLE_INSTANCE(ZipSerialM)
135TRAVERSABLE_INSTANCE(ZipSerialM)