on efe0be3 help sign in
91 lines 2.9 kB history raw
1{-# LANGUAGE UndecidableInstances #-}
3-- |
4-- Module : Streamly.Internal.Data.Stream.Zip
5-- Copyright : (c) 2017 Composewell Technologies
6--
7-- License : BSD3
8-- Maintainer : streamly@composewell.com
9-- Stability : experimental
10-- Portability : GHC
11--
12-- To run examples in this module:
13--
14-- >>> import qualified Streamly.Data.Fold as Fold
15-- >>> import qualified Streamly.Data.Stream as Stream
16-- >>> import qualified Streamly.Internal.Data.Stream.Zip as Stream
17--
18module Streamly.Internal.Data.Stream.Zip
19 (
20 ZipStream (..)
21 , ZipSerialM
22 , ZipSerial
23 )
24where
26import Data.Functor.Identity (Identity(..))
27import GHC.Exts (IsList(..), IsString(..))
28import Streamly.Internal.Data.Stream.Type (Stream)
29import Text.Read
30 ( Lexeme(Ident), lexP, parens, prec, readPrec, readListPrec
31 , readListPrecDefault)
33import qualified Streamly.Internal.Data.Stream.Bottom as Stream
34import qualified Streamly.Internal.Data.Stream.Generate as Stream
36-- $setup
37-- >>> import qualified Streamly.Data.Fold as Fold
38-- >>> import qualified Streamly.Data.Stream as Stream
39-- >>> import qualified Streamly.Internal.Data.Stream.Zip as Stream
41------------------------------------------------------------------------------
42-- Serially Zipping Streams
43------------------------------------------------------------------------------
45-- | For 'ZipStream':
46--
47-- @
48-- (<>) = 'Streamly.Data.Stream.append'
49-- (\<*>) = 'Streamly.Data.Stream.zipWith' id
50-- @
51--
52-- Applicative evaluates the streams being zipped serially:
53--
54-- >>> s1 = Stream.ZipStream $ Stream.fromFoldable [1, 2]
55-- >>> s2 = Stream.ZipStream $ Stream.fromFoldable [3, 4]
56-- >>> s3 = Stream.ZipStream $ Stream.fromFoldable [5, 6]
57-- >>> s = (,,) <$> s1 <*> s2 <*> s3
58-- >>> Stream.fold Fold.toList (Stream.unZipStream s)
59-- [(1,3,5),(2,4,6)]
60--
61newtype ZipStream m a = ZipStream {unZipStream :: Stream m a}
62 deriving (Functor, Semigroup, Monoid)
64deriving instance IsList (ZipStream Identity a)
65deriving instance (a ~ Char) => IsString (ZipStream Identity a)
66deriving instance Eq a => Eq (ZipStream Identity a)
67deriving instance Ord a => Ord (ZipStream Identity a)
68deriving instance (Foldable m, Monad m) => Foldable (ZipStream m)
69deriving instance Traversable (ZipStream Identity)
71instance Show a => Show (ZipStream Identity a) where
72 showsPrec p dl = showParen (p > 10) $
73 showString "fromList " . shows (toList dl)
75instance Read a => Read (ZipStream Identity a) where
76 readPrec = parens $ prec 10 $ do
77 Ident "fromList" <- lexP
78 fromList <$> readPrec
79 readListPrec = readListPrecDefault
81type ZipSerialM = ZipStream
83-- | An IO stream whose applicative instance zips streams serially.
84--
85type ZipSerial = ZipSerialM IO
87instance Monad m => Applicative (ZipStream m) where
88 pure = ZipStream . Stream.repeat
90 {-# INLINE (<*>) #-}
91 ZipStream m1 <*> ZipStream m2 = ZipStream $ Stream.zipWith id m1 m2