@@ -14,10 +14,10 @@
14-- For example, to compute the average of numbers in a stream without going
14-- For example, to compute the average of numbers in a stream without going
15-- through the stream twice:
15-- through the stream twice:
17−-- >>> import Streamly.Internal.Data.Fold.Tee (mkT, toFold)
17+-- >>> import Streamly.Internal.Data.Fold.Tee (Tee(..), toFold)
18-- >>> import Streamly.Internal.Data.Fold as Fold
18-- >>> import Streamly.Internal.Data.Fold as Fold
20−-- >>> avg = (/) <$> (mkT Fold.sum) <*> (mkT $ fmap fromIntegral Fold.length)
20+-- >>> avg = (/) <$> (Tee Fold.sum) <*> (Tee $ fmap fromIntegral Fold.length)
21-- >>> Stream.fold (toFold avg) $ Stream.fromList [1.0..100.0]
21-- >>> Stream.fold (toFold avg) $ Stream.fromList [1.0..100.0]
@@ -26,20 +26,18 @@
26-- instances of the output types:
26-- instances of the output types:
28-- >>> import Data.Monoid (Sum(..))
28-- >>> import Data.Monoid (Sum(..))
29−-- >>> t = mkT Fold.head <> mkT Fold.last
29+-- >>> t = Tee Fold.head <> Tee Fold.last
30-- >>> Stream.fold (toFold t) (fmap Sum $ Stream.enumerateFromTo 1.0 100.0)
30-- >>> Stream.fold (toFold t) (fmap Sum $ Stream.enumerateFromTo 1.0 100.0)
31-- Just (Sum {getSum = 101.0})
31-- Just (Sum {getSum = 101.0})
33-- The 'Num', 'Floating', and 'Fractional' instances work in the same way.
33-- The 'Num', 'Floating', and 'Fractional' instances work in the same way.
35module Streamly.Internal.Data.Fold.Tee
35module Streamly.Internal.Data.Fold.Tee
36− ( Tee(..)
36+ ( Tee(..)
41import Control.Applicative (liftA2)
40import Control.Applicative (liftA2)
42−import Data.Coerce (coerce)
43#if __GLASGOW_HASKELL__ < 808
41#if __GLASGOW_HASKELL__ < 808
44import Data.Semigroup (Semigroup(..))
42import Data.Semigroup (Semigroup(..))
@@ -56,24 +54,16 @@ newtype Tee m a b =
56 Tee { toFold :: Fold m a b }
54 Tee { toFold :: Fold m a b }
57 deriving (Functor)
55 deriving (Functor)
59−-- | Make a 'Tee' from a 'Fold'.
63−mkT :: Fold m a b -> Tee m a b
67-- | '<*>' distributes the input to both the argument 'Tee's and combines their
57-- | '<*>' distributes the input to both the argument 'Tee's and combines their
68-- outputs using function application.
58-- outputs using function application.
70instance Monad m => Applicative (Tee m a) where
60instance Monad m => Applicative (Tee m a) where
72 {-# INLINE pure #-}
62 {-# INLINE pure #-}
73− pure a = mkT (Fold.yield a)
63+ pure a = Tee (Fold.yield a)
75 {-# INLINE (<*>) #-}
65 {-# INLINE (<*>) #-}
76− (<*>) a b = mkT (Fold.teeWith ($) (toFold a) (toFold b))
66+ (<*>) a b = Tee (Fold.teeWith ($) (toFold a) (toFold b))
78-- | '<>' distributes the input to both the argument 'Tee's and combines their
68-- | '<>' distributes the input to both the argument 'Tee's and combines their
79-- outputs using the 'Semigroup' instance of the output type.
69-- outputs using the 'Semigroup' instance of the output type.