on master help sign in
Export the Tee type via Data.Fold (#2166)
f5fa162Ranjeet Ranjan
core/src/Streamly/Data/Fold.hs+1
@@ -131,6 +131,7 @@ module Streamly.Data.Fold
131 -- * Fold Type
131 -- * Fold Type
132
132
133 Fold -- (..)
133 Fold -- (..)
134+ , Tee (..)
134
135
135 -- * Constructors
136 -- * Constructors
136 , foldl'
137 , foldl'
core/src/Streamly/Data/Fold/Tee.hs+1−27
@@ -6,34 +6,8 @@
6-- Stability : released
6-- Stability : released
7-- Portability : GHC
7-- Portability : GHC
8--
8--
9−-- The 'Tee' type is a newtype wrapper over the 'Streamly.Data.Fold.Fold' type providing
10−-- distributive 'Applicative', 'Semigroup', 'Monoid', 'Num', 'Floating' and
11−-- 'Fractional' instances. The input received by the composed 'Tee' is
12−-- replicated and distributed to both the constituent Tees.
13−--
14−-- For example, to compute the average of numbers in a stream without going
15−-- through the stream twice:
16−--
17−-- >>> import Streamly.Data.Fold.Tee (Tee(..))
18−-- >>> import qualified Streamly.Data.Stream as Stream
19−-- >>> import qualified Streamly.Data.Fold as Fold
20−--
21−-- >>> avg = (/) <$> (Tee Fold.sum) <*> (Tee $ fmap fromIntegral Fold.length)
22−-- >>> Stream.fold (unTee avg) $ Stream.fromList [1.0..100.0]
23−-- 50.5
24−--
25−-- Similarly, the 'Semigroup' and 'Monoid' instances of 'Tee' distribute the
26−-- input to both the folds and combine the outputs using Monoid or Semigroup
27−-- instances of the output types:
28−--
29−-- >>> import Data.Monoid (Sum(..))
30−-- >>> t = Tee Fold.one <> Tee Fold.latest
31−-- >>> Stream.fold (unTee t) (fmap Sum $ Stream.enumerateFromTo 1.0 100.0)
32−-- Just (Sum {getSum = 101.0})
33−--
34−-- The 'Num', 'Floating', and 'Fractional' instances work in the same way.
35−--
36module Streamly.Data.Fold.Tee
9module Streamly.Data.Fold.Tee
10+{-# DEPRECATED "Please use \"Streamly.Data.Fold\" instead." #-}
37 ( Tee(..)
11 ( Tee(..)
38 )
12 )
39where
13where
core/src/Streamly/Internal/Data/Fold.hs+2
@@ -18,6 +18,7 @@ module Streamly.Internal.Data.Fold
18 -- * Fold Type
18 -- * Fold Type
19 Step (..)
19 Step (..)
20 , Fold (..)
20 , Fold (..)
21+ , Tee (..)
21
22
22 -- * Constructors
23 -- * Constructors
23 -- | Which constructor to use?
24 -- | Which constructor to use?
@@ -334,6 +335,7 @@ import Prelude hiding
334 , scanl, scanl1, replicate, concatMap, mconcat, foldMap, unzip
335 , scanl, scanl1, replicate, concatMap, mconcat, foldMap, unzip
335 , span, splitAt, break, mapM, zip, maybe)
336 , span, splitAt, break, mapM, zip, maybe)
336import Streamly.Internal.Data.Fold.Type
337import Streamly.Internal.Data.Fold.Type
338+import Streamly.Internal.Data.Fold.Tee
337
339
338-- $setup
340-- $setup
339-- >>> :m
341-- >>> :m
core/src/Streamly/Internal/Data/Fold/Tee.hs+25
@@ -24,6 +24,31 @@ import qualified Streamly.Internal.Data.Fold.Type as Fold
24-- 'Applicative', 'Semigroup', 'Monoid', 'Num', 'Floating' and 'Fractional'
24-- 'Applicative', 'Semigroup', 'Monoid', 'Num', 'Floating' and 'Fractional'
25-- instances.
25-- instances.
26--
26--
27+-- The input received by the
28+-- composed 'Tee' is replicated and distributed to both the constituent Tees.
29+--
30+-- For example, to compute the average of numbers in a stream without going
31+-- through the stream twice:
32+--
33+-- >>> import Streamly.Internal.Data.Fold.Tee (Tee(..))
34+-- >>> import qualified Streamly.Data.Stream as Stream
35+-- >>> import qualified Streamly.Data.Fold as Fold
36+--
37+-- >>> avg = (/) <$> (Tee Fold.sum) <*> (Tee $ fmap fromIntegral Fold.length)
38+-- >>> Stream.fold (unTee avg) $ Stream.fromList [1.0..100.0]
39+-- 50.5
40+--
41+-- Similarly, the 'Semigroup' and 'Monoid' instances of 'Tee' distribute the
42+-- input to both the folds and combine the outputs using Monoid or Semigroup
43+-- instances of the output types:
44+--
45+-- >>> import Data.Monoid (Sum(..))
46+-- >>> t = Tee Fold.one <> Tee Fold.latest
47+-- >>> Stream.fold (unTee t) (fmap Sum $ Stream.enumerateFromTo 1.0 100.0)
48+-- Just (Sum {getSum = 101.0})
49+--
50+-- The 'Num', 'Floating', and 'Fractional' instances work in the same way.
51+--
27newtype Tee m a b =
52newtype Tee m a b =
28 Tee { unTee :: Fold m a b }
53 Tee { unTee :: Fold m a b }
29 deriving (Functor)
54 deriving (Functor)