on fff2704 help sign in
41 lines 1.5 kB history raw
1-- |
2-- Module : Streamly.Data.Fold.Tee
3-- Copyright : (c) 2021 Composewell Technologies
4-- License : BSD-3-Clause
5-- Maintainer : streamly@composewell.com
6-- Stability : released
7-- Portability : GHC
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.Prelude as Stream
19-- >>> import qualified Streamly.Data.Fold as Fold
20--
21-- >>> avg = (/) <$> (Tee Fold.sum) <*> (Tee $ fmap fromIntegral Fold.length)
22-- >>> Stream.fold (toFold 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.head <> Tee Fold.last
31-- >>> Stream.fold (toFold 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
37 ( Tee(..)
38 )
39where
41import Streamly.Internal.Data.Fold.Tee