on master help sign in
Add Applicative instance for Fold type
71d9582Harendra Kumar
core/src/Streamly/Internal/Data/Fold/Type.hs+16
@@ -417,6 +417,7 @@ where
417
417
418#include "inline.hs"
418#include "inline.hs"
419
419
420+import Control.Applicative (liftA2)
420import Control.Monad ((>=>))
421import Control.Monad ((>=>))
421import Data.Bifunctor (Bifunctor(..))
422import Data.Bifunctor (Bifunctor(..))
422import Data.Either (fromLeft, fromRight, isLeft, isRight)
423import Data.Either (fromLeft, fromRight, isLeft, isRight)
@@ -846,6 +847,21 @@ split_ (Fold stepL initialL _) (Fold stepR initialR extractR) =
846 Partial sR -> extractR sR
847 Partial sR -> extractR sR
847 Done rR -> return rR
848 Done rR -> return rR
848
849
850+-- | 'Applicative' form of 'splitWith'. Split the input serially over two
851+-- folds.
852+instance Monad m => Applicative (Fold m a) where
853+ {-# INLINE pure #-}
854+ pure = fromPure
855+
856+ {-# INLINE (<*>) #-}
857+ (<*>) = splitWith id
858+
859+ {-# INLINE (*>) #-}
860+ (*>) = split_
861+
862+ {-# INLINE liftA2 #-}
863+ liftA2 f x = (<*>) (fmap f x)
864+
849{-# ANN type TeeState Fuse #-}
865{-# ANN type TeeState Fuse #-}
850data TeeState sL sR bL bR
866data TeeState sL sR bL bR
851 = TeeBoth !sL !sR
867 = TeeBoth !sL !sR