on master help sign in
Deprecate mapM and sequence in favor of rmapM and rsequence
commit9473e6bbbef2dc93c8dd40ad7d69b3934a47d8f9 authorAdithya Kumar <adithya@composewell.com> dateFri 15 Jan 2021, 20:16 (6 years ago) parentec19422
8 files changed+38−18
benchmark/Streamly/Benchmark/Data/Fold.hs+3−3 src/Streamly/Data/Fold.hs+2 src/Streamly/Internal/Data/Array/Prim/TypesInclude.hs+3−3 src/Streamly/Internal/Data/Array/Storable/Foreign/Mut/Types.hs+1−1 src/Streamly/Internal/Data/Array/Storable/Foreign/Types.hs+1−1 src/Streamly/Internal/Data/Fold.hs+23−5 src/Streamly/Internal/Data/Stream/IsStream/Transform.hs+1−1 test/Streamly/Test/Data/Fold.hs+4−4
benchmark/Streamly/Benchmark/Data/Fold.hs+3−3
@@ -245,9 +245,9 @@ o_1_space_serial_transformation value =
245245 in benchIOSink value "mapMaybe" (S.fold fld)
246246 , benchIOSink
247247 value
248− "sequence"
249− (S.fold (FL.sequence (return <$> FL.drain)))
250− , benchIOSink value "mapM" (S.fold (FL.mapM return FL.drain))
248+ "rsequence"
249+ (S.fold (FL.rsequence (return <$> FL.drain)))
250+ , benchIOSink value "rmapM" (S.fold (FL.rmapM return FL.drain))
251251 , benchIOSink
252252 value
253253 "pipe-mapM"
src/Streamly/Data/Fold.hs+2
@@ -128,7 +128,9 @@ module Streamly.Data.Fold
128128 --
129129 -- Note: Output transformations are also known as covariant
130130 -- transformations.
131+ , rsequence
131132 , sequence
133+ , rmapM
132134 , mapM
133135
134136 {-
src/Streamly/Internal/Data/Array/Prim/TypesInclude.hs+3−3
@@ -162,7 +162,7 @@ nil =
162162-- /Internal/
163163{-# INLINE_NORMAL write #-}
164164write :: (MonadIO m, Prim a) => Fold m a (Array a)
165−write = FL.mapM unsafeFreeze MA.write
165+write = FL.rmapM unsafeFreeze MA.write
166166
167167-- | @writeN n@ folds a maximum of @n@ elements from the input stream to an
168168-- 'Array'.
@@ -170,7 +170,7 @@ write = FL.mapM unsafeFreeze MA.write
170170-- /Internal/
171171{-# INLINE_NORMAL writeN #-}
172172writeN :: (MonadIO m, Prim a) => Int -> Fold m a (Array a)
173−writeN limit = FL.mapM unsafeFreeze (MA.writeN limit)
173+writeN limit = FL.rmapM unsafeFreeze (MA.writeN limit)
174174
175175-- | Like 'writeN' but does not check the array bounds when writing. The fold
176176-- driver must not call the step function more than 'n' times otherwise it will
@@ -181,7 +181,7 @@ writeN limit = FL.mapM unsafeFreeze (MA.writeN limit)
181181-- /Internal/
182182{-# INLINE_NORMAL writeNUnsafe #-}
183183writeNUnsafe :: (MonadIO m, Prim a) => Int -> Fold m a (Array a)
184−writeNUnsafe limit = FL.mapM unsafeFreeze (MA.writeNUnsafe limit)
184+writeNUnsafe limit = FL.rmapM unsafeFreeze (MA.writeNUnsafe limit)
185185
186186{-# INLINE_NORMAL fromStreamDN #-}
187187fromStreamDN :: (MonadIO m, Prim a) => Int -> D.Stream m a -> m (Array a)
src/Streamly/Internal/Data/Array/Storable/Foreign/Mut/Types.hs+1−1
@@ -1177,7 +1177,7 @@ writeNUnsafe n = Fold step initial extract
11771177{-# INLINE_NORMAL toArrayMinChunk #-}
11781178toArrayMinChunk :: forall m a. (MonadIO m, Storable a)
11791179 => Int -> Int -> Fold m a (Array a)
1180−-- toArrayMinChunk n = FL.mapM spliceArrays $ toArraysOf n
1180+-- toArrayMinChunk n = FL.rmapM spliceArrays $ toArraysOf n
11811181toArrayMinChunk alignSize elemCount = FL.mkAccumM step initial extract
11821182
11831183 where
src/Streamly/Internal/Data/Array/Storable/Foreign/Types.hs+1−1
@@ -552,7 +552,7 @@ writeNUnsafe n = unsafeFreeze <$> MA.writeNUnsafe n
552552{-# INLINE_NORMAL toArrayMinChunk #-}
553553toArrayMinChunk :: forall m a. (MonadIO m, Storable a)
554554 => Int -> Int -> Fold m a (Array a)
555−-- toArrayMinChunk n = FL.mapM spliceArrays $ toArraysOf n
555+-- toArrayMinChunk n = FL.rmapM spliceArrays $ toArraysOf n
556556toArrayMinChunk alignSize elemCount =
557557 unsafeFreeze <$> MA.toArrayMinChunk alignSize elemCount
558558
src/Streamly/Internal/Data/Fold.hs+23−5
@@ -100,7 +100,9 @@ module Streamly.Internal.Data.Fold
100100 , runStep
101101
102102 -- * Output Transformations
103+ , rsequence
103104 , sequence
105+ , rmapM
104106 , mapM
105107
106108 -- * Input Transformations
@@ -366,10 +368,10 @@ generally = hoist (return . runIdentity)
366368
367369-- | Flatten the monadic output of a fold to pure output.
368370--
369−-- @since 0.7.0
370−{-# INLINE sequence #-}
371−sequence :: Monad m => Fold m a (m b) -> Fold m a b
372−sequence (Fold step initial extract) = Fold step' initial1 extract'
371+-- @since 0.8.0
372+{-# INLINE rsequence #-}
373+rsequence :: Monad m => Fold m a (m b) -> Fold m a b
374+rsequence (Fold step initial extract) = Fold step' initial1 extract'
373375
374376 where
375377
@@ -384,12 +386,28 @@ sequence (Fold step initial extract) = Fold step' initial1 extract'
384386
385387 extract' = join . extract
386388
389+-- | Flatten the monadic output of a fold to pure output.
390+--
391+-- @since 0.7.0
392+{-# DEPRECATED sequence "Use rsequence instead" #-}
393+{-# INLINE sequence #-}
394+sequence :: Monad m => Fold m a (m b) -> Fold m a b
395+sequence = rsequence
396+
397+-- | Map a monadic function on the output of a fold.
398+--
399+-- @since 0.8.0
400+{-# INLINE rmapM #-}
401+rmapM :: Monad m => (b -> m c) -> Fold m a b -> Fold m a c
402+rmapM f = sequence . fmap f
403+
387404-- | Map a monadic function on the output of a fold.
388405--
389406-- @since 0.7.0
407+{-# DEPRECATED mapM "Use rmapM instead" #-}
390408{-# INLINE mapM #-}
391409mapM :: Monad m => (b -> m c) -> Fold m a b -> Fold m a c
392−mapM f = sequence . fmap f
410+mapM = rmapM
393411
394412-- | @mapMaybe f fold@ maps a 'Maybe' returning function @f@ on the input of
395413-- the fold, filters out 'Nothing' elements, and return the values extracted
src/Streamly/Internal/Data/Stream/IsStream/Transform.hs+1−1
@@ -372,7 +372,7 @@ tap f xs = D.fromStreamD $ D.tap f (D.toStreamD xs)
372372-- this range then @offset `mod` n@ is used as offset.
373373--
374374-- @
375−-- >>> S.drain $ S.tapOffsetEvery 0 2 (FL.mapM print FL.toList) $ S.enumerateFromTo 0 10
375+-- >>> S.drain $ S.tapOffsetEvery 0 2 (FL.rmapM print FL.toList) $ S.enumerateFromTo 0 10
376376-- > [0,2,4,6,8,10]
377377-- @
378378--
test/Streamly/Test/Data/Fold.hs+4−4
@@ -343,8 +343,8 @@ lookup =
343343 let v2 = Prelude.lookup key ls
344344 assert (v1 == v2)
345345
346−mapM :: Property
347−mapM =
346+rmapM :: Property
347+rmapM =
348348 forAll (listOf1 (chooseInt (intMin, intMax)))
349349 $ \ls0 -> monadicIO $ action ls0
350350
@@ -352,7 +352,7 @@ mapM =
352352
353353 action ls = do
354354 let addLen x = return $ x + Prelude.length ls
355− fld = FL.mapM addLen FL.sum
355+ fld = FL.rmapM addLen FL.sum
356356 v2 = foldl (+) (Prelude.length ls) ls
357357 v1 <- run $ S.fold fld $ S.fromList ls
358358 assert (v1 == v2)
@@ -455,7 +455,7 @@ main = hspec $
455455 prop "foldMap" Main.foldMap
456456 prop "foldMapM" Main.foldMapM
457457 prop "lookup" Main.lookup
458− prop "mapM" Main.mapM
458+ prop "rmapM" Main.rmapM
459459 prop "teeWithLength" Main.teeWithLength
460460 prop "teeWithMax" Main.teeWithMax
461461 prop "distribute" Main.distribute