@@ -236,6 +236,7 @@ module Streamly.Internal.Data.Fold.Type
237 -- ** Parallel Distribution
237 -- ** Parallel Distribution
238 , GenericRunner(..)
238 , GenericRunner(..)
239 , teeWith
240 , teeWith
240 , teeWithFst
241 , teeWithFst
241 , teeWithMin
242 , teeWithMin
@@ -708,12 +709,44 @@ serialWith func (Fold stepL initialL extractL) (Fold stepR initialR extractR) =
708-- | Same as applicative '*>'. Run two folds serially one after the other
709-- | Same as applicative '*>'. Run two folds serially one after the other
709-- discarding the result of the first.
710-- discarding the result of the first.
713{-# INLINE serial_ #-}
712{-# INLINE serial_ #-}
714−serial_ :: -- Monad m =>
713+serial_ :: Monad m =>
715 Fold m x a -> Fold m x b -> Fold m x b
714 Fold m x a -> Fold m x b -> Fold m x b
716−serial_ _f1 _f2 = undefined
715+serial_ (Fold stepL initialL _) (Fold stepR initialR extractR) =
716+ Fold step initial extract
725+ Partial sl -> return $ Partial $ SeqFoldL sl
728+ return $ first (SeqFoldR (func bl)) resR
730+ step (SeqFoldL st) a = do
733+ Partial s -> return $ Partial (SeqFoldL s)
736+ return $ first (SeqFoldR (func b)) res
737+ step (SeqFoldR f st) a = do
741+ Partial s -> Partial (SeqFoldR f s)
744+ extract (SeqFoldR _ sR) = extractR sR
745+ extract (SeqFoldL _) = do
748+ Partial sR -> extractR sR
749+ Done rR -> return rR
718{-# ANN type GenericRunner Fuse #-}
751{-# ANN type GenericRunner Fuse #-}
719data GenericRunner sL sR bL bR
752data GenericRunner sL sR bL bR
@@ -721,6 +754,11 @@ data GenericRunner sL sR bL bR
721 | RunLeft !sL !bR
754 | RunLeft !sL !bR
722 | RunRight !bL !sR
755 | RunRight !bL !sR
757+{-# ANN type FstRunner Fuse #-}
758+data FstRunner sL sR b
759+ = RunFstBoth !sL !sR
724-- | @teeWith k f1 f2@ distributes its input to both @f1@ and @f2@ until both
762-- | @teeWith k f1 f2@ distributes its input to both @f1@ and @f2@ until both
725-- of them terminate and combines their output using @k@.
763-- of them terminate and combines their output using @k@.
@@ -797,42 +835,223 @@ teeWith f (Fold stepL beginL doneL) (Fold stepR beginR doneR) =
798-- | Like 'teeWith' but terminates as soon as the first fold terminates.
836-- | Like 'teeWith' but terminates as soon as the first fold terminates.
800−-- /Unimplemented/
838+-- /Pre-release/
802{-# INLINE teeWithFst #-}
840{-# INLINE teeWithFst #-}
803−teeWithFst :: (b -> c -> d) -> Fold m a b -> Fold m a c -> Fold m a d
841+teeWithFst :: Monad m => (b -> c -> d) -> Fold m a b -> Fold m a c -> Fold m a d
804−teeWithFst = undefined
842+teeWithFst f (Fold stepL beginL doneL) (Fold stepR beginR doneR) =
843+ Fold step begin done
855+ Partial sr -> RunFstBoth sl sr
856+ Done br -> RunFstLeft sl br
861+ return $ Done $ f bl br
862+ Done br -> return $ Done $ f bl br
864+ step (RunFstBoth sL sR) a = do
872+ Partial sR1 -> RunFstBoth sL1 sR1
873+ Done bR -> RunFstLeft sL1 bR
878+ return $ Done $ f bL br
879+ Done bR -> return $ Done $ f bL bR
881+ step (RunFstLeft sL bR) a = do
885+ Partial sL1 -> Partial $ RunFstLeft sL1 bR
886+ Done bL -> Done $ f bL bR
888+ done (RunFstBoth sL sR) = do
892+ done (RunFstLeft sL bR) = do
806-- | Like 'teeWith' but terminates as soon as any one of the two folds
896-- | Like 'teeWith' but terminates as soon as any one of the two folds
807-- terminates.
897-- terminates.
809−-- /Unimplemented/
899+-- /Pre-release/
811{-# INLINE teeWithMin #-}
901{-# INLINE teeWithMin #-}
812−teeWithMin :: (b -> c -> d) -> Fold m a b -> Fold m a c -> Fold m a d
902+teeWithMin :: Monad m => (b -> c -> d) -> Fold m a b -> Fold m a c -> Fold m a d
813−teeWithMin = undefined
903+teeWithMin f (Fold stepL beginL doneL) (Fold stepR beginR doneR) =
904+ Fold step begin done
914+ Partial sR1 -> return $ Partial $ Tuple' sL1 sR1
917+ return $ Done $ f bl br
923+ return $ Done $ f bl br
924+ Done br -> return $ Done $ f bl br
926+ step (Tuple' sL sR) a = do
932+ Partial sR1 -> return $ Partial $ Tuple' sL1 sR1
935+ return $ Done $ f bL bR
940+ return $ Done $ f bL br
941+ Done bR -> return $ Done $ f bL bR
943+ done (Tuple' sL sR) = do
815-- | Shortest alternative. Apply both folds in parallel but choose the result
948-- | Shortest alternative. Apply both folds in parallel but choose the result
816-- from the one which consumed least input i.e. take the shortest succeeding
949-- from the one which consumed least input i.e. take the shortest succeeding
819−-- /Unimplemented/
952+-- /Pre-release/
821{-# INLINE shortest #-}
954{-# INLINE shortest #-}
822−shortest :: -- Monad m =>
823− Fold m x a -> Fold m x a -> Fold m x a
955+shortest :: Monad m => Fold m x a -> Fold m x b -> Fold m x (Either a b)
824−shortest _f1 _f2 = undefined
956+shortest (Fold stepL beginL doneL) (Fold stepR beginR _) =
957+ Fold step begin done
968+ Partial sR -> Partial $ Tuple' sL sR
969+ Done bR -> Done $ Right bR
970+ Done bL -> Done $ Left bL
972+ step (Tuple' sL sR) a = do
979+ Partial sR1 -> Partial $ Tuple' sL1 sR1
980+ Done bR -> Done $ Right bR
981+ Done bL -> Done $ Left bL
983+ done (Tuple' sL _) = do
987+{-# ANN type LongestRunner Fuse #-}
988+data LongestRunner sL sR bL bR
989+ = LongestRunBoth !sL !sR
990+ | LongestRunLeft !sL !bR
991+ | LongestRunRight !bL !sR
826-- | Longest alternative. Apply both folds in parallel but choose the result
993-- | Longest alternative. Apply both folds in parallel but choose the result
827-- from the one which consumed more input i.e. take the longest succeeding
994-- from the one which consumed more input i.e. take the longest succeeding
830−-- /Unimplemented/
997+-- /Pre-release/
832−{-# INLINE longest #-}
833−longest :: -- Monad m =>
834− Fold m x a -> Fold m x a -> Fold m x a
999+longest :: Monad m => Fold m x a -> Fold m x b -> Fold m x (Either a b)
835−longest _f1 _f2 = undefined
1000+longest (Fold stepL beginL doneL) (Fold stepR beginR doneR) =
1001+ Fold step begin done
1013+ Partial sR -> LongestRunBoth sL sR
1014+ Done bR -> LongestRunLeft sL bR
1017+ Partial sR -> Partial $ LongestRunRight bL sR
1018+ Done bR -> Done $ Right bR
1020+ step (LongestRunBoth sL sR) a = do
1028+ Partial sR1 -> LongestRunBoth sL1 sR1
1029+ Done bR -> LongestRunLeft sL1 bR
1033+ Partial sR1 -> Partial $ LongestRunRight bL sR1
1034+ Done bR -> Done $ Right bR
1036+ step (LongestRunLeft sL bR) a = do
1040+ Partial sL1 -> Partial $ LongestRunLeft sL1 bR
1041+ Done bL -> Done $ Left bL
1043+ step (LongestRunRight bL sR) a = do
1047+ Partial sR1 -> Partial $ LongestRunRight bL sR1
1048+ Done bR -> Done $ Right bR
1050+ done (LongestRunLeft sL _) = Left <$> doneL sL
1052+ done (LongestRunRight _ sR) = Right <$> doneR sR
1054+ done (LongestRunBoth sL _) = Left <$> doneL sL
837data ConcatMapState m sa a c
1056data ConcatMapState m sa a c