on master help sign in
Add Streamly.Internal.Data.Fold.Tee
commitbbfe463f0aa754a036439808d3b289d74f46a8bf authorAdithya Kumar <adithya@composewell.com> dateFri 22 Jan 2021, 07:22 (6 years ago) parentb9892e3
3 files changed+54
Changelog.md+2 src/Streamly/Internal/Data/Fold/Tee.hs+51 streamly.cabal+1
Changelog.md+2
@@ -84,6 +84,8 @@
8484* A bug was fixed in the conversion of MicroSecond64 and MilliSecond64
8585 (commit e5119626)
8686* Bug fix: classifySessionsBy now flushes sessions at the end and terminates.
87+* `Streamly.Internal.Data.Fold.Tee` module added for folds with tee-like
88+ applicative behaviour.
8789
8890## 0.7.2
8991
src/Streamly/Internal/Data/Fold/Tee.hsnew file+51
@@ -0,0 +1,51 @@
1+-- |
2+-- Module : Streamly.Internal.Data.Fold.Tee
3+-- Copyright : (c) 2020 Composewell Technologies
4+-- License : BSD-3-Clause
5+-- Maintainer : streamly@composewell.com
6+-- Stability : experimental
7+-- Portability : GHC
8+--
9+module Streamly.Internal.Data.Fold.Tee
10+ ( Tee(..)
11+ , fromFold
12+ , toFold
13+ )
14+where
15+
16+import Data.Coerce (coerce)
17+import Streamly.Internal.Data.Fold.Types (Fold)
18+
19+import qualified Streamly.Internal.Data.Fold.Types as Fold
20+
21+-- | The type @Tee m a b@ represents a left fold over an input stream of values
22+-- of type @a@ to a single value of type @b@ in 'Monad' @m@.
23+--
24+-- @Tee@ is a wrapper over 'Fold' that uses 'teeWith' to define the applicative
25+-- instance.
26+--
27+-- /Internal/
28+newtype Tee m a b =
29+ Tee { runTee :: Fold m a b }
30+ deriving (Functor)
31+
32+-- | Convert a 'Tee' to 'Fold'.
33+{-# INLINE toFold #-}
34+toFold :: Tee m a b -> Fold m a b
35+toFold = coerce
36+
37+-- | Convert a 'Fold' to 'Tee'.
38+{-# INLINE fromFold #-}
39+fromFold :: Fold m a b -> Tee m a b
40+fromFold = coerce
41+
42+-- | The 'Tee' resulting from '<*>' distributes its input to both the argument
43+-- 'Tee's and combines their output using function application.
44+--
45+instance Monad m => Applicative (Tee m a) where
46+
47+ {-# INLINE pure #-}
48+ pure a = fromFold (Fold.yield a)
49+
50+ {-# INLINE (<*>) #-}
51+ (<*>) a b = fromFold (Fold.teeWith ($) (toFold a) (toFold b))
streamly.cabal+1
@@ -468,6 +468,7 @@ library
468468
469469 -- streamly-core
470470 , Streamly.Internal.Data.Unfold
471+ , Streamly.Internal.Data.Fold.Tee
471472 , Streamly.Internal.Data.Fold
472473 , Streamly.Internal.Data.Sink
473474 , Streamly.Internal.Data.Parser