on master help sign in
Remove span, break, spanBy, and spanByRolling
commitc5b6a8bfed0dd9eb0568ea6ce6e1946284547f0d authorAdithya Kumar <adithya@composewell.com> dateMon 25 Jan 2021, 19:59 (6 years ago) parentbf396f0
2 files changed+1−312
benchmark/Streamly/Benchmark/Data/Fold.hs+1−22 src/Streamly/Internal/Data/Fold.hs−290
benchmark/Streamly/Benchmark/Data/Fold.hs+1−22
@@ -27,7 +27,7 @@ import qualified Streamly.Prelude as S
2727
2828import Gauge
2929import Streamly.Benchmark.Common
30−import Prelude hiding (all, any, take, unzip, span)
30+import Prelude hiding (all, any, take, unzip)
3131
3232-- We need a monadic bind here to make sure that the function f does not get
3333-- completely optimized out by the compiler in some cases.
@@ -162,24 +162,6 @@ classifyWith f = S.fold (FL.classifyWith f FL.sum)
162162unzip :: Monad m => SerialT m Int -> m (Int, Int)
163163unzip = IP.fold $ FL.map (\a -> (a, a)) (FL.unzip FL.sum FL.length)
164164
165−-------------------------------------------------------------------------------
166−-- Spanning
167−-------------------------------------------------------------------------------
168−
169−{-# INLINE span #-}
170−span :: Monad m => Int -> SerialT m Int -> m ((), ())
171−span bound = S.fold (FL.span (<= (bound `div` 2)) FL.drain FL.drain)
172−
173−{-# INLINE spanBy #-}
174−spanBy :: Monad m => Int -> SerialT m Int -> m ((), ())
175−spanBy bound =
176− S.fold (FL.spanBy (\_ i -> i <= (bound `div` 2)) FL.drain FL.drain)
177−
178−{-# INLINE spanByRolling #-}
179−spanByRolling :: Monad m => Int -> SerialT m Int -> m ((), ())
180−spanByRolling bound =
181− S.fold (FL.spanByRolling (\_ i -> i <= (bound `div` 2)) FL.drain FL.drain)
182−
183165-------------------------------------------------------------------------------
184166-- Benchmarks
185167-------------------------------------------------------------------------------
@@ -292,9 +274,6 @@ o_1_space_serial_composition value =
292274 $ demuxDefaultWith fn mp
293275 , benchIOSink value "demuxWith [sum, length]" $ demuxWith fn mp
294276 , benchIOSink value "classifyWith sum" $ classifyWith (fst . fn)
295− , benchIOSink value "span" $ span value
296− , benchIOSink value "spanBy" $ spanBy value
297− , benchIOSink value "spanByRolling" $ spanByRolling value
298277 ]
299278 ]
300279
src/Streamly/Internal/Data/Fold.hs−290
@@ -159,13 +159,6 @@ module Streamly.Internal.Data.Fold
159159 , splitAt -- spanN
160160 -- , splitIn -- sessionN
161161
162− -- XXX To be moved to parsers
163− , span -- spanWhile
164− , break -- breakBefore
165− -- , breakAround
166− , spanBy
167− , spanByRolling
168−
169162 -- By sequences
170163 -- , breakOnSeq
171164 -- , breakOnStream -- on a stream
@@ -1119,289 +1112,6 @@ sliceEndWith predicate (Fold fstep finitial fextract) =
11191112 Partial s1 -> Done <$> fextract s1
11201113 Done b -> return $ Done b
11211114
1122−data SpanByState a bl fl fr
1123− = SpanByLeft0 !fl
1124− | SpanByLeft !a !fl
1125− | SpanByRight !bl !fr
1126−
1127−-- | Break the input stream into two groups, the first group takes the input as
1128−-- long as the predicate applied to the first element of the stream and next
1129−-- input element holds 'True', the second group takes the rest of the input.
1130−--
1131−-- /Internal/
1132−--
1133−{-# INLINE spanBy #-}
1134−spanBy
1135− :: Monad m
1136− => (a -> a -> Bool)
1137− -> Fold m a b
1138− -> Fold m a c
1139− -> Fold m a (b, c)
1140−spanBy cmp (Fold stepL initialL extractL) (Fold stepR initialR extractR) =
1141− Fold step initial extract
1142−
1143− where
1144−
1145− initial = do
1146− resL <- initialL
1147− case resL of
1148− Partial fl -> return $ Partial $ SpanByLeft0 fl
1149− Done bl -> do
1150− resR <- initialR
1151− return
1152− $ case resR of
1153− Partial fr -> Partial $ SpanByRight bl fr
1154− Done br -> Done (bl, br)
1155−
1156− step (SpanByLeft0 fl) input = do
1157− sfl <- stepL fl input
1158− case sfl of
1159− Partial fl1 -> return $ Partial $ SpanByLeft input fl1
1160− Done bl -> do
1161− resR <- initialR
1162− return
1163− $ case resR of
1164− Partial fr -> Partial $ SpanByRight bl fr
1165− Done br -> Done (bl, br)
1166− step (SpanByLeft frst fl) input =
1167− if cmp frst input
1168− then do
1169− sfl <- stepL fl input
1170− case sfl of
1171− Partial fl1 -> return $ Partial $ SpanByLeft frst fl1
1172− Done bl -> do
1173− resR <- initialR
1174− return
1175− $ case resR of
1176− Partial fr -> Partial $ SpanByRight bl fr
1177− Done br -> Done (bl, br)
1178− else do
1179− bl <- extractL fl
1180− resR <- initialR
1181− case resR of
1182− Partial fr -> do
1183− sfr <- stepR fr input
1184− return
1185− $ case sfr of
1186− Partial fr1 -> Partial $ SpanByRight bl fr1
1187− Done br -> Done (bl, br)
1188− Done br -> return $ Done (bl, br)
1189− step (SpanByRight bl fr) input = do
1190− sfr <- stepR fr input
1191− return
1192− $ case sfr of
1193− Partial fr1 -> Partial $ SpanByRight bl fr1
1194− Done br -> Done (bl, br)
1195−
1196− extract (SpanByLeft0 fl) = do
1197− bl <- extractL fl
1198− resR <- initialR
1199− case resR of
1200− Partial fr -> (bl, ) <$> extractR fr
1201− Done br -> return (bl, br)
1202− extract (SpanByLeft _ fl) = do
1203− bl <- extractL fl
1204− resR <- initialR
1205− case resR of
1206− Partial fr -> (bl, ) <$> extractR fr
1207− Done br -> return (bl, br)
1208− extract (SpanByRight bl fr) = (bl, ) <$> extractR fr
1209−
1210−data SpanState bl fl fr
1211− = SpanLeft !fl
1212− | SpanRight !bl !fr
1213−
1214−-- | @span p f1 f2@ composes folds @f1@ and @f2@ such that @f1@ consumes the
1215−-- input as long as the predicate @p@ is 'True'. @f2@ consumes the rest of the
1216−-- input.
1217−--
1218−-- > let span_ p xs = S.fold (FL.span p FL.toList FL.toList) $ S.fromList xs
1219−--
1220−-- >>> span_ (< 1) [1,2,3]
1221−-- > ([],[1,2,3])
1222−--
1223−-- >>> span_ (< 2) [1,2,3]
1224−-- > ([1],[2,3])
1225−--
1226−-- >>> span_ (< 4) [1,2,3]
1227−-- > ([1,2,3],[])
1228−--
1229−-- /Internal/
1230−
1231−-- This can be considered as a two-fold version of 'ltakeWhile' where we take
1232−-- both the segments instead of discarding the leftover.
1233−{-# INLINE span #-}
1234−span
1235− :: Monad m
1236− => (a -> Bool)
1237− -> Fold m a b
1238− -> Fold m a c
1239− -> Fold m a (b, c)
1240−span p (Fold stepL initialL extractL) (Fold stepR initialR extractR) =
1241− Fold step initial extract
1242−
1243− where
1244−
1245− initial = do
1246− resL <- initialL
1247− case resL of
1248− Partial fl -> return $ Partial $ SpanLeft fl
1249− Done bl -> do
1250− resR <- initialR
1251− return
1252− $ case resR of
1253− Partial fr -> Partial $ SpanRight bl fr
1254− Done br -> Done (bl, br)
1255−
1256− step (SpanLeft fl) input =
1257− if p input
1258− then do
1259− sfl <- stepL fl input
1260− case sfl of
1261− Partial fl1 -> return $ Partial $ SpanLeft fl1
1262− Done bl -> do
1263− resR <- initialR
1264− return
1265− $ case resR of
1266− Partial fr -> Partial $ SpanRight bl fr
1267− Done br -> Done (bl, br)
1268− else do
1269− bl <- extractL fl
1270− resR <- initialR
1271− case resR of
1272− Partial fr -> do
1273− sfr <- stepR fr input
1274− return
1275− $ case sfr of
1276− Partial fr1 -> Partial $ SpanRight bl fr1
1277− Done br -> Done (bl, br)
1278− Done br -> return $ Done (bl, br)
1279− step (SpanRight bl fr) input = do
1280− sfr <- stepR fr input
1281− return
1282− $ case sfr of
1283− Partial fr1 -> Partial $ SpanRight bl fr1
1284− Done br -> Done (bl, br)
1285−
1286− extract (SpanLeft fl) = do
1287− bl <- extractL fl
1288− resR <- initialR
1289− case resR of
1290− Partial fr -> (bl, ) <$> extractR fr
1291− Done br -> return (bl, br)
1292− extract (SpanRight bl fr) = (bl, ) <$> extractR fr
1293−
1294−-- |
1295−-- > break p = span (not . p)
1296−--
1297−-- Break as soon as the predicate becomes 'True'. @break p f1 f2@ composes
1298−-- folds @f1@ and @f2@ such that @f1@ stops consuming input as soon as the
1299−-- predicate @p@ becomes 'True'. The rest of the input is consumed @f2@.
1300−--
1301−-- This is the binary version of 'splitBy'.
1302−--
1303−-- > let break_ p xs = S.fold (S.break p FL.toList FL.toList) $ S.fromList xs
1304−--
1305−-- >>> break_ (< 1) [3,2,1]
1306−-- > ([3,2,1],[])
1307−--
1308−-- >>> break_ (< 2) [3,2,1]
1309−-- > ([3,2],[1])
1310−--
1311−-- >>> break_ (< 4) [3,2,1]
1312−-- > ([],[3,2,1])
1313−--
1314−-- /Internal/
1315−{-# INLINE break #-}
1316−break
1317− :: Monad m
1318− => (a -> Bool)
1319− -> Fold m a b
1320− -> Fold m a c
1321− -> Fold m a (b, c)
1322−break p = span (not . p)
1323−
1324−-- | Like 'spanBy' but applies the predicate in a rolling fashion i.e.
1325−-- predicate is applied to the previous and the next input elements.
1326−--
1327−-- /Internal/
1328−{-# INLINE spanByRolling #-}
1329−spanByRolling
1330− :: Monad m
1331− => (a -> a -> Bool)
1332− -> Fold m a b
1333− -> Fold m a c
1334− -> Fold m a (b, c)
1335−spanByRolling cmp (Fold stepL initialL extractL) (Fold stepR initialR extractR) =
1336− Fold step initial extract
1337−
1338− where
1339−
1340− initial = do
1341− resL <- initialL
1342− case resL of
1343− Partial fl -> return $ Partial $ SpanByLeft0 fl
1344− Done bl -> do
1345− resR <- initialR
1346− return
1347− $ case resR of
1348− Partial fr -> Partial $ SpanByRight bl fr
1349− Done br -> Done (bl, br)
1350−
1351− step (SpanByLeft0 fl) input = do
1352− sfl <- stepL fl input
1353− case sfl of
1354− Partial fl1 -> return $ Partial $ SpanByLeft input fl1
1355− Done bl -> do
1356− resR <- initialR
1357− return
1358− $ case resR of
1359− Partial fr -> Partial $ SpanByRight bl fr
1360− Done br -> Done (bl, br)
1361− step (SpanByLeft input0 fl) input =
1362− if cmp input0 input
1363− then do
1364− sfl <- stepL fl input
1365− case sfl of
1366− Partial fl1 -> return $ Partial $ SpanByLeft input fl1
1367− Done bl -> do
1368− resR <- initialR
1369− return
1370− $ case resR of
1371− Partial fr -> Partial $ SpanByRight bl fr
1372− Done br -> Done (bl, br)
1373− else do
1374− bl <- extractL fl
1375− resR <- initialR
1376− case resR of
1377− Partial fr -> do
1378− sfr <- stepR fr input
1379− return
1380− $ case sfr of
1381− Partial fr1 -> Partial $ SpanByRight bl fr1
1382− Done br -> Done (bl, br)
1383− Done br -> return $ Done (bl, br)
1384− step (SpanByRight bl fr) input = do
1385− sfr <- stepR fr input
1386− return
1387− $ case sfr of
1388− Partial fr1 -> Partial $ SpanByRight bl fr1
1389− Done br -> Done (bl, br)
1390−
1391− extract (SpanByLeft0 fl) = do
1392− bl <- extractL fl
1393− resR <- initialR
1394− case resR of
1395− Partial fr -> (bl, ) <$> extractR fr
1396− Done br -> return (bl, br)
1397− extract (SpanByLeft _ fl) = do
1398− bl <- extractL fl
1399− resR <- initialR
1400− case resR of
1401− Partial fr -> (bl, ) <$> extractR fr
1402− Done br -> return (bl, br)
1403− extract (SpanByRight bl fr) = (bl, ) <$> extractR fr
1404−
14051115------------------------------------------------------------------------------
14061116-- Binary splitting on a separator
14071117------------------------------------------------------------------------------