on master help sign in
Rename Array.Fold to ArrayFold
f1c5e5cHarendra Kumar
core/src/Streamly/Internal/Data/Array/Stream/Fold/Foreign.hs+43−36
@@ -28,13 +28,13 @@
28--
28--
29module Streamly.Internal.Data.Array.Stream.Fold.Foreign
29module Streamly.Internal.Data.Array.Stream.Fold.Foreign
30 (
30 (
31− Fold (..)
31+ ArrayFold (..)
32
32
33 -- * Construction
33 -- * Construction
34 , fromFold
34 , fromFold
35+ , adaptFold
35 , fromParser
36 , fromParser
36 , fromParserD
37 , fromParserD
37− , fromArrayFold
38
38
39 -- * Mapping
39 -- * Mapping
40 , rmapM
40 , rmapM
@@ -47,7 +47,7 @@ module Streamly.Internal.Data.Array.Stream.Fold.Foreign
47 -- * Monad
47 -- * Monad
48 , concatMap
48 , concatMap
49
49
50− -- * Transformation
50+ -- * Combinators
51 , take
51 , take
52 )
52 )
53where
53where
@@ -86,7 +86,7 @@ import Prelude hiding (concatMap, take)
86--
86--
87-- /Pre-release/
87-- /Pre-release/
88--
88--
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)
90
90
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)
103
103
104 where
104 where
105
105
@@ -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)
142
142
143 where
143 where
144
144
@@ -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
174
174
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.
177--
177--
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
183
183
184-- | Adapt an array stream fold.
184-- | Adapt an array stream fold.
185--
185--
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
191
191
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.
197--
197--
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
202
202
203-- | Map a monadic function on the output of a fold.
203-- | Map a monadic function on the output of a fold.
204--
204--
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
209
209
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/
217--
217--
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
221
221
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/
226--
226--
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
230
230
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.
233--
233--
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
238
240
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
248
250
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
254
256
@@ -265,6 +267,8 @@ instance MonadThrow m => Applicative (Fold m a) where
265-- Monad
267-- Monad
266-------------------------------------------------------------------------------
268-------------------------------------------------------------------------------
267
269
270+-- XXX This should be implemented using CPS
271+--
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.
270--
274--
@@ -272,15 +276,16 @@ instance MonadThrow m => Applicative (Fold m a) where
272--
276--
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
278
283
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'.
281--
286--
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
286
291
@@ -294,10 +299,12 @@ instance MonadThrow m => Monad (Fold m a) where
294-- Array to Array folds
299-- Array to Array folds
295-------------------------------------------------------------------------------
300-------------------------------------------------------------------------------
296
301
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
301
308
302 where
309 where
303
310
src/Streamly/Internal/Data/Array/Stream/Foreign.hs+8−8
@@ -70,6 +70,7 @@ import Prelude hiding (null, last, (!!), read, concat, unlines)
70import Streamly.Internal.Data.Array.Foreign.Mut.Type
70import Streamly.Internal.Data.Array.Foreign.Mut.Type
71 (arrayToFptrContents, fptrToArrayContents, touch)
71 (arrayToFptrContents, fptrToArrayContents, touch)
72import Streamly.Internal.Data.Array.Foreign.Type (Array(..))
72import Streamly.Internal.Data.Array.Foreign.Type (Array(..))
73+import Streamly.Internal.Data.Array.Stream.Fold.Foreign (ArrayFold(..))
73import Streamly.Internal.Data.Fold.Type (Fold(..))
74import Streamly.Internal.Data.Fold.Type (Fold(..))
74import Streamly.Internal.Data.Parser (ParseError(..))
75import Streamly.Internal.Data.Parser (ParseError(..))
75import Streamly.Internal.Data.Stream.Serial (SerialT)
76import Streamly.Internal.Data.Stream.Serial (SerialT)
@@ -84,7 +85,6 @@ import qualified Streamly.Internal.Data.Array.Foreign as Array
84import qualified Streamly.Internal.Data.Array.Foreign.Type as A
85import qualified Streamly.Internal.Data.Array.Foreign.Type as A
85import qualified Streamly.Internal.Data.Array.Foreign.Mut.Type as MA
86import qualified Streamly.Internal.Data.Array.Foreign.Mut.Type as MA
86import qualified Streamly.Internal.Data.Array.Stream.Mut.Foreign as AS
87import qualified Streamly.Internal.Data.Array.Stream.Mut.Foreign as AS
87−import qualified Streamly.Internal.Data.Array.Stream.Fold.Foreign as ASF
88import qualified Streamly.Internal.Data.Fold.Type as FL
88import qualified Streamly.Internal.Data.Fold.Type as FL
89import qualified Streamly.Internal.Data.Parser as PR
89import qualified Streamly.Internal.Data.Parser as PR
90import qualified Streamly.Internal.Data.Parser.ParserD as PRD
90import qualified Streamly.Internal.Data.Parser.ParserD as PRD
@@ -704,8 +704,8 @@ parseArr p s = fmap fromStreamD <$> parseD p (toStreamD s)
704--
704--
705{-# INLINE foldArr #-}
705{-# INLINE foldArr #-}
706foldArr :: (MonadIO m, MonadThrow m, Storable a) =>
706foldArr :: (MonadIO m, MonadThrow m, Storable a) =>
707− ASF.Fold m a b -> SerialT m (A.Array a) -> m b
707+ ArrayFold m a b -> SerialT m (A.Array a) -> m b
708−foldArr (ASF.Fold p) s = fst <$> parseArrD p (toStreamD s)
708+foldArr (ArrayFold p) s = fst <$> parseArrD p (toStreamD s)
709
709
710-- | Like 'fold' but also returns the remaining stream.
710-- | Like 'fold' but also returns the remaining stream.
711--
711--
@@ -713,8 +713,8 @@ foldArr (ASF.Fold p) s = fst <$> parseArrD p (toStreamD s)
713--
713--
714{-# INLINE foldArr_ #-}
714{-# INLINE foldArr_ #-}
715foldArr_ :: (MonadIO m, MonadThrow m, Storable a) =>
715foldArr_ :: (MonadIO m, MonadThrow m, Storable a) =>
716− ASF.Fold m a b -> SerialT m (A.Array a) -> m (b, SerialT m (A.Array a))
716+ ArrayFold m a b -> SerialT m (A.Array a) -> m (b, SerialT m (A.Array a))
717−foldArr_ (ASF.Fold p) s = second fromStreamD <$> parseArrD p (toStreamD s)
717+foldArr_ (ArrayFold p) s = second fromStreamD <$> parseArrD p (toStreamD s)
718
718
719{-# ANN type ParseChunksState Fuse #-}
719{-# ANN type ParseChunksState Fuse #-}
720data ParseChunksState x inpBuf st pst =
720data ParseChunksState x inpBuf st pst =
@@ -727,10 +727,10 @@ data ParseChunksState x inpBuf st pst =
727{-# INLINE_NORMAL foldArrManyD #-}
727{-# INLINE_NORMAL foldArrManyD #-}
728foldArrManyD
728foldArrManyD
729 :: (MonadThrow m, Storable a)
729 :: (MonadThrow m, Storable a)
730− => ASF.Fold m a b
730+ => ArrayFold m a b
731 -> D.Stream m (Array a)
731 -> D.Stream m (Array a)
732 -> D.Stream m b
732 -> D.Stream m b
733−foldArrManyD (ASF.Fold (PRD.Parser pstep initial extract)) (D.Stream step state) =
733+foldArrManyD (ArrayFold (PRD.Parser pstep initial extract)) (D.Stream step state) =
734 D.Stream stepOuter (ParseChunksInit [] state)
734 D.Stream stepOuter (ParseChunksInit [] state)
735
735
736 where
736 where
@@ -853,7 +853,7 @@ foldArrManyD (ASF.Fold (PRD.Parser pstep initial extract)) (D.Stream step state)
853{-# INLINE foldArrMany #-}
853{-# INLINE foldArrMany #-}
854foldArrMany
854foldArrMany
855 :: (IsStream t, MonadThrow m, Storable a)
855 :: (IsStream t, MonadThrow m, Storable a)
856− => ASF.Fold m a b
856+ => ArrayFold m a b
857 -> t m (Array a)
857 -> t m (Array a)
858 -> t m b
858 -> t m b
859foldArrMany p m = fromStreamD $ foldArrManyD p (toStreamD m)
859foldArrMany p m = fromStreamD $ foldArrManyD p (toStreamD m)