on master help sign in
Refactor and reformat
commit2c4860045d236f66ef31017e266ecaf7f9c17f12 authorHarendra Kumar <harendra@composewell.com> dateWed 1 Sep 2021, 20:28 (5 years ago) parent824aa0a
3 files changed+53−57
benchmark/Streamly/Benchmark/Data/Fold.hs+13−16 src/Streamly/Internal/Data/Fold.hs+32−33 src/Streamly/Internal/Data/Fold/Type.hs+8−8
benchmark/Streamly/Benchmark/Data/Fold.hs+13−16
@@ -148,26 +148,23 @@ distribute = IP.fold (FL.distribute [FL.sum, FL.length])
148148-- Partitioning
149149-------------------------------------------------------------------------------
150150
151+{-# INLINE oddEven #-}
152+oddEven :: Int -> Either Int Int
153+oddEven x = if odd x then Left x else Right x
154+
151155{-# INLINE partition #-}
152156partition :: 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)
159158
160159{-# INLINE partitionByFstM #-}
161160partitionByFstM :: Monad m => SerialT m Int -> m (Int, Int)
162−partitionByFstM = do
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)
161+partitionByFstM =
162+ IP.fold (FL.partitionByFstM (return . oddEven) FL.sum FL.length)
165163
166164{-# INLINE partitionByMinM #-}
167165partitionByMinM :: Monad m => SerialT m Int -> m (Int, Int)
168−partitionByMinM = do
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)
166+partitionByMinM =
167+ IP.fold (FL.partitionByMinM (return . oddEven) FL.sum FL.length)
171168
172169{-# INLINE demuxWith #-}
173170demuxWith ::
@@ -203,13 +200,13 @@ unzip = IP.fold $ FL.lmap (\a -> (a, a)) (FL.unzip FL.sum FL.length)
203200{-# INLINE unzipWithFstM #-}
204201unzipWithFstM :: Monad m => SerialT m Int -> m (Int, Int)
205202unzipWithFstM = do
206− let f = \a -> return (a+1, a)
203+ let f = \a -> return (a + 1, a)
207204 IP.fold (FL.unzipWithFstM f FL.sum FL.length)
208205
209206{-# INLINE unzipWithMinM #-}
210207unzipWithMinM :: Monad m => SerialT m Int -> m (Int, Int)
211208unzipWithMinM = do
212− let f = \a -> return (a+1, a)
209+ let f = \a -> return (a + 1, a)
213210 IP.fold (FL.unzipWithMinM f FL.sum FL.length)
214211
215212-------------------------------------------------------------------------------
@@ -323,8 +320,8 @@ o_1_space_serial_composition value =
323320 , benchIOSink value "teeWithMin (sum, length)" teeWithMin
324321 , benchIOSink value "distribute [sum, length]" distribute
325322 , benchIOSink value "partition (sum, length)" partition
326− , benchIOSink value "partitionByFstM (length, length)" partitionByFstM
327− , benchIOSink value "partitionByMinM (length, length)" partitionByMinM
323+ , benchIOSink value "partitionByFstM (sum, length)" partitionByFstM
324+ , benchIOSink value "partitionByMinM (sum, length)" partitionByMinM
328325 , benchIOSink value "unzip (sum, length)" unzip
329326 , benchIOSink value "unzipWithFstM (sum, length)" unzipWithFstM
330327 , 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) =
13251325 Right c -> do
13261326 res <- stepR sR c
13271327 case res of
1328− Partial sres -> return $ Partial $ Tuple' sL sres
1329− Done bR -> do
1330− bL <- doneL sL
1331− return $ Done (bL, bR)
1328+ Partial sres -> return $ Partial $ Tuple' sL sres
1329+ Done bR -> do
1330+ bL <- doneL sL
1331+ return $ Done (bL, bR)
13321332
13331333 done (Tuple' sL sR) = (,) <$> doneL sL <*> doneR sR
13341334
@@ -1729,16 +1729,16 @@ unzipWithFstM f (Fold stepL beginL doneL) (Fold stepR beginR doneR) =
17291729 resL <- beginL
17301730 resR <- beginR
17311731 case resL of
1732− Partial sL -> return $ Partial $
1733− case resR of
1734− Partial sR -> RunFstBoth sL sR
1735− Done bR -> RunFstLeft sL bR
1736− Done bL -> do
1737− case resR of
1738− Partial sR -> do
1739− bR <- doneR sR
1740− return $ Done (bL, bR)
1741− Done bR -> return $ Done (bL, bR)
1732+ Partial sL -> return $ Partial $
1733+ case resR of
1734+ Partial sR -> RunFstBoth sL sR
1735+ Done bR -> RunFstLeft sL bR
1736+ Done bL -> do
1737+ case resR of
1738+ Partial sR -> do
1739+ bR <- doneR sR
1740+ return $ Done (bL, bR)
1741+ Done bR -> return $ Done (bL, bR)
17421742
17431743 step (RunFstBoth sL sR) a = do
17441744 (b, c) <- f a
@@ -1747,8 +1747,8 @@ unzipWithFstM f (Fold stepL beginL doneL) (Fold stepR beginR doneR) =
17471747 case resL of
17481748 Partial sresL -> return $ Partial $
17491749 case resR of
1750− Partial sresR -> RunFstBoth sresL sresR
1751− Done bresR -> RunFstLeft sresL bresR
1750+ Partial sresR -> RunFstBoth sresL sresR
1751+ Done bresR -> RunFstLeft sresL bresR
17521752 Done bL -> do
17531753 case resR of
17541754 Partial sresR -> do
@@ -1782,18 +1782,18 @@ unzipWithMinM f (Fold stepL beginL doneL) (Fold stepR beginR doneR) =
17821782 resL <- beginL
17831783 resR <- beginR
17841784 case resL of
1785− Partial sL ->
1786− case resR of
1787− Partial sR -> return $ Partial $ Tuple' sL sR
1788− Done bR -> do
1789− bL <- doneL sL
1790− return $ Done (bL, bR)
1791− Done bL -> do
1792− case resR of
1793− Partial sR -> do
1794− bR <- doneR sR
1795− return $ Done (bL, bR)
1796− Done bR -> return $ Done (bL, bR)
1785+ Partial sL ->
1786+ case resR of
1787+ Partial sR -> return $ Partial $ Tuple' sL sR
1788+ Done bR -> do
1789+ bL <- doneL sL
1790+ return $ Done (bL, bR)
1791+ Done bL -> do
1792+ case resR of
1793+ Partial sR -> do
1794+ bR <- doneR sR
1795+ return $ Done (bL, bR)
1796+ Done bR -> return $ Done (bL, bR)
17971797
17981798 step (Tuple' sL sR) a = do
17991799 (b, c) <- f a
@@ -1802,10 +1802,10 @@ unzipWithMinM f (Fold stepL beginL doneL) (Fold stepR beginR doneR) =
18021802 case resL of
18031803 Partial sresL ->
18041804 case resR of
1805− Partial sresR -> return $ Partial $ Tuple' sresL sresR
1806− Done bR -> do
1807− bL <- doneL sL
1808− return $ Done (bL, bR)
1805+ Partial sresR -> return $ Partial $ Tuple' sresL sresR
1806+ Done bR -> do
1807+ bL <- doneL sL
1808+ return $ Done (bL, bR)
18091809 Done bL -> do
18101810 case resR of
18111811 Partial sresR -> do
@@ -1815,7 +1815,6 @@ unzipWithMinM f (Fold stepL beginL doneL) (Fold stepR beginR doneR) =
18151815
18161816 done (Tuple' sL sR) = (,) <$> doneL sL <*> doneR sR
18171817
1818−
18191818-- | Split elements in the input stream into two parts using a pure splitter
18201819-- function, direct each part to a different fold and zip the results.
18211820--
src/Streamly/Internal/Data/Fold/Type.hs+8−8
@@ -1009,15 +1009,15 @@ longest (Fold stepL beginL doneL) (Fold stepR beginR doneR) =
10091009 resR <- beginR
10101010 return $
10111011 case resL of
1012− Partial sL ->
1013− Partial $
1014− case resR of
1015− Partial sR -> LongestRunBoth sL sR
1016− Done bR -> LongestRunLeft sL bR
1017− Done bL ->
1012+ Partial sL ->
1013+ Partial $
10181014 case resR of
1019− Partial sR -> Partial $ LongestRunRight bL sR
1020− Done bR -> Done $ Right bR
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
1020+ Done bR -> Done $ Right bR
10211021
10221022 step (LongestRunBoth sL sR) a = do
10231023 resL <- stepL sL a