Copyright | (c) 2021 Composewell Technologies |
---|---|
License | BSD-3-Clause |
Maintainer | streamly@composewell.com |
Stability | released |
Portability | GHC |
Safe Haskell | Safe-Inferred |
Language | Haskell2010 |
The Tee
type is a newtype wrapper over the Fold
type providing
distributive Applicative
, Semigroup
, Monoid
, Num
, Floating
and
Fractional
instances. The input received by the composed Tee
is
replicated and distributed to both the constituent Tees.
For example, to compute the average of numbers in a stream without going through the stream twice:
>>>
import Streamly.Data.Fold.Tee (Tee(..))
>>>
import qualified Streamly.Prelude as Stream
>>>
import qualified Streamly.Data.Fold as Fold
>>>
avg = (/) <$> (Tee Fold.sum) <*> (Tee $ fmap fromIntegral Fold.length)
>>>
Stream.fold (toFold avg) $ Stream.fromList [1.0..100.0]
50.5
Similarly, the Semigroup
and Monoid
instances of Tee
distribute the
input to both the folds and combine the outputs using Monoid or Semigroup
instances of the output types:
>>>
import Data.Monoid (Sum(..))
>>>
t = Tee Fold.head <> Tee Fold.last
>>>
Stream.fold (toFold t) (fmap Sum $ Stream.enumerateFromTo 1.0 100.0)
Just (Sum {getSum = 101.0})
The Num
, Floating
, and Fractional
instances work in the same way.
Documentation
Tee
is a newtype wrapper over the Fold
type providing distributing
Applicative
, Semigroup
, Monoid
, Num
, Floating
and Fractional
instances.
Since: 0.8.0
Instances
Monad m => Applicative (Tee m a) Source # |
|
Functor m => Functor (Tee m a) Source # | |
(Semigroup b, Monoid b, Monad m) => Monoid (Tee m a b) Source # |
|
(Semigroup b, Monad m) => Semigroup (Tee m a b) Source # |
|
(Monad m, Floating b) => Floating (Tee m a b) Source # | Binary |
Defined in Streamly.Internal.Data.Fold.Tee exp :: Tee m a b -> Tee m a b # log :: Tee m a b -> Tee m a b # sqrt :: Tee m a b -> Tee m a b # (**) :: Tee m a b -> Tee m a b -> Tee m a b # logBase :: Tee m a b -> Tee m a b -> Tee m a b # sin :: Tee m a b -> Tee m a b # cos :: Tee m a b -> Tee m a b # tan :: Tee m a b -> Tee m a b # asin :: Tee m a b -> Tee m a b # acos :: Tee m a b -> Tee m a b # atan :: Tee m a b -> Tee m a b # sinh :: Tee m a b -> Tee m a b # cosh :: Tee m a b -> Tee m a b # tanh :: Tee m a b -> Tee m a b # asinh :: Tee m a b -> Tee m a b # acosh :: Tee m a b -> Tee m a b # atanh :: Tee m a b -> Tee m a b # log1p :: Tee m a b -> Tee m a b # expm1 :: Tee m a b -> Tee m a b # | |
(Monad m, Num b) => Num (Tee m a b) Source # | Binary |
Defined in Streamly.Internal.Data.Fold.Tee | |
(Monad m, Fractional b) => Fractional (Tee m a b) Source # | Binary |