on master help sign in
Move instances based on Applicative from Fold to Tee
commitd79dd95a13da0df200b2abff4949262ebe0a4027 authorAdithya Kumar <adithya@composewell.com> dateFri 5 Mar 2021, 02:48 (6 years ago) parent9231d2d
3 files changed+108−4
src/Streamly/Data/Fold.hs+1−1 src/Streamly/Internal/Data/Fold/Tee.hs+107 src/Streamly/Internal/Data/Fold/Types.hs−3
src/Streamly/Data/Fold.hs+1−1
@@ -208,7 +208,7 @@ module Streamly.Data.Fold
208208 -- Semigroup instances of the output types:
209209 --
210210 -- >>> import Data.Monoid (Sum(..))
211− -- >>> Stream.fold (Fold.head <> Fold.last) (fmap Sum $ Stream.enumerateFromTo 1.0 100.0)
211+ -- >>> Stream.fold (Fold.teeWith (<>) Fold.head Fold.last) (fmap Sum $ Stream.enumerateFromTo 1.0 100.0)
212212 -- Just (Sum {getSum = 101.0})
213213 --
214214 -- The 'Num', 'Floating', and 'Fractional' instances work in the same way.
src/Streamly/Internal/Data/Fold/Tee.hs+107
@@ -13,7 +13,11 @@ module Streamly.Internal.Data.Fold.Tee
1313 )
1414where
1515
16+import Control.Applicative (liftA2)
1617import Data.Coerce (coerce)
18+#if __GLASGOW_HASKELL__ < 808
19+import Data.Semigroup (Semigroup(..))
20+#endif
1721import Streamly.Internal.Data.Fold.Types (Fold)
1822
1923import qualified Streamly.Internal.Data.Fold.Types as Fold
@@ -49,3 +53,106 @@ instance Monad m => Applicative (Tee m a) where
4953
5054 {-# INLINE (<*>) #-}
5155 (<*>) a b = fromFold (Fold.teeWith ($) (toFold a) (toFold b))
56+
57+-- | Combines the outputs (the type @b@) using their 'Semigroup' instances.
58+instance (Semigroup b, Monad m) => Semigroup (Tee m a b) where
59+ {-# INLINE (<>) #-}
60+ (<>) = liftA2 (<>)
61+
62+-- | Combines the outputs (the type @b@) using their 'Monoid' instances.
63+instance (Semigroup b, Monoid b, Monad m) => Monoid (Tee m a b) where
64+ {-# INLINE mempty #-}
65+ mempty = pure mempty
66+
67+ {-# INLINE mappend #-}
68+ mappend = (<>)
69+
70+-- | Combines the outputs (type @b@) using their 'Num' instances.
71+instance (Monad m, Num b) => Num (Tee m a b) where
72+ {-# INLINE fromInteger #-}
73+ fromInteger = pure . fromInteger
74+
75+ {-# INLINE negate #-}
76+ negate = fmap negate
77+
78+ {-# INLINE abs #-}
79+ abs = fmap abs
80+
81+ {-# INLINE signum #-}
82+ signum = fmap signum
83+
84+ {-# INLINE (+) #-}
85+ (+) = liftA2 (+)
86+
87+ {-# INLINE (*) #-}
88+ (*) = liftA2 (*)
89+
90+ {-# INLINE (-) #-}
91+ (-) = liftA2 (-)
92+
93+-- | Combines the outputs (type @b@) using their 'Fractional' instances.
94+instance (Monad m, Fractional b) => Fractional (Tee m a b) where
95+ {-# INLINE fromRational #-}
96+ fromRational = pure . fromRational
97+
98+ {-# INLINE recip #-}
99+ recip = fmap recip
100+
101+ {-# INLINE (/) #-}
102+ (/) = liftA2 (/)
103+
104+-- | Combines the outputs using their 'Floating' instances.
105+instance (Monad m, Floating b) => Floating (Tee m a b) where
106+ {-# INLINE pi #-}
107+ pi = pure pi
108+
109+ {-# INLINE exp #-}
110+ exp = fmap exp
111+
112+ {-# INLINE sqrt #-}
113+ sqrt = fmap sqrt
114+
115+ {-# INLINE log #-}
116+ log = fmap log
117+
118+ {-# INLINE sin #-}
119+ sin = fmap sin
120+
121+ {-# INLINE tan #-}
122+ tan = fmap tan
123+
124+ {-# INLINE cos #-}
125+ cos = fmap cos
126+
127+ {-# INLINE asin #-}
128+ asin = fmap asin
129+
130+ {-# INLINE atan #-}
131+ atan = fmap atan
132+
133+ {-# INLINE acos #-}
134+ acos = fmap acos
135+
136+ {-# INLINE sinh #-}
137+ sinh = fmap sinh
138+
139+ {-# INLINE tanh #-}
140+ tanh = fmap tanh
141+
142+ {-# INLINE cosh #-}
143+ cosh = fmap cosh
144+
145+ {-# INLINE asinh #-}
146+ asinh = fmap asinh
147+
148+ {-# INLINE atanh #-}
149+ atanh = fmap atanh
150+
151+ {-# INLINE acosh #-}
152+ acosh = fmap acosh
153+
154+ {-# INLINE (**) #-}
155+ (**) = liftA2 (**)
156+
157+ {-# INLINE logBase #-}
158+ logBase = liftA2 logBase
src/Streamly/Internal/Data/Fold/Types.hs−3
@@ -238,9 +238,6 @@ import Control.Monad.IO.Class (MonadIO(..))
238238import Control.Monad.Trans.Control (control)
239239import Data.Bifunctor (Bifunctor(..))
240240import Data.Maybe (isJust, fromJust)
241−#if __GLASGOW_HASKELL__ < 808
242−import Data.Semigroup (Semigroup(..))
243−#endif
244241import Fusion.Plugin.Types (Fuse(..))
245242import Streamly.Internal.Data.Tuple.Strict (Tuple'(..), Tuple3'(..))
246243import Streamly.Internal.Data.SVar (MonadAsync)