on master help sign in
Refactor shortest
d2222d2Harendra Kumar
src/Streamly/Internal/Data/Fold/Type.hs+14−22
@@ -879,40 +879,32 @@ teeWithMin f (Fold stepL initialL extractL) (Fold stepR initialR extractR) =
879-- from the one which consumed least input i.e. take the shortest succeeding
879-- from the one which consumed least input i.e. take the shortest succeeding
880-- fold.
880-- fold.
881--
881--
882+-- If the result is extracted before any of the folds could finish then the
883+-- left one is taken.
884+--
882-- /Pre-release/
885-- /Pre-release/
883--
886--
884{-# INLINE shortest #-}
887{-# INLINE shortest #-}
885shortest :: Monad m => Fold m x a -> Fold m x b -> Fold m x (Either a b)
888shortest :: Monad m => Fold m x a -> Fold m x b -> Fold m x (Either a b)
886−shortest (Fold stepL beginL doneL) (Fold stepR beginR _) =
889+shortest (Fold stepL initialL extractL) (Fold stepR initialR _) =
887− Fold step begin done
890+ Fold step initial extract
888
891
889 where
892 where
890
893
891− begin = do
894+ {-# INLINE runBoth #-}
892− resL <- beginL
895+ runBoth actionL actionR = do
893− resR <- beginR
896+ resL <- actionL
897+ resR <- actionR
894 return $
898 return $
895 case resL of
899 case resL of
896− Partial sL ->
900+ Partial sL -> bimap (Tuple' sL) Right resR
897− case resR of
898− Partial sR -> Partial $ Tuple' sL sR
899− Done bR -> Done $ Right bR
900 Done bL -> Done $ Left bL
901 Done bL -> Done $ Left bL
901
902
903+ initial = runBoth initialL initialR
904+
902− step (Tuple' sL sR) a = do
905+ step (Tuple' sL sR) a = runBoth (stepL sL a) (stepR sR a)
903− resL <- stepL sL a
904− resR <- stepR sR a
905− return $
906− case resL of
907− Partial sL1 ->
908− case resR of
909− Partial sR1 -> Partial $ Tuple' sL1 sR1
910− Done bR -> Done $ Right bR
911− Done bL -> Done $ Left bL
912
906
913− done (Tuple' sL _) = do
907+ extract (Tuple' sL _) = Left <$> extractL sL
914− x <- doneL sL
915− return $ Left x
916
908
917{-# ANN type LongestRunner Fuse #-}
909{-# ANN type LongestRunner Fuse #-}
918data LongestRunner sL sR bL bR
910data LongestRunner sL sR bL bR