on master help sign in
Refactor longest, also change of behavior

Now it returns the left fold result in case both the folds finish at the same time.

commit3e72e48d939a785565d0e9fdca19d395d9965f2a authorHarendra Kumar <harendra@composewell.com> dateThu 2 Sep 2021, 12:37 (5 years ago) parentd2222d2
1 file changed+27−51
src/Streamly/Internal/Data/Fold/Type.hs+27−51
src/Streamly/Internal/Data/Fold/Type.hs+27−51
@@ -879,8 +879,8 @@ teeWithMin f (Fold stepL initialL extractL) (Fold stepR initialR extractR) =
879879-- from the one which consumed least input i.e. take the shortest succeeding
880880-- fold.
881881--
882−-- If the result is extracted before any of the folds could finish then the
883−-- left one is taken.
882+-- If both the folds finish at the same time or if the result is extracted
883+-- before any of the folds could finish then the left one is taken.
884884--
885885-- /Pre-release/
886886--
@@ -906,75 +906,51 @@ shortest (Fold stepL initialL extractL) (Fold stepR initialR _) =
906906
907907 extract (Tuple' sL _) = Left <$> extractL sL
908908
909−{-# ANN type LongestRunner Fuse #-}
910−data LongestRunner sL sR bL bR
911− = LongestRunBoth !sL !sR
912− | LongestRunLeft !sL !bR
913− | LongestRunRight !bL !sR
909+{-# ANN type LongestState Fuse #-}
910+data LongestState sL sR
911+ = LongestBoth !sL !sR
912+ | LongestLeft !sL
913+ | LongestRight !sR
914914
915915-- | Longest alternative. Apply both folds in parallel but choose the result
916916-- from the one which consumed more input i.e. take the longest succeeding
917917-- fold.
918918--
919+-- If both the folds finish at the same time or if the result is extracted
920+-- before any of the folds could finish then the left one is taken.
921+--
919922-- /Pre-release/
920923--
921924{-# INLINE longest #-}
922925longest :: Monad m => Fold m x a -> Fold m x b -> Fold m x (Either a b)
923−longest (Fold stepL beginL doneL) (Fold stepR beginR doneR) =
924− Fold step begin done
926+longest (Fold stepL initialL extractL) (Fold stepR initialR extractR) =
927+ Fold step initial extract
925928
926929 where
927930
928− begin = do
929− resL <- beginL
930− resR <- beginR
931+ {-# INLINE runBoth #-}
932+ runBoth actionL actionR = do
933+ resL <- actionL
934+ resR <- actionR
931935 return $
932936 case resL of
933937 Partial sL ->
934938 Partial $
935939 case resR of
936− Partial sR -> LongestRunBoth sL sR
937− Done bR -> LongestRunLeft sL bR
938− Done bL ->
939− case resR of
940− Partial sR -> Partial $ LongestRunRight bL sR
941− Done bR -> Done $ Right bR
940+ Partial sR -> LongestBoth sL sR
941+ Done _ -> LongestLeft sL
942+ Done bL -> bimap LongestRight (const (Left bL)) resR
942943
943− step (LongestRunBoth sL sR) a = do
944− resL <- stepL sL a
945− resR <- stepR sR a
946− case resL of
947− Partial sL1 ->
948− return
949− $ Partial
950− $ case resR of
951− Partial sR1 -> LongestRunBoth sL1 sR1
952− Done bR -> LongestRunLeft sL1 bR
953− Done bL ->
954− return
955− $ case resR of
956− Partial sR1 -> Partial $ LongestRunRight bL sR1
957− Done bR -> Done $ Right bR
958−
959− step (LongestRunLeft sL bR) a = do
960− resL <- stepL sL a
961− return $
962− case resL of
963− Partial sL1 -> Partial $ LongestRunLeft sL1 bR
964− Done bL -> Done $ Left bL
965−
966− step (LongestRunRight bL sR) a = do
967− resR <- stepR sR a
968− return
969− $ case resR of
970− Partial sR1 -> Partial $ LongestRunRight bL sR1
971− Done bR -> Done $ Right bR
972−
973− done (LongestRunLeft sL _) = Left <$> doneL sL
944+ initial = runBoth initialL initialR
974945
975− done (LongestRunRight _ sR) = Right <$> doneR sR
946+ step (LongestBoth sL sR) a = runBoth (stepL sL a) (stepR sR a)
947+ step (LongestLeft sL) a = bimap LongestLeft Left <$> stepL sL a
948+ step (LongestRight sR) a = bimap LongestRight Right <$> stepR sR a
976949
977− done (LongestRunBoth sL _) = Left <$> doneL sL
950+ left sL = Left <$> extractL sL
951+ extract (LongestLeft sL) = left sL
952+ extract (LongestRight sR) = Right <$> extractR sR
953+ extract (LongestBoth sL _) = left sL
978954
979955data ConcatMapState m sa a c
980956 = B !sa