on master help sign in
Refactor and reformat
2c48600Harendra Kumar
benchmark/Streamly/Benchmark/Data/Fold.hs+13−16
@@ -148,26 +148,23 @@ distribute = IP.fold (FL.distribute [FL.sum, FL.length])
148-- Partitioning
148-- Partitioning
149-------------------------------------------------------------------------------
149-------------------------------------------------------------------------------
150
150
151+{-# INLINE oddEven #-}
152+oddEven :: Int -> Either Int Int
153+oddEven x = if odd x then Left x else Right x
154+
151{-# INLINE partition #-}
155{-# INLINE partition #-}
152partition :: Monad m => SerialT m Int -> m (Int, Int)
156partition :: Monad m => SerialT m Int -> m (Int, Int)
153−partition =
154− let f a =
155− if odd a
156− then Left a
157− else Right a
158− in IP.fold $ FL.lmap f (FL.partition FL.sum FL.length)
157+partition = IP.fold $ FL.lmap oddEven (FL.partition FL.sum FL.length)
159
158
160{-# INLINE partitionByFstM #-}
159{-# INLINE partitionByFstM #-}
161partitionByFstM :: Monad m => SerialT m Int -> m (Int, Int)
160partitionByFstM :: Monad m => SerialT m Int -> m (Int, Int)
162−partitionByFstM = do
161+partitionByFstM =
163− let f x = if odd x then return (Left x) else return (Right x)
164− IP.fold (FL.partitionByFstM f FL.length FL.length)
162+ IP.fold (FL.partitionByFstM (return . oddEven) FL.sum FL.length)
165
163
166{-# INLINE partitionByMinM #-}
164{-# INLINE partitionByMinM #-}
167partitionByMinM :: Monad m => SerialT m Int -> m (Int, Int)
165partitionByMinM :: Monad m => SerialT m Int -> m (Int, Int)
168−partitionByMinM = do
166+partitionByMinM =
169− let f x = if odd x then return (Left x) else return (Right x)
170− IP.fold (FL.partitionByMinM f FL.length FL.length)
167+ IP.fold (FL.partitionByMinM (return . oddEven) FL.sum FL.length)
171
168
172{-# INLINE demuxWith #-}
169{-# INLINE demuxWith #-}
173demuxWith ::
170demuxWith ::
@@ -203,13 +200,13 @@ unzip = IP.fold $ FL.lmap (\a -> (a, a)) (FL.unzip FL.sum FL.length)
203{-# INLINE unzipWithFstM #-}
200{-# INLINE unzipWithFstM #-}
204unzipWithFstM :: Monad m => SerialT m Int -> m (Int, Int)
201unzipWithFstM :: Monad m => SerialT m Int -> m (Int, Int)
205unzipWithFstM = do
202unzipWithFstM = do
206− let f = \a -> return (a+1, a)
203+ let f = \a -> return (a + 1, a)
207 IP.fold (FL.unzipWithFstM f FL.sum FL.length)
204 IP.fold (FL.unzipWithFstM f FL.sum FL.length)
208
205
209{-# INLINE unzipWithMinM #-}
206{-# INLINE unzipWithMinM #-}
210unzipWithMinM :: Monad m => SerialT m Int -> m (Int, Int)
207unzipWithMinM :: Monad m => SerialT m Int -> m (Int, Int)
211unzipWithMinM = do
208unzipWithMinM = do
212− let f = \a -> return (a+1, a)
209+ let f = \a -> return (a + 1, a)
213 IP.fold (FL.unzipWithMinM f FL.sum FL.length)
210 IP.fold (FL.unzipWithMinM f FL.sum FL.length)
214
211
215-------------------------------------------------------------------------------
212-------------------------------------------------------------------------------
@@ -323,8 +320,8 @@ o_1_space_serial_composition value =
323 , benchIOSink value "teeWithMin (sum, length)" teeWithMin
320 , benchIOSink value "teeWithMin (sum, length)" teeWithMin
324 , benchIOSink value "distribute [sum, length]" distribute
321 , benchIOSink value "distribute [sum, length]" distribute
325 , benchIOSink value "partition (sum, length)" partition
322 , benchIOSink value "partition (sum, length)" partition
326− , benchIOSink value "partitionByFstM (length, length)" partitionByFstM
323+ , benchIOSink value "partitionByFstM (sum, length)" partitionByFstM
327− , benchIOSink value "partitionByMinM (length, length)" partitionByMinM
324+ , benchIOSink value "partitionByMinM (sum, length)" partitionByMinM
328 , benchIOSink value "unzip (sum, length)" unzip
325 , benchIOSink value "unzip (sum, length)" unzip
329 , benchIOSink value "unzipWithFstM (sum, length)" unzipWithFstM
326 , benchIOSink value "unzipWithFstM (sum, length)" unzipWithFstM
330 , benchIOSink value "unzipWithMinM (sum, length)" unzipWithMinM
327 , benchIOSink value "unzipWithMinM (sum, length)" unzipWithMinM
src/Streamly/Internal/Data/Fold.hs+32−33
@@ -1325,10 +1325,10 @@ partitionByMinM f (Fold stepL beginL doneL) (Fold stepR beginR doneR) =
1325 Right c -> do
1325 Right c -> do
1326 res <- stepR sR c
1326 res <- stepR sR c
1327 case res of
1327 case res of
1328− Partial sres -> return $ Partial $ Tuple' sL sres
1328+ Partial sres -> return $ Partial $ Tuple' sL sres
1329− Done bR -> do
1329+ Done bR -> do
1330− bL <- doneL sL
1330+ bL <- doneL sL
1331− return $ Done (bL, bR)
1331+ return $ Done (bL, bR)
1332
1332
1333 done (Tuple' sL sR) = (,) <$> doneL sL <*> doneR sR
1333 done (Tuple' sL sR) = (,) <$> doneL sL <*> doneR sR
1334
1334
@@ -1729,16 +1729,16 @@ unzipWithFstM f (Fold stepL beginL doneL) (Fold stepR beginR doneR) =
1729 resL <- beginL
1729 resL <- beginL
1730 resR <- beginR
1730 resR <- beginR
1731 case resL of
1731 case resL of
1732− Partial sL -> return $ Partial $
1732+ Partial sL -> return $ Partial $
1733− case resR of
1733+ case resR of
1734− Partial sR -> RunFstBoth sL sR
1734+ Partial sR -> RunFstBoth sL sR
1735− Done bR -> RunFstLeft sL bR
1735+ Done bR -> RunFstLeft sL bR
1736− Done bL -> do
1736+ Done bL -> do
1737− case resR of
1737+ case resR of
1738− Partial sR -> do
1738+ Partial sR -> do
1739− bR <- doneR sR
1739+ bR <- doneR sR
1740− return $ Done (bL, bR)
1740+ return $ Done (bL, bR)
1741− Done bR -> return $ Done (bL, bR)
1741+ Done bR -> return $ Done (bL, bR)
1742
1742
1743 step (RunFstBoth sL sR) a = do
1743 step (RunFstBoth sL sR) a = do
1744 (b, c) <- f a
1744 (b, c) <- f a
@@ -1747,8 +1747,8 @@ unzipWithFstM f (Fold stepL beginL doneL) (Fold stepR beginR doneR) =
1747 case resL of
1747 case resL of
1748 Partial sresL -> return $ Partial $
1748 Partial sresL -> return $ Partial $
1749 case resR of
1749 case resR of
1750− Partial sresR -> RunFstBoth sresL sresR
1750+ Partial sresR -> RunFstBoth sresL sresR
1751− Done bresR -> RunFstLeft sresL bresR
1751+ Done bresR -> RunFstLeft sresL bresR
1752 Done bL -> do
1752 Done bL -> do
1753 case resR of
1753 case resR of
1754 Partial sresR -> do
1754 Partial sresR -> do
@@ -1782,18 +1782,18 @@ unzipWithMinM f (Fold stepL beginL doneL) (Fold stepR beginR doneR) =
1782 resL <- beginL
1782 resL <- beginL
1783 resR <- beginR
1783 resR <- beginR
1784 case resL of
1784 case resL of
1785− Partial sL ->
1785+ Partial sL ->
1786− case resR of
1786+ case resR of
1787− Partial sR -> return $ Partial $ Tuple' sL sR
1787+ Partial sR -> return $ Partial $ Tuple' sL sR
1788− Done bR -> do
1788+ Done bR -> do
1789− bL <- doneL sL
1789+ bL <- doneL sL
1790− return $ Done (bL, bR)
1790+ return $ Done (bL, bR)
1791− Done bL -> do
1791+ Done bL -> do
1792− case resR of
1792+ case resR of
1793− Partial sR -> do
1793+ Partial sR -> do
1794− bR <- doneR sR
1794+ bR <- doneR sR
1795− return $ Done (bL, bR)
1795+ return $ Done (bL, bR)
1796− Done bR -> return $ Done (bL, bR)
1796+ Done bR -> return $ Done (bL, bR)
1797
1797
1798 step (Tuple' sL sR) a = do
1798 step (Tuple' sL sR) a = do
1799 (b, c) <- f a
1799 (b, c) <- f a
@@ -1802,10 +1802,10 @@ unzipWithMinM f (Fold stepL beginL doneL) (Fold stepR beginR doneR) =
1802 case resL of
1802 case resL of
1803 Partial sresL ->
1803 Partial sresL ->
1804 case resR of
1804 case resR of
1805− Partial sresR -> return $ Partial $ Tuple' sresL sresR
1805+ Partial sresR -> return $ Partial $ Tuple' sresL sresR
1806− Done bR -> do
1806+ Done bR -> do
1807− bL <- doneL sL
1807+ bL <- doneL sL
1808− return $ Done (bL, bR)
1808+ return $ Done (bL, bR)
1809 Done bL -> do
1809 Done bL -> do
1810 case resR of
1810 case resR of
1811 Partial sresR -> do
1811 Partial sresR -> do
@@ -1815,7 +1815,6 @@ unzipWithMinM f (Fold stepL beginL doneL) (Fold stepR beginR doneR) =
1815
1815
1816 done (Tuple' sL sR) = (,) <$> doneL sL <*> doneR sR
1816 done (Tuple' sL sR) = (,) <$> doneL sL <*> doneR sR
1817
1817
1818−
1819-- | Split elements in the input stream into two parts using a pure splitter
1818-- | Split elements in the input stream into two parts using a pure splitter
1820-- function, direct each part to a different fold and zip the results.
1819-- function, direct each part to a different fold and zip the results.
1821--
1820--
src/Streamly/Internal/Data/Fold/Type.hs+8−8
@@ -1009,15 +1009,15 @@ longest (Fold stepL beginL doneL) (Fold stepR beginR doneR) =
1009 resR <- beginR
1009 resR <- beginR
1010 return $
1010 return $
1011 case resL of
1011 case resL of
1012− Partial sL ->
1012+ Partial sL ->
1013− Partial $
1013+ Partial $
1014− case resR of
1015− Partial sR -> LongestRunBoth sL sR
1016− Done bR -> LongestRunLeft sL bR
1017− Done bL ->
1018 case resR of
1014 case resR of
1015+ Partial sR -> LongestRunBoth sL sR
1016+ Done bR -> LongestRunLeft sL bR
1017+ Done bL ->
1018+ case resR of
1019− Partial sR -> Partial $ LongestRunRight bL sR
1019+ Partial sR -> Partial $ LongestRunRight bL sR
1020− Done bR -> Done $ Right bR
1020+ Done bR -> Done $ Right bR
1021
1021
1022 step (LongestRunBoth sL sR) a = do
1022 step (LongestRunBoth sL sR) a = do
1023 resL <- stepL sL a
1023 resL <- stepL sL a