on master help sign in
Refactor longest, also change of behavior
3e72e48Harendra Kumar
src/Streamly/Internal/Data/Fold/Type.hs+27−51
@@ -879,8 +879,8 @@ 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 both the folds finish at the same time or if the result is extracted
882−-- If the result is extracted before any of the folds could finish then the
883+-- before any of the folds could finish then the left one is taken.
883−-- left one is taken.
884--
884--
885-- /Pre-release/
885-- /Pre-release/
886--
886--
@@ -906,75 +906,51 @@ shortest (Fold stepL initialL extractL) (Fold stepR initialR _) =
906
906
907 extract (Tuple' sL _) = Left <$> extractL sL
907 extract (Tuple' sL _) = Left <$> extractL sL
908
908
909−{-# ANN type LongestRunner Fuse #-}
909+{-# ANN type LongestState Fuse #-}
910−data LongestRunner sL sR bL bR
910+data LongestState sL sR
911− = LongestRunBoth !sL !sR
911+ = LongestBoth !sL !sR
912− | LongestRunLeft !sL !bR
912+ | LongestLeft !sL
913− | LongestRunRight !bL !sR
913+ | LongestRight !sR
914
914
915-- | Longest alternative. Apply both folds in parallel but choose the result
915-- | Longest alternative. Apply both folds in parallel but choose the result
916-- from the one which consumed more input i.e. take the longest succeeding
916-- from the one which consumed more input i.e. take the longest succeeding
917-- fold.
917-- fold.
918--
918--
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+--
919-- /Pre-release/
922-- /Pre-release/
920--
923--
921{-# INLINE longest #-}
924{-# INLINE longest #-}
922longest :: Monad m => Fold m x a -> Fold m x b -> Fold m x (Either a b)
925longest :: 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) =
926+longest (Fold stepL initialL extractL) (Fold stepR initialR extractR) =
924− Fold step begin done
927+ Fold step initial extract
925
928
926 where
929 where
927
930
928− begin = do
931+ {-# INLINE runBoth #-}
929− resL <- beginL
932+ runBoth actionL actionR = do
930− resR <- beginR
933+ resL <- actionL
934+ resR <- actionR
931 return $
935 return $
932 case resL of
936 case resL of
933 Partial sL ->
937 Partial sL ->
934 Partial $
938 Partial $
935 case resR of
939 case resR of
936− Partial sR -> LongestRunBoth sL sR
940+ Partial sR -> LongestBoth sL sR
937− Done bR -> LongestRunLeft sL bR
941+ Done _ -> LongestLeft sL
938− Done bL ->
942+ Done bL -> bimap LongestRight (const (Left bL)) resR
939− case resR of
940− Partial sR -> Partial $ LongestRunRight bL sR
941− Done bR -> Done $ Right bR
942
943
943− step (LongestRunBoth sL sR) a = do
944+ initial = runBoth initialL initialR
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
974
945
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
976
949
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
978
954
979data ConcatMapState m sa a c
955data ConcatMapState m sa a c
980 = B !sa
956 = B !sa