@@ -1,7 +1,5 @@
1−{-# LANGUAGE BangPatterns #-}
21{-# LANGUAGE CPP #-}
32{-# LANGUAGE RankNTypes #-}
4−{-# LANGUAGE RecordWildCards #-}
53{-# LANGUAGE ScopedTypeVariables #-}
64
75-- |
@@ -44,8 +42,8 @@ where
4442import Prelude
4543 hiding (any, all, takeWhile)
4644
47−import Streamly.Internal.Data.Parser.Types (Parser(..), Step(..))
4845import Streamly.Internal.Data.Fold.Types (Fold(..))
46+import Streamly.Internal.Data.Parser.Types (Parser(..), Step(..))
4947
5048import Streamly.Internal.Data.Strict
5149
@@ -57,17 +55,14 @@ import Streamly.Internal.Data.Strict
5755--
5856{-# INLINE fromFold #-}
5957fromFold :: 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
59+
6160 where
6261
63− initial = finitial
6462 step s a = Yield 0 <$> fstep s a
65− extract s = do
66− r <- fextract s
67− return $ Right r
6863
6964-------------------------------------------------------------------------------
70−-- Terminating folds
65+-- Terminating but not failing folds
7166-------------------------------------------------------------------------------
7267--
7368-- |
@@ -76,9 +71,12 @@ fromFold (Fold fstep finitial fextract) = Parser step initial extract
7671--
7772{-# INLINABLE any #-}
7873any :: Monad m => (a -> Bool) -> Parser m a Bool
79−any predicate = Parser step initial (return . Right)
74+any predicate = Parser step initial return
75+
8076 where
77+
8178 initial = return False
79+
8280 step s a = return $
8381 if s
8482 then Stop 0 True
@@ -93,9 +91,12 @@ any predicate = Parser step initial (return . Right)
9391--
9492{-# INLINABLE all #-}
9593all :: Monad m => (a -> Bool) -> Parser m a Bool
96−all predicate = Parser step initial (return . Right)
94+all predicate = Parser step initial return
95+
9796 where
97+
9898 initial = return True
99+
99100 step s a = return $
100101 if s
101102 then
@@ -108,6 +109,8 @@ all predicate = Parser step initial (return . Right)
108109-- Taking elements
109110-------------------------------------------------------------------------------
110111--
112+-- XXX can we use a "cmp" operation in a common implementation?
113+--
111114-- | Stops after taking exactly @n@ input elements.
112115--
113116-- * Stops - after @n@ elements.
@@ -118,21 +121,24 @@ all predicate = Parser step initial (return . Right)
118121--
119122-- /Internal/
120123--
121−{-# INLINABLE takeEQ #-}
124+{-# INLINE takeEQ #-}
122125takeEQ :: Monad m => Int -> Fold m a b -> Parser m a b
123126takeEQ n (Fold fstep finitial fextract) = Parser step initial extract
124127
125128 where
126129
127− initial = (Tuple' 0) <$> finitial
130+ initial = Tuple' 0 <$> finitial
128131
129132 step (Tuple' i r) a = do
130133 res <- fstep r a
131134 let i1 = i + 1
132135 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
134137
135− extract (Tuple' i r) = fmap f (fextract r)
138+ extract (Tuple' i r) =
139+ if n == i
140+ then fextract r
141+ else error err
136142
137143 where
138144
@@ -140,11 +146,6 @@ takeEQ n (Fold fstep finitial fextract) = Parser step initial extract
140146 "takeEQ: Expecting exactly " ++ show n
141147 ++ " elements, got " ++ show i
142148
143− f x =
144− if n == i
145− then Right x
146− else Left err
147−
148149-- | Take at least @n@ input elements, but can collect more.
149150--
150151-- * Stops - never.
@@ -158,21 +159,22 @@ takeEQ n (Fold fstep finitial fextract) = Parser step initial extract
158159--
159160-- /Internal/
160161--
161−{-# INLINABLE takeGE #-}
162+{-# INLINE takeGE #-}
162163takeGE :: Monad m => Int -> Fold m a b -> Parser m a b
163164takeGE n (Fold fstep finitial fextract) = Parser step initial extract
164165
165166 where
166167
167− initial = (Tuple' 0) <$> finitial
168+ initial = Tuple' 0 <$> finitial
168169
169170 step (Tuple' i r) a = do
170171 res <- fstep r a
171172 let i1 = i + 1
172173 s1 = Tuple' i1 res
173− if i1 < n
174− then return $ Skip 0 s1
175− else return $ Yield 0 s1
174+ return $
175+ if i1 < n
176+ then Skip 0 s1
177+ else Yield 0 s1
176178
177179 extract (Tuple' i r) = fmap f (fextract r)
178180
@@ -184,8 +186,8 @@ takeGE n (Fold fstep finitial fextract) = Parser step initial extract
184186
185187 f x =
186188 if i >= n
187− then Right x
188− else Left err
189+ then x
190+ else error err
189191
190192-- | Take until the predicate fails. The element on which the predicate fails
191193-- is returned back to the input stream.
@@ -202,21 +204,19 @@ takeGE n (Fold fstep finitial fextract) = Parser step initial extract
202204--
203205-- /Internal/
204206--
205−{-# INLINABLE takeWhile #-}
207+{-# INLINE takeWhile #-}
206208takeWhile :: Monad m => (a -> Bool) -> Fold m a b -> Parser m a b
207209takeWhile predicate (Fold fstep finitial fextract) =
208− Parser step initial extract
210+ Parser step initial fextract
209211
210212 where
211213
212214 initial = finitial
213− step s a = do
215+
216+ step s a =
214217 if predicate a
215218 then Yield 0 <$> fstep s a
216− else return $ Stop 1 s
217− extract s = do
218− b <- fextract s
219− return $ Right b
219+ else Stop 1 <$> fextract s
220220
221221-- | Keep taking elements until the predicate succeeds. Drop the succeeding
222222-- element.
@@ -237,18 +237,15 @@ takeWhile predicate (Fold fstep finitial fextract) =
237237{-# INLINABLE sepBy #-}
238238sepBy :: Monad m => (a -> Bool) -> Fold m a b -> Parser m a b
239239sepBy predicate (Fold fstep finitial fextract) =
240− Parser step initial extract
240+ Parser step initial fextract
241241
242242 where
243243
244244 initial = finitial
245− step s a = do
245+ step s a =
246246 if not (predicate a)
247247 then Yield 0 <$> fstep s a
248− else return $ Stop 0 s
249− extract s = do
250− b <- fextract s
251− return $ Right b
248+ else Stop 0 <$> fextract s
252249
253250-- | Keep taking elements until the predicate succeeds. Take the succeeding
254251-- element as well.
@@ -303,10 +300,10 @@ sepByMax predicate count (Fold fstep finitial fextract) =
303300 s1 = Tuple' i1 res
304301 if not (predicate a) && i1 < count
305302 then return $ Yield 0 s1
306− else return $ Stop 0 s1
307− extract (Tuple' _ r) = do
308− b <- fextract r
309− return $ Right b
303+ else do
304+ b <- fextract res
305+ return $ Stop 0 b
306+ extract (Tuple' _ r) = fextract r
310307
311308-- | Like 'splitOn' after stripping leading, trailing, and repeated separators.
312309-- Therefore, @".a..b."@ with '.' as the separator would be parsed as