on master help sign in
Move length and takeEndBy_ to lower level module
commit8cdf6ede1136f84788eac80ae35dafa7b412d272 authorHarendra Kumar <harendra@composewell.com> dateSun 31 Dec 2023, 01:57 (3 years ago) parent471de54
2 files changed+91−92
core/src/Streamly/Internal/Data/Fold/Combinators.hs−92 core/src/Streamly/Internal/Data/Fold/Type.hs+91
core/src/Streamly/Internal/Data/Fold/Combinators.hs−92
@@ -30,8 +30,6 @@ module Streamly.Internal.Data.Fold.Combinators
3030 -- *** Reducers
3131 , drainMapM
3232 , the
33− , length
34− , lengthGeneric
3533 , mean
3634 , rollingHash
3735 , defaultSalt
@@ -166,8 +164,6 @@ module Streamly.Internal.Data.Fold.Combinators
166164 -- ** Trimming
167165
168166 -- By elements
169− , takeEndBy
170− , takeEndBy_
171167 , takeEndBySeq
172168 , takeEndBySeq_
173169 {-
@@ -709,30 +705,6 @@ the = foldt' step initial id
709705-- To Summary
710706------------------------------------------------------------------------------
711707
712−-- | Like 'length', except with a more general 'Num' return value
713−--
714−-- Definition:
715−--
716−-- >>> lengthGeneric = fmap getSum $ Fold.foldMap (Sum . const 1)
717−-- >>> lengthGeneric = Fold.foldl' (\n _ -> n + 1) 0
718−--
719−-- /Pre-release/
720−{-# INLINE lengthGeneric #-}
721−lengthGeneric :: (Monad m, Num b) => Fold m a b
722−lengthGeneric = foldl' (\n _ -> n + 1) 0
723−
724−-- | Determine the length of the input stream.
725−--
726−-- Definition:
727−--
728−-- >>> length = Fold.lengthGeneric
729−-- >>> length = fmap getSum $ Fold.foldMap (Sum . const 1)
730−--
731−{-# INLINE length #-}
732−length :: Monad m => Fold m a Int
733−length = lengthGeneric
734−
735−
736708-- | Determine the sum of all elements of a stream of numbers. Returns additive
737709-- identity (@0@) when the stream is empty. Note that this is not numerically
738710-- stable for floating point numbers.
@@ -1510,70 +1482,6 @@ droppingWhileM p = Fold step initial extract extract
15101482droppingWhile :: Monad m => (a -> Bool) -> Fold m a (Maybe a)
15111483droppingWhile p = droppingWhileM (return . p)
15121484
1513−-- Note: Keep this consistent with S.splitOn. In fact we should eliminate
1514−-- S.splitOn in favor of the fold.
1515−--
1516−-- XXX Use Fold.many instead once it is fixed.
1517−-- > Stream.splitOnSuffix p f = Stream.foldMany (Fold.takeEndBy_ p f)
1518−
1519−-- | Like 'takeEndBy' but drops the element on which the predicate succeeds.
1520−--
1521−-- Example:
1522−--
1523−-- >>> input = Stream.fromList "hello\nthere\n"
1524−-- >>> line = Fold.takeEndBy_ (== '\n') Fold.toList
1525−-- >>> Stream.fold line input
1526−-- "hello"
1527−--
1528−-- >>> Stream.fold Fold.toList $ Stream.foldMany line input
1529−-- ["hello","there"]
1530−--
1531−{-# INLINE takeEndBy_ #-}
1532−takeEndBy_ :: Monad m => (a -> Bool) -> Fold m a b -> Fold m a b
1533−-- takeEndBy_ predicate = scanMaybe (takingEndBy_ predicate)
1534−takeEndBy_ predicate (Fold fstep finitial fextract ffinal) =
1535− Fold step finitial fextract ffinal
1536−
1537− where
1538−
1539− step s a =
1540− if not (predicate a)
1541− then fstep s a
1542− else Done <$> ffinal s
1543−
1544−-- Note:
1545−-- > Stream.splitWithSuffix p f = Stream.foldMany (Fold.takeEndBy p f)
1546−
1547−-- | Take the input, stop when the predicate succeeds taking the succeeding
1548−-- element as well.
1549−--
1550−-- Example:
1551−--
1552−-- >>> input = Stream.fromList "hello\nthere\n"
1553−-- >>> line = Fold.takeEndBy (== '\n') Fold.toList
1554−-- >>> Stream.fold line input
1555−-- "hello\n"
1556−--
1557−-- >>> Stream.fold Fold.toList $ Stream.foldMany line input
1558−-- ["hello\n","there\n"]
1559−--
1560−{-# INLINE takeEndBy #-}
1561−takeEndBy :: Monad m => (a -> Bool) -> Fold m a b -> Fold m a b
1562−-- takeEndBy predicate = scanMaybe (takingEndBy predicate)
1563−takeEndBy predicate (Fold fstep finitial fextract ffinal) =
1564− Fold step finitial fextract ffinal
1565−
1566− where
1567−
1568− step s a = do
1569− res <- fstep s a
1570− if not (predicate a)
1571− then return res
1572− else do
1573− case res of
1574− Partial s1 -> Done <$> ffinal s1
1575− Done b -> return $ Done b
1576−
15771485------------------------------------------------------------------------------
15781486-- Binary splitting on a separator
15791487------------------------------------------------------------------------------
core/src/Streamly/Internal/Data/Fold/Type.hs+91
@@ -367,6 +367,8 @@ module Streamly.Internal.Data.Fold.Type
367367 , toList
368368 , toStreamK
369369 , toStreamKRev
370+ , lengthGeneric
371+ , length
370372
371373 -- * Combinators
372374
@@ -391,6 +393,8 @@ module Streamly.Internal.Data.Fold.Type
391393 -- ** Trimming
392394 , take
393395 , taking
396+ , takeEndBy_
397+ , takeEndBy
394398 , dropping
395399
396400 -- ** Sequential application
@@ -767,6 +771,29 @@ toStreamKRev = foldl' (flip K.cons) K.nil
767771toStreamK :: Monad m => Fold m a (K.StreamK n a)
768772toStreamK = foldr K.cons K.nil
769773
774+-- | Like 'length', except with a more general 'Num' return value
775+--
776+-- Definition:
777+--
778+-- >>> lengthGeneric = fmap getSum $ Fold.foldMap (Sum . const 1)
779+-- >>> lengthGeneric = Fold.foldl' (\n _ -> n + 1) 0
780+--
781+-- /Pre-release/
782+{-# INLINE lengthGeneric #-}
783+lengthGeneric :: (Monad m, Num b) => Fold m a b
784+lengthGeneric = foldl' (\n _ -> n + 1) 0
785+
786+-- | Determine the length of the input stream.
787+--
788+-- Definition:
789+--
790+-- >>> length = Fold.lengthGeneric
791+-- >>> length = fmap getSum $ Fold.foldMap (Sum . const 1)
792+--
793+{-# INLINE length #-}
794+length :: Monad m => Fold m a Int
795+length = lengthGeneric
796+
770797------------------------------------------------------------------------------
771798-- Instances
772799------------------------------------------------------------------------------
@@ -1517,6 +1544,70 @@ take n (Fold fstep finitial fextract ffinal) = Fold step initial extract final
15171544
15181545 final (Tuple'Fused _ r) = ffinal r
15191546
1547+-- Note: Keep this consistent with S.splitOn. In fact we should eliminate
1548+-- S.splitOn in favor of the fold.
1549+--
1550+-- XXX Use Fold.many instead once it is fixed.
1551+-- > Stream.splitOnSuffix p f = Stream.foldMany (Fold.takeEndBy_ p f)
1552+
1553+-- | Like 'takeEndBy' but drops the element on which the predicate succeeds.
1554+--
1555+-- Example:
1556+--
1557+-- >>> input = Stream.fromList "hello\nthere\n"
1558+-- >>> line = Fold.takeEndBy_ (== '\n') Fold.toList
1559+-- >>> Stream.fold line input
1560+-- "hello"
1561+--
1562+-- >>> Stream.fold Fold.toList $ Stream.foldMany line input
1563+-- ["hello","there"]
1564+--
1565+{-# INLINE takeEndBy_ #-}
1566+takeEndBy_ :: Monad m => (a -> Bool) -> Fold m a b -> Fold m a b
1567+-- takeEndBy_ predicate = scanMaybe (takingEndBy_ predicate)
1568+takeEndBy_ predicate (Fold fstep finitial fextract ffinal) =
1569+ Fold step finitial fextract ffinal
1570+
1571+ where
1572+
1573+ step s a =
1574+ if not (predicate a)
1575+ then fstep s a
1576+ else Done <$> ffinal s
1577+
1578+-- Note:
1579+-- > Stream.splitWithSuffix p f = Stream.foldMany (Fold.takeEndBy p f)
1580+
1581+-- | Take the input, stop when the predicate succeeds taking the succeeding
1582+-- element as well.
1583+--
1584+-- Example:
1585+--
1586+-- >>> input = Stream.fromList "hello\nthere\n"
1587+-- >>> line = Fold.takeEndBy (== '\n') Fold.toList
1588+-- >>> Stream.fold line input
1589+-- "hello\n"
1590+--
1591+-- >>> Stream.fold Fold.toList $ Stream.foldMany line input
1592+-- ["hello\n","there\n"]
1593+--
1594+{-# INLINE takeEndBy #-}
1595+takeEndBy :: Monad m => (a -> Bool) -> Fold m a b -> Fold m a b
1596+-- takeEndBy predicate = scanMaybe (takingEndBy predicate)
1597+takeEndBy predicate (Fold fstep finitial fextract ffinal) =
1598+ Fold step finitial fextract ffinal
1599+
1600+ where
1601+
1602+ step s a = do
1603+ res <- fstep s a
1604+ if not (predicate a)
1605+ then return res
1606+ else do
1607+ case res of
1608+ Partial s1 -> Done <$> ffinal s1
1609+ Done b -> return $ Done b
1610+
15201611------------------------------------------------------------------------------
15211612-- Nesting
15221613------------------------------------------------------------------------------