on master help sign in
Add Applicative instance for Fold type
commit71d9582a4373441889caac6ad6c0588cdb6521be authorHarendra Kumar <harendra@composewell.com> dateTue 8 Nov 2022, 20:16 (4 years ago) parent5a9eab8
1 file changed+16
core/src/Streamly/Internal/Data/Fold/Type.hs+16
core/src/Streamly/Internal/Data/Fold/Type.hs+16
@@ -417,6 +417,7 @@ where
417417
418418#include "inline.hs"
419419
420+import Control.Applicative (liftA2)
420421import Control.Monad ((>=>))
421422import Data.Bifunctor (Bifunctor(..))
422423import Data.Either (fromLeft, fromRight, isLeft, isRight)
@@ -846,6 +847,21 @@ split_ (Fold stepL initialL _) (Fold stepR initialR extractR) =
846847 Partial sR -> extractR sR
847848 Done rR -> return rR
848849
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+
849865{-# ANN type TeeState Fuse #-}
850866data TeeState sL sR bL bR
851867 = TeeBoth !sL !sR