on master help sign in
Export the Tee type via Data.Fold (#2166)
commitf5fa162a905c4622465076801b2334bf0352890a authorRanjeet Ranjan <ranjeetr@composewell.com> dateTue 27 Dec 2022, 14:09 (4 years ago) parent4afad15
4 files changed+29−27
core/src/Streamly/Data/Fold.hs+1 core/src/Streamly/Data/Fold/Tee.hs+1−27 core/src/Streamly/Internal/Data/Fold.hs+2 core/src/Streamly/Internal/Data/Fold/Tee.hs+25
core/src/Streamly/Data/Fold.hs+1
@@ -131,6 +131,7 @@ module Streamly.Data.Fold
131131 -- * Fold Type
132132
133133 Fold -- (..)
134+ , Tee (..)
134135
135136 -- * Constructors
136137 , foldl'
core/src/Streamly/Data/Fold/Tee.hs+1−27
@@ -6,34 +6,8 @@
66-- Stability : released
77-- Portability : GHC
88--
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−--
369module Streamly.Data.Fold.Tee
10+{-# DEPRECATED "Please use \"Streamly.Data.Fold\" instead." #-}
3711 ( Tee(..)
3812 )
3913where
core/src/Streamly/Internal/Data/Fold.hs+2
@@ -18,6 +18,7 @@ module Streamly.Internal.Data.Fold
1818 -- * Fold Type
1919 Step (..)
2020 , Fold (..)
21+ , Tee (..)
2122
2223 -- * Constructors
2324 -- | Which constructor to use?
@@ -334,6 +335,7 @@ import Prelude hiding
334335 , scanl, scanl1, replicate, concatMap, mconcat, foldMap, unzip
335336 , span, splitAt, break, mapM, zip, maybe)
336337import Streamly.Internal.Data.Fold.Type
338+import Streamly.Internal.Data.Fold.Tee
337339
338340-- $setup
339341-- >>> :m
core/src/Streamly/Internal/Data/Fold/Tee.hs+25
@@ -24,6 +24,31 @@ import qualified Streamly.Internal.Data.Fold.Type as Fold
2424-- 'Applicative', 'Semigroup', 'Monoid', 'Num', 'Floating' and 'Fractional'
2525-- instances.
2626--
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+--
2752newtype Tee m a b =
2853 Tee { unTee :: Fold m a b }
2954 deriving (Functor)