on master help sign in
Publish the Streamly.Data.Stream.MkType module
734d2cfHarendra Kumar
src/Streamly/Data/Stream/MkType.hsnew file+81
@@ -0,0 +1,81 @@
1+-- |
2+-- Module : Streamly.Data.Stream.MkType
3+-- Copyright : (c) 2022 Composewell Technologies
4+-- License : BSD-3-Clause
5+-- Maintainer : streamly@composewell.com
6+-- Stability : released
7+-- Portability : GHC
8+--
9+-- Template Haskell macros to create custom newtype wrappers for the 'Stream'
10+-- type, deriving all the usual instances.
11+--
12+-- To use this module, "Streamly.Data.Stream" or "Streamly.Data.Stream.Prelude"
13+-- must be imported as "Stream". Also, it is recommended to import this module
14+-- unqualified to bring everything needed in scope without having to import
15+-- several other modules.
16+--
17+-- >>> import qualified Streamly.Data.Stream.Prelude as Stream
18+-- >>> import Streamly.Internal.Data.Stream.MkType
19+--
20+-- Instead of using these macros directly you could use the generated code as
21+-- well. Use these macros in ghci to generate the required code and paste it in
22+-- your package, you can customize it as you want. See the docs of the macros
23+-- below for examples of viewing the generated code.
24+--
25+-- Example, create an applicative type with zipping apply:
26+--
27+-- >>> import Streamly.Data.Stream.Prelude (Stream)
28+-- >>> :{
29+-- zipApply :: Monad m => Stream m (a -> b) -> Stream m a -> Stream m b
30+-- zipApply = Stream.zipWith ($)
31+-- :}
32+--
33+-- > $(mkZipType "ZipStream" "zipApply" False)
34+--
35+-- Example, create an applicative type with concurrent zipping apply:
36+--
37+-- > $(mkZipType "ParZipStream" "parApply" True)
38+--
39+-- Example, create a monad type with an interleaving cross product bind:
40+--
41+-- >>> :{
42+-- interleaveBind :: Stream m a -> (a -> Stream m b) -> Stream m b
43+-- interleaveBind = flip (Stream.concatMapWith Stream.interleave)
44+-- :}
45+--
46+-- > $(mkCrossType "InterleaveStream" "interleaveBind" False)
47+--
48+-- Example, create a monad type with an eager concurrent cross product bind:
49+--
50+-- >>> :{
51+-- parBind :: Stream.MonadAsync m => Stream m a -> (a -> Stream m b) -> Stream m b
52+-- parBind = flip (Stream.parConcatMap (Stream.eager True))
53+-- :}
54+--
55+-- > $(mkCrossType "ParEagerStream" "parBind" True)
56+--
57+module Streamly.Data.Stream.MkType
58+ (
59+ -- * Imports for Examples
60+ -- $setup
61+
62+ -- * Template Haskell Macros
63+ mkZipType
64+ , mkCrossType
65+
66+ -- * Re-exports
67+ , MonadIO(..)
68+ , MonadThrow(..)
69+ , MonadReader(..)
70+ , MonadTrans(..)
71+ , ap
72+ )
73+where
74+
75+import Streamly.Internal.Data.Stream.MkType
76+
77+-- $setup
78+-- >>> :m
79+-- >>> import Language.Haskell.TH
80+-- >>> import qualified Streamly.Data.Stream.Prelude as Stream
81+-- >>> import Streamly.Internal.Data.Stream.MkType
src/Streamly/Internal/Data/Stream/MkType.hs+4−56
@@ -6,55 +6,6 @@
6-- Stability : experimental
6-- Stability : experimental
7-- Portability : GHC
7-- Portability : GHC
8--
8--
9−-- Template Haskell macros to create custom newtype wrappers for the 'Stream'
10−-- type, deriving all the usual instances.
11−--
12−-- To use this module, "Streamly.Data.Stream" or "Streamly.Data.Stream.Prelude"
13−-- must be imported as "Stream". Also, it is recommended to import this module
14−-- unqualified to bring everything needed in scope without having to import
15−-- several other modules.
16−--
17−-- >>> import Control.Monad.Reader.Class (MonadReader(..))
18−-- >>> import qualified Streamly.Data.Stream.Prelude as Stream
19−-- >>> import Streamly.Internal.Data.Stream.MkType
20−--
21−-- Instead of using these macros directly you could use the generated code as
22−-- well. Use these macros in ghci to generate the required code and paste it in
23−-- your package, you can customize it as you want. See the docs of the macros
24−-- below for examples of viewing the generated code.
25−--
26−-- Example, create an applicative type with zipping apply:
27−--
28−-- >>> import Streamly.Data.Stream.Prelude (Stream)
29−-- >>> :{
30−-- zipApply :: Monad m => Stream m (a -> b) -> Stream m a -> Stream m b
31−-- zipApply = Stream.zipWith ($)
32−-- :}
33−--
34−-- > $(mkZipType "ZipStream" "zipApply" False)
35−--
36−-- Example, create an applicative type with concurrent zipping apply:
37−--
38−-- > $(mkZipType "ParZipStream" "parApply" True)
39−--
40−-- Example, create a monad type with an interleaving cross product bind:
41−--
42−-- >>> :{
43−-- interleaveBind :: Stream m a -> (a -> Stream m b) -> Stream m b
44−-- interleaveBind = flip (Stream.concatMapWith Stream.interleave)
45−-- :}
46−--
47−-- > $(mkCrossType "InterleaveStream" "interleaveBind" False)
48−--
49−-- Example, create a monad type with an eager concurrent cross product bind:
50−--
51−-- >>> :{
52−-- parBind :: Stream.MonadAsync m => Stream m a -> (a -> Stream m b) -> Stream m b
53−-- parBind = flip (Stream.parConcatMap (Stream.eager True))
54−-- :}
55−--
56−-- > $(mkCrossType "ParEagerStream" "parBind" True)
57−--
58module Streamly.Internal.Data.Stream.MkType
9module Streamly.Internal.Data.Stream.MkType
59 (
10 (
60 -- * Imports for Examples
11 -- * Imports for Examples
@@ -67,7 +18,7 @@ module Streamly.Internal.Data.Stream.MkType
67 -- * Re-exports
18 -- * Re-exports
68 , MonadIO(..)
19 , MonadIO(..)
69 , MonadThrow(..)
20 , MonadThrow(..)
70− -- , MonadReader(..)
21+ , MonadReader(..)
71 , MonadTrans(..)
22 , MonadTrans(..)
72 , ap
23 , ap
73 ) where
24 ) where
@@ -82,16 +33,13 @@ import Language.Haskell.TH.Syntax
82import Control.Monad (ap)
33import Control.Monad (ap)
83import Control.Monad.Catch (MonadThrow(..))
34import Control.Monad.Catch (MonadThrow(..))
84import Control.Monad.IO.Class (MonadIO(..))
35import Control.Monad.IO.Class (MonadIO(..))
85−-- import Control.Monad.Reader.Class (MonadReader(..))
36+import Control.Monad.Reader.Class (MonadReader(..))
86import Control.Monad.Trans.Class (MonadTrans(..))
37import Control.Monad.Trans.Class (MonadTrans(..))
87import Prelude hiding (repeat)
38import Prelude hiding (repeat)
88
39
89-- $setup
40-- $setup
90-- >>> :m
41-- >>> :m
91−-- >>> :set -package mtl
92−-- >>> :set -package streamly
93-- >>> import Language.Haskell.TH
42-- >>> import Language.Haskell.TH
94−-- >>> import Control.Monad.Reader.Class (MonadReader(..))
95-- >>> import qualified Streamly.Data.Stream.Prelude as Stream
43-- >>> import qualified Streamly.Data.Stream.Prelude as Stream
96-- >>> import Streamly.Internal.Data.Stream.MkType
44-- >>> import Streamly.Internal.Data.Stream.MkType
97
45
@@ -391,7 +339,7 @@ mkSimpleStreamInstances dtNameStr = do
391 []
339 []
392 ]
340 ]
393 ]
341 ]
394− reader <-
342+ rdr <-
395 instanceD
343 instanceD
396 (sequence
344 (sequence
397 [ appT (conT _Monad) (appT (conT _Type) (varT _m))
345 [ appT (conT _Monad) (appT (conT _Type) (varT _m))
@@ -416,7 +364,7 @@ mkSimpleStreamInstances dtNameStr = do
416 []
364 []
417 ]
365 ]
418 ]
366 ]
419− return [trans, io, throw, reader]
367+ return [trans, io, throw, rdr]
420
368
421 where
369 where
422
370
streamly.cabal+1
@@ -381,6 +381,7 @@ library
381 , Streamly.Internal.Network.Inet.TCP
381 , Streamly.Internal.Network.Inet.TCP
382
382
383 -- Exposed modules
383 -- Exposed modules
384+ , Streamly.Data.Stream.MkType
384 , Streamly.Data.Stream.Prelude
385 , Streamly.Data.Stream.Prelude
385
386
386 -- Network/IO
387 -- Network/IO