@@ -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.
27+-- The input received by the
28+-- composed 'Tee' is replicated and distributed to both the constituent Tees.
30+-- For example, to compute the average of numbers in a stream without going
31+-- through the stream twice:
33+-- >>> import Streamly.Internal.Data.Fold.Tee (Tee(..))
34+-- >>> import qualified Streamly.Data.Stream as Stream
35+-- >>> import qualified Streamly.Data.Fold as Fold
37+-- >>> avg = (/) <$> (Tee Fold.sum) <*> (Tee $ fmap fromIntegral Fold.length)
38+-- >>> Stream.fold (unTee avg) $ Stream.fromList [1.0..100.0]
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:
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})
50+-- The 'Num', 'Floating', and 'Fractional' instances work in the same way.
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)