@@ -28,13 +28,13 @@
29module Streamly.Internal.Data.Array.Stream.Fold.Foreign
29module Streamly.Internal.Data.Array.Stream.Fold.Foreign
31− Fold (..)
31+ ArrayFold (..)
33 -- * Construction
33 -- * Construction
34 , fromFold
34 , fromFold
35 , fromParser
36 , fromParser
36 , fromParserD
37 , fromParserD
39 -- * Mapping
39 -- * Mapping
@@ -47,7 +47,7 @@ module Streamly.Internal.Data.Array.Stream.Fold.Foreign
47 -- * Monad
47 -- * Monad
48 , concatMap
48 , concatMap
50− -- * Transformation
50+ -- * Combinators
@@ -86,7 +86,7 @@ import Prelude hiding (concatMap, take)
87-- /Pre-release/
87-- /Pre-release/
89−newtype Fold m a b = Fold (ParserD.Parser m (Array a) b)
89+newtype ArrayFold m a b = ArrayFold (ParserD.Parser m (Array a) b)
91-------------------------------------------------------------------------------
91-------------------------------------------------------------------------------
92-- Constructing array stream folds from element folds and parsers
92-- Constructing array stream folds from element folds and parsers
@@ -97,9 +97,9 @@ newtype Fold m a b = Fold (ParserD.Parser m (Array a) b)
97-- /Pre-release/
97-- /Pre-release/
98{-# INLINE fromFold #-}
98{-# INLINE fromFold #-}
99fromFold :: forall m a b. (MonadIO m, Storable a) =>
99fromFold :: forall m a b. (MonadIO m, Storable a) =>
100− Fold.Fold m a b -> Fold m a b
100+ Fold.Fold m a b -> ArrayFold m a b
101fromFold (Fold.Fold fstep finitial fextract) =
101fromFold (Fold.Fold fstep finitial fextract) =
102− Fold (ParserD.Parser step initial fextract)
102+ ArrayFold (ParserD.Parser step initial fextract)
@@ -136,9 +136,9 @@ fromFold (Fold.Fold fstep finitial fextract) =
136-- /Pre-release/
136-- /Pre-release/
137{-# INLINE fromParserD #-}
137{-# INLINE fromParserD #-}
138fromParserD :: forall m a b. (MonadIO m, Storable a) =>
138fromParserD :: forall m a b. (MonadIO m, Storable a) =>
139− ParserD.Parser m a b -> Fold m a b
139+ ParserD.Parser m a b -> ArrayFold m a b
140fromParserD (ParserD.Parser step1 initial1 extract1) =
140fromParserD (ParserD.Parser step1 initial1 extract1) =
141− Fold (ParserD.Parser step initial1 extract1)
141+ ArrayFold (ParserD.Parser step initial1 extract1)
@@ -172,22 +172,22 @@ fromParserD (ParserD.Parser step1 initial1 extract1) =
172 partial arrRem cur next elemSize Continue n fs1
172 partial arrRem cur next elemSize Continue n fs1
173 Error err -> return $ Error err
173 Error err -> return $ Error err
175−-- | Convert an element 'Parser.Parser' into an array stream fold. If the parser
175+-- | Convert an element 'Parser.Parser' into an array stream fold. If the
176−-- fails the fold would throw an exception.
176+-- parser fails the fold would throw an exception.
178-- /Pre-release/
178-- /Pre-release/
179{-# INLINE fromParser #-}
179{-# INLINE fromParser #-}
180fromParser :: forall m a b. (MonadThrow m, MonadIO m, Storable a) =>
180fromParser :: forall m a b. (MonadThrow m, MonadIO m, Storable a) =>
181− Parser.Parser m a b -> Fold m a b
181+ Parser.Parser m a b -> ArrayFold m a b
182fromParser = fromParserD . ParserD.fromParserK
182fromParser = fromParserD . ParserD.fromParserK
184-- | Adapt an array stream fold.
184-- | Adapt an array stream fold.
186-- /Pre-release/
186-- /Pre-release/
187−{-# INLINE fromArrayFold #-}
187+{-# INLINE adaptFold #-}
188−fromArrayFold :: forall m a b. (MonadIO m) =>
188+adaptFold :: forall m a b. (MonadIO m) =>
189− Fold.Fold m (Array a) b -> Fold m a b
189+ Fold.Fold m (Array a) b -> ArrayFold m a b
190−fromArrayFold f = Fold $ ParserD.fromFold f
190+adaptFold f = ArrayFold $ ParserD.fromFold f
192-------------------------------------------------------------------------------
192-------------------------------------------------------------------------------
193-- Functor
193-- Functor
@@ -196,16 +196,16 @@ fromArrayFold f = Fold $ ParserD.fromFold f
196-- | Maps a function over the result of fold.
196-- | Maps a function over the result of fold.
198-- /Pre-release/
198-- /Pre-release/
199−instance Functor m => Functor (Fold m a) where
199+instance Functor m => Functor (ArrayFold m a) where
200 {-# INLINE fmap #-}
200 {-# INLINE fmap #-}
201− fmap f (Fold p) = Fold $ fmap f p
201+ fmap f (ArrayFold p) = ArrayFold $ fmap f p
203-- | Map a monadic function on the output of a fold.
203-- | Map a monadic function on the output of a fold.
205-- /Pre-release/
205-- /Pre-release/
206{-# INLINE rmapM #-}
206{-# INLINE rmapM #-}
207−rmapM :: Monad m => (b -> m c) -> Fold m a b -> Fold m a c
207+rmapM :: Monad m => (b -> m c) -> ArrayFold m a b -> ArrayFold m a c
208−rmapM f (Fold p) = Fold $ ParserD.rmapM f p
208+rmapM f (ArrayFold p) = ArrayFold $ ParserD.rmapM f p
210-------------------------------------------------------------------------------
210-------------------------------------------------------------------------------
211-- Sequential applicative
211-- Sequential applicative
@@ -216,8 +216,8 @@ rmapM f (Fold p) = Fold $ ParserD.rmapM f p
216-- /Pre-release/
216-- /Pre-release/
218{-# INLINE fromPure #-}
218{-# INLINE fromPure #-}
219−fromPure :: Monad m => b -> Fold m a b
219+fromPure :: Monad m => b -> ArrayFold m a b
220−fromPure = Fold . ParserD.fromPure
220+fromPure = ArrayFold . ParserD.fromPure
222-- | A fold that always yields the result of an effectful action without
222-- | A fold that always yields the result of an effectful action without
223-- consuming any input.
223-- consuming any input.
@@ -225,16 +225,18 @@ fromPure = Fold . ParserD.fromPure
225-- /Pre-release/
225-- /Pre-release/
227{-# INLINE fromEffect #-}
227{-# INLINE fromEffect #-}
228−fromEffect :: Monad m => m b -> Fold m a b
228+fromEffect :: Monad m => m b -> ArrayFold m a b
229−fromEffect = Fold . ParserD.fromEffect
229+fromEffect = ArrayFold . ParserD.fromEffect
231-- | Applies two folds sequentially on the input stream and combines their
231-- | Applies two folds sequentially on the input stream and combines their
232-- results using the supplied function.
232-- results using the supplied function.
234-- /Pre-release/
234-- /Pre-release/
235{-# INLINE serial_ #-}
235{-# INLINE serial_ #-}
236−serial_ :: MonadThrow m => Fold m x a -> Fold m x b -> Fold m x b
236+serial_ :: MonadThrow m =>
237+ ArrayFold m x a -> ArrayFold m x b -> ArrayFold m x b
238+serial_ (ArrayFold p1) (ArrayFold p2) =
237−serial_ (Fold p1) (Fold p2) = Fold $ ParserD.noErrorUnsafeSplit_ p1 p2
239+ ArrayFold $ ParserD.noErrorUnsafeSplit_ p1 p2
239-- | Applies two folds sequentially on the input stream and combines their
241-- | Applies two folds sequentially on the input stream and combines their
240-- results using the supplied function.
242-- results using the supplied function.
@@ -242,13 +244,13 @@ serial_ (Fold p1) (Fold p2) = Fold $ ParserD.noErrorUnsafeSplit_ p1 p2
242-- /Pre-release/
244-- /Pre-release/
243{-# INLINE serialWith #-}
245{-# INLINE serialWith #-}
244serialWith :: MonadThrow m
246serialWith :: MonadThrow m
245− => (a -> b -> c) -> Fold m x a -> Fold m x b -> Fold m x c
247+ => (a -> b -> c) -> ArrayFold m x a -> ArrayFold m x b -> ArrayFold m x c
246−serialWith f (Fold p1) (Fold p2) =
248+serialWith f (ArrayFold p1) (ArrayFold p2) =
247− Fold $ ParserD.noErrorUnsafeSplitWith f p1 p2
249+ ArrayFold $ ParserD.noErrorUnsafeSplitWith f p1 p2
249-- | 'Applicative' form of 'serialWith'.
251-- | 'Applicative' form of 'serialWith'.
250-- > (<*>) = serialWith id
252-- > (<*>) = serialWith id
251−instance MonadThrow m => Applicative (Fold m a) where
253+instance MonadThrow m => Applicative (ArrayFold m a) where
252 {-# INLINE pure #-}
254 {-# INLINE pure #-}
253 pure = fromPure
255 pure = fromPure
@@ -265,6 +267,8 @@ instance MonadThrow m => Applicative (Fold m a) where
266-------------------------------------------------------------------------------
268-------------------------------------------------------------------------------
270+-- XXX This should be implemented using CPS
268-- | Applies a fold on the input stream, generates the next fold from the
272-- | Applies a fold on the input stream, generates the next fold from the
269-- output of the previously applied fold and then applies that fold.
273-- output of the previously applied fold and then applies that fold.
@@ -272,15 +276,16 @@ instance MonadThrow m => Applicative (Fold m a) where
273{-# INLINE concatMap #-}
277{-# INLINE concatMap #-}
274concatMap :: MonadThrow m =>
278concatMap :: MonadThrow m =>
275− (b -> Fold m a c) -> Fold m a b -> Fold m a c
279+ (b -> ArrayFold m a c) -> ArrayFold m a b -> ArrayFold m a c
276−concatMap func (Fold p) =
280+concatMap func (ArrayFold p) =
281+ let f x = let ArrayFold y = func x in y
277− Fold $ ParserD.noErrorUnsafeConcatMap (\x -> let Fold y = func x in y) p
282+ in ArrayFold $ ParserD.noErrorUnsafeConcatMap f p
279-- | Monad instance applies folds sequentially. Next fold can depend on the
284-- | Monad instance applies folds sequentially. Next fold can depend on the
280-- output of the previous fold. See 'concatMap'.
285-- output of the previous fold. See 'concatMap'.
282-- > (>>=) = flip concatMap
287-- > (>>=) = flip concatMap
283−instance MonadThrow m => Monad (Fold m a) where
288+instance MonadThrow m => Monad (ArrayFold m a) where
284 {-# INLINE return #-}
289 {-# INLINE return #-}
285 return = pure
290 return = pure
@@ -294,10 +299,12 @@ instance MonadThrow m => Monad (Fold m a) where
294-- Array to Array folds
299-- Array to Array folds
295-------------------------------------------------------------------------------
300-------------------------------------------------------------------------------
302+-- | Take @n@ array elements (@a@) from a stream of arrays (@Array a@).
297{-# INLINE take #-}
303{-# INLINE take #-}
298−take :: forall m a b. (Monad m, Storable a) => Int -> Fold m a b -> Fold m a b
304+take :: forall m a b. (Monad m, Storable a) =>
305+ Int -> ArrayFold m a b -> ArrayFold m a b
299−take n (Fold (ParserD.Parser step1 initial1 extract1)) =
306+take n (ArrayFold (ParserD.Parser step1 initial1 extract1)) =
300− Fold $ ParserD.Parser step initial extract
307+ ArrayFold $ ParserD.Parser step initial extract