@@ -1,7 +1,5 @@
1−{-# LANGUAGE BangPatterns #-}
2{-# LANGUAGE CPP #-}
1{-# LANGUAGE CPP #-}
3{-# LANGUAGE RankNTypes #-}
2{-# LANGUAGE RankNTypes #-}
4−{-# LANGUAGE RecordWildCards #-}
5{-# LANGUAGE ScopedTypeVariables #-}
3{-# LANGUAGE ScopedTypeVariables #-}
@@ -44,8 +42,8 @@ where
44import Prelude
42import Prelude
45 hiding (any, all, takeWhile)
43 hiding (any, all, takeWhile)
47−import Streamly.Internal.Data.Parser.Types (Parser(..), Step(..))
48import Streamly.Internal.Data.Fold.Types (Fold(..))
45import Streamly.Internal.Data.Fold.Types (Fold(..))
46+import Streamly.Internal.Data.Parser.Types (Parser(..), Step(..))
50import Streamly.Internal.Data.Strict
48import Streamly.Internal.Data.Strict
@@ -57,17 +55,14 @@ import Streamly.Internal.Data.Strict
58{-# INLINE fromFold #-}
56{-# INLINE fromFold #-}
59fromFold :: Monad m => Fold m a b -> Parser m a b
57fromFold :: Monad m => Fold m a b -> Parser m a b
60−fromFold (Fold fstep finitial fextract) = Parser step initial extract
58+fromFold (Fold fstep finitial fextract) = Parser step finitial fextract
64 step s a = Yield 0 <$> fstep s a
62 step s a = Yield 0 <$> fstep s a
69-------------------------------------------------------------------------------
64-------------------------------------------------------------------------------
70−-- Terminating folds
65+-- Terminating but not failing folds
71-------------------------------------------------------------------------------
66-------------------------------------------------------------------------------
@@ -76,9 +71,12 @@ fromFold (Fold fstep finitial fextract) = Parser step initial extract
77{-# INLINABLE any #-}
72{-# INLINABLE any #-}
78any :: Monad m => (a -> Bool) -> Parser m a Bool
73any :: Monad m => (a -> Bool) -> Parser m a Bool
79−any predicate = Parser step initial (return . Right)
74+any predicate = Parser step initial return
81 initial = return False
78 initial = return False
82 step s a = return $
80 step s a = return $
84 then Stop 0 True
82 then Stop 0 True
@@ -93,9 +91,12 @@ any predicate = Parser step initial (return . Right)
94{-# INLINABLE all #-}
92{-# INLINABLE all #-}
95all :: Monad m => (a -> Bool) -> Parser m a Bool
93all :: Monad m => (a -> Bool) -> Parser m a Bool
96−all predicate = Parser step initial (return . Right)
94+all predicate = Parser step initial return
98 initial = return True
98 initial = return True
99 step s a = return $
100 step s a = return $
@@ -108,6 +109,8 @@ all predicate = Parser step initial (return . Right)
108-- Taking elements
109-- Taking elements
109-------------------------------------------------------------------------------
110-------------------------------------------------------------------------------
112+-- XXX can we use a "cmp" operation in a common implementation?
111-- | Stops after taking exactly @n@ input elements.
114-- | Stops after taking exactly @n@ input elements.
113-- * Stops - after @n@ elements.
116-- * Stops - after @n@ elements.
@@ -118,21 +121,24 @@ all predicate = Parser step initial (return . Right)
119-- /Internal/
122-- /Internal/
121−{-# INLINABLE takeEQ #-}
124+{-# INLINE takeEQ #-}
122takeEQ :: Monad m => Int -> Fold m a b -> Parser m a b
125takeEQ :: Monad m => Int -> Fold m a b -> Parser m a b
123takeEQ n (Fold fstep finitial fextract) = Parser step initial extract
126takeEQ n (Fold fstep finitial fextract) = Parser step initial extract
127− initial = (Tuple' 0) <$> finitial
130+ initial = Tuple' 0 <$> finitial
129 step (Tuple' i r) a = do
132 step (Tuple' i r) a = do
130 res <- fstep r a
133 res <- fstep r a
131 let i1 = i + 1
134 let i1 = i + 1
132 s1 = Tuple' i1 res
135 s1 = Tuple' i1 res
133− return $ if i1 < n then Skip 0 s1 else Stop 0 s1
136+ if i1 < n then return (Skip 0 s1) else Stop 0 <$> fextract res
135− extract (Tuple' i r) = fmap f (fextract r)
138+ extract (Tuple' i r) =
@@ -140,11 +146,6 @@ takeEQ n (Fold fstep finitial fextract) = Parser step initial extract
140 "takeEQ: Expecting exactly " ++ show n
146 "takeEQ: Expecting exactly " ++ show n
141 ++ " elements, got " ++ show i
147 ++ " elements, got " ++ show i
148-- | Take at least @n@ input elements, but can collect more.
149-- | Take at least @n@ input elements, but can collect more.
150-- * Stops - never.
151-- * Stops - never.
@@ -158,21 +159,22 @@ takeEQ n (Fold fstep finitial fextract) = Parser step initial extract
159-- /Internal/
160-- /Internal/
161−{-# INLINABLE takeGE #-}
162+{-# INLINE takeGE #-}
162takeGE :: Monad m => Int -> Fold m a b -> Parser m a b
163takeGE :: Monad m => Int -> Fold m a b -> Parser m a b
163takeGE n (Fold fstep finitial fextract) = Parser step initial extract
164takeGE n (Fold fstep finitial fextract) = Parser step initial extract
167− initial = (Tuple' 0) <$> finitial
168+ initial = Tuple' 0 <$> finitial
169 step (Tuple' i r) a = do
170 step (Tuple' i r) a = do
170 res <- fstep r a
171 res <- fstep r a
171 let i1 = i + 1
172 let i1 = i + 1
172 s1 = Tuple' i1 res
173 s1 = Tuple' i1 res
173− if i1 < n
175+ if i1 < n
174− then return $ Skip 0 s1
176+ then Skip 0 s1
175− else return $ Yield 0 s1
177+ else Yield 0 s1
177 extract (Tuple' i r) = fmap f (fextract r)
179 extract (Tuple' i r) = fmap f (fextract r)
@@ -184,8 +186,8 @@ takeGE n (Fold fstep finitial fextract) = Parser step initial extract
186 if i >= n
188 if i >= n
187− then Right x
189+ then x
188− else Left err
190+ else error err
190-- | Take until the predicate fails. The element on which the predicate fails
192-- | Take until the predicate fails. The element on which the predicate fails
191-- is returned back to the input stream.
193-- is returned back to the input stream.
@@ -202,21 +204,19 @@ takeGE n (Fold fstep finitial fextract) = Parser step initial extract
203-- /Internal/
205-- /Internal/
205−{-# INLINABLE takeWhile #-}
207+{-# INLINE takeWhile #-}
206takeWhile :: Monad m => (a -> Bool) -> Fold m a b -> Parser m a b
208takeWhile :: Monad m => (a -> Bool) -> Fold m a b -> Parser m a b
207takeWhile predicate (Fold fstep finitial fextract) =
209takeWhile predicate (Fold fstep finitial fextract) =
208− Parser step initial extract
210+ Parser step initial fextract
212 initial = finitial
214 initial = finitial
213− step s a = do
216+ step s a =
214 if predicate a
217 if predicate a
215 then Yield 0 <$> fstep s a
218 then Yield 0 <$> fstep s a
216− else return $ Stop 1 s
218− b <- fextract s
219+ else Stop 1 <$> fextract s
221-- | Keep taking elements until the predicate succeeds. Drop the succeeding
221-- | Keep taking elements until the predicate succeeds. Drop the succeeding
222-- element.
222-- element.
@@ -237,18 +237,15 @@ takeWhile predicate (Fold fstep finitial fextract) =
237{-# INLINABLE sepBy #-}
237{-# INLINABLE sepBy #-}
238sepBy :: Monad m => (a -> Bool) -> Fold m a b -> Parser m a b
238sepBy :: Monad m => (a -> Bool) -> Fold m a b -> Parser m a b
239sepBy predicate (Fold fstep finitial fextract) =
239sepBy predicate (Fold fstep finitial fextract) =
240− Parser step initial extract
240+ Parser step initial fextract
244 initial = finitial
244 initial = finitial
245− step s a = do
245+ step s a =
246 if not (predicate a)
246 if not (predicate a)
247 then Yield 0 <$> fstep s a
247 then Yield 0 <$> fstep s a
248− else return $ Stop 0 s
250− b <- fextract s
248+ else Stop 0 <$> fextract s
253-- | Keep taking elements until the predicate succeeds. Take the succeeding
250-- | Keep taking elements until the predicate succeeds. Take the succeeding
254-- element as well.
251-- element as well.
@@ -303,10 +300,10 @@ sepByMax predicate count (Fold fstep finitial fextract) =
303 s1 = Tuple' i1 res
300 s1 = Tuple' i1 res
304 if not (predicate a) && i1 < count
301 if not (predicate a) && i1 < count
305 then return $ Yield 0 s1
302 then return $ Yield 0 s1
306− else return $ Stop 0 s1
305+ return $ Stop 0 b
307− extract (Tuple' _ r) = do
306+ extract (Tuple' _ r) = fextract r
311-- | Like 'splitOn' after stripping leading, trailing, and repeated separators.
308-- | Like 'splitOn' after stripping leading, trailing, and repeated separators.
312-- Therefore, @".a..b."@ with '.' as the separator would be parsed as
309-- Therefore, @".a..b."@ with '.' as the separator would be parsed as