on master help sign in
Change the signature of foldlM' to use a monadic seed.
202f674pranaysashank
.hlint.ignore−1
@@ -15,7 +15,6 @@ src/Streamly/Internal/Data/Stream/StreamDK/Type.hs
15src/Streamly/Internal/Data/Stream/StreamD.hs
15src/Streamly/Internal/Data/Stream/StreamD.hs
16src/Streamly/Internal/Data/Pipe/Types.hs
16src/Streamly/Internal/Data/Pipe/Types.hs
17src/Streamly/Internal/Data/Sink.hs
17src/Streamly/Internal/Data/Sink.hs
18−src/Streamly/Internal/Data/Zipper/Array.hs
19src/Streamly/Internal/Data/Parser/ParserD/Types.hs
18src/Streamly/Internal/Data/Parser/ParserD/Types.hs
20src/Streamly/Internal/Data/Parser/ParserD.hs
19src/Streamly/Internal/Data/Parser/ParserD.hs
21src/Streamly/Internal/Data/Prim/Array/Types.hs
20src/Streamly/Internal/Data/Prim/Array/Types.hs
Changelog.md+5
@@ -1,5 +1,10 @@
1## Unreleased
1## Unreleased
2
2
3+### Breaking changes
4+
5+* Change the signature of `foldlM'` to ensure that the accumulator is
6+ monadic.
7+
3### Bug Fixes
8### Bug Fixes
4
9
5* `accept*` and `connect` APIs in `Streamly.Network.Inet.TCP` and the `accept`
10* `accept*` and `connect` APIs in `Streamly.Network.Inet.TCP` and the `accept`
benchmark/Streamly/Benchmark/FileIO/Stream.hs+1−1
@@ -433,7 +433,7 @@ inspect $ 'chunksOf `hasNoType` ''GroupState
433{-# INLINE chunksOfD #-}
433{-# INLINE chunksOfD #-}
434chunksOfD :: Int -> Handle -> IO Int
434chunksOfD :: Int -> Handle -> IO Int
435chunksOfD n inh =
435chunksOfD n inh =
436− D.foldlM' (\i _ -> return $ i + 1) 0
436+ D.foldlM' (\i _ -> return $ i + 1) (return 0)
437 $ D.groupsOf n (AT.writeNUnsafe n)
437 $ D.groupsOf n (AT.writeNUnsafe n)
438 $ D.fromStreamK (S.unfold FH.read inh)
438 $ D.fromStreamK (S.unfold FH.read inh)
439
439
benchmark/Streamly/Benchmark/Memory/ArrayOps.hs+2−2
@@ -173,13 +173,13 @@ toRevList = S.toRevList
173
173
174foldrMBuild = S.foldrM (\x xs -> xs >>= return . (x :)) (return [])
174foldrMBuild = S.foldrM (\x xs -> xs >>= return . (x :)) (return [])
175foldl'Build = S.foldl' (flip (:)) []
175foldl'Build = S.foldl' (flip (:)) []
176−foldlM'Build = S.foldlM' (\xs x -> return $ x : xs) []
176+foldlM'Build = S.foldlM' (\xs x -> return $ x : xs) (return [])
177
177
178foldrMReduce = S.foldrM (\x xs -> xs >>= return . (x +)) (return 0)
178foldrMReduce = S.foldrM (\x xs -> xs >>= return . (x +)) (return 0)
179foldl'Reduce = S.foldl' (+) 0
179foldl'Reduce = S.foldl' (+) 0
180foldl'ReduceMap = P.fmap (+1) . S.foldl' (+) 0
180foldl'ReduceMap = P.fmap (+1) . S.foldl' (+) 0
181foldl1'Reduce = S.foldl1' (+)
181foldl1'Reduce = S.foldl1' (+)
182−foldlM'Reduce = S.foldlM' (\xs a -> return $ a + xs) 0
182+foldlM'Reduce = S.foldlM' (\xs a -> return $ a + xs) (return 0)
183
183
184last = S.last
184last = S.last
185null = S.null
185null = S.null
benchmark/Streamly/Benchmark/Prelude/Serial.hs+2−2
@@ -383,7 +383,7 @@ foldl1'Reduce = S.foldl1' (+)
383
383
384{-# INLINE foldlM'Reduce #-}
384{-# INLINE foldlM'Reduce #-}
385foldlM'Reduce :: Monad m => SerialT m Int -> m Int
385foldlM'Reduce :: Monad m => SerialT m Int -> m Int
386−foldlM'Reduce = S.foldlM' (\xs a -> return $ a + xs) 0
386+foldlM'Reduce = S.foldlM' (\xs a -> return $ a + xs) (return 0)
387
387
388{-# INLINE last #-}
388{-# INLINE last #-}
389last :: Monad m => SerialT m Int -> m (Maybe Int)
389last :: Monad m => SerialT m Int -> m (Maybe Int)
@@ -539,7 +539,7 @@ foldl'Build = S.foldl' (flip (:)) []
539
539
540{-# INLINE foldlM'Build #-}
540{-# INLINE foldlM'Build #-}
541foldlM'Build :: Monad m => SerialT m Int -> m [Int]
541foldlM'Build :: Monad m => SerialT m Int -> m [Int]
542−foldlM'Build = S.foldlM' (\xs x -> return $ x : xs) []
542+foldlM'Build = S.foldlM' (\xs x -> return $ x : xs) (return [])
543
543
544o_n_heap_elimination_foldl :: Int -> [Benchmark]
544o_n_heap_elimination_foldl :: Int -> [Benchmark]
545o_n_heap_elimination_foldl value =
545o_n_heap_elimination_foldl value =
examples/WordClassifier.hs+1−1
@@ -61,7 +61,7 @@ main = do
61 & S.map toLower -- SerialT IO Char
61 & S.map toLower -- SerialT IO Char
62 & S.words FL.toList -- SerialT IO String
62 & S.words FL.toList -- SerialT IO String
63 & S.filter (all isAlpha) -- SerialT IO String
63 & S.filter (all isAlpha) -- SerialT IO String
64− & S.foldlM' (flip (Map.alterF alter)) Map.empty -- IO (Map String (IORef Int))
64+ & S.foldlM' (flip (Map.alterF alter)) (return Map.empty) -- IO (Map String (IORef Int))
65
65
66 -- Print the top hashmap entries
66 -- Print the top hashmap entries
67 counts <-
67 counts <-
examples/WordCount.hs+1−1
@@ -603,7 +603,7 @@ countArray src = do
603{-# INLINE wc_mwl_parallel #-}
603{-# INLINE wc_mwl_parallel #-}
604wc_mwl_parallel :: Handle -> Int -> IO (V.IOVector Int)
604wc_mwl_parallel :: Handle -> Int -> IO (V.IOVector Int)
605wc_mwl_parallel src n = do
605wc_mwl_parallel src n = do
606− counts <- newCounts
606+ let counts = newCounts
607 S.foldlM' addCounts counts
607 S.foldlM' addCounts counts
608 $ S.aheadly
608 $ S.aheadly
609 $ S.maxThreads numCapabilities
609 $ S.maxThreads numCapabilities
src/Streamly/Internal/Data/Array.hs+1−1
@@ -143,7 +143,7 @@ fromStreamDN limit str = do
143 i <-
143 i <-
144 D.foldlM'
144 D.foldlM'
145 (\i x -> i `seq` liftIO $ writeArray marr i x >> return (i + 1))
145 (\i x -> i `seq` liftIO $ writeArray marr i x >> return (i + 1))
146− 0 $
146+ (return 0) $
147 D.take limit str
147 D.take limit str
148 liftIO $ freezeArray marr 0 i
148 liftIO $ freezeArray marr 0 i
149
149
src/Streamly/Internal/Data/Prim/Array.hs+1−1
@@ -136,7 +136,7 @@ fromStreamDN limit str = do
136 _ <-
136 _ <-
137 D.foldlM'
137 D.foldlM'
138 (\i x -> i `seq` liftIO (writePrimArray marr i x) >> return (i + 1))
138 (\i x -> i `seq` liftIO (writePrimArray marr i x) >> return (i + 1))
139− 0 $
139+ (return 0) $
140 D.take limit str
140 D.take limit str
141 liftIO $ unsafeFreezePrimArray marr
141 liftIO $ unsafeFreezePrimArray marr
142
142
src/Streamly/Internal/Data/SmallArray.hs+1−1
@@ -122,7 +122,7 @@ fromStreamDN limit str = do
122 i <-
122 i <-
123 D.foldlM'
123 D.foldlM'
124 (\i x -> i `seq` liftIO (writeSmallArray marr i x) >> return (i + 1))
124 (\i x -> i `seq` liftIO (writeSmallArray marr i x) >> return (i + 1))
125− 0 $
125+ (return 0) $
126 D.take limit str
126 D.take limit str
127 liftIO $ freezeSmallArray marr 0 i
127 liftIO $ freezeSmallArray marr 0 i
128
128
src/Streamly/Internal/Data/Stream/StreamD/Type.hs+5−3
@@ -480,8 +480,10 @@ foldlx' fstep begin done m =
480
480
481-- XXX implement in terms of foldlMx'?
481-- XXX implement in terms of foldlMx'?
482{-# INLINE_NORMAL foldlM' #-}
482{-# INLINE_NORMAL foldlM' #-}
483−foldlM' :: Monad m => (b -> a -> m b) -> b -> Stream m a -> m b
483+foldlM' :: Monad m => (b -> a -> m b) -> m b -> Stream m a -> m b
484−foldlM' fstep begin (Stream step state) = go SPEC begin state
484+foldlM' fstep mbegin (Stream step state) = do
485+ begin <- mbegin
486+ go SPEC begin state
485 where
487 where
486 {-# INLINE_LATE go #-}
488 {-# INLINE_LATE go #-}
487 go !_ acc st = acc `seq` do
489 go !_ acc st = acc `seq` do
@@ -495,7 +497,7 @@ foldlM' fstep begin (Stream step state) = go SPEC begin state
495
497
496{-# INLINE foldl' #-}
498{-# INLINE foldl' #-}
497foldl' :: Monad m => (b -> a -> b) -> b -> Stream m a -> m b
499foldl' :: Monad m => (b -> a -> b) -> b -> Stream m a -> m b
498−foldl' fstep = foldlM' (\b a -> return (fstep b a))
500+foldl' fstep begin = foldlM' (\b a -> return (fstep b a)) (return begin)
499
501
500-- | Convert a list of pure values to a 'Stream'
502-- | Convert a list of pure values to a 'Stream'
501{-# INLINE_LATE fromList #-}
503{-# INLINE_LATE fromList #-}
src/Streamly/Internal/Data/Stream/StreamK.hs+2−2
@@ -433,8 +433,8 @@ foldlMx' step begin done m = go begin m
433
433
434-- | Like 'foldl'' but with a monadic step function.
434-- | Like 'foldl'' but with a monadic step function.
435{-# INLINE foldlM' #-}
435{-# INLINE foldlM' #-}
436−foldlM' :: (IsStream t, Monad m) => (b -> a -> m b) -> b -> t m a -> m b
436+foldlM' :: (IsStream t, Monad m) => (b -> a -> m b) -> m b -> t m a -> m b
437−foldlM' step begin = foldlMx' step (return begin) return
437+foldlM' step begin = foldlMx' step begin return
438
438
439-- | Lazy left fold to a stream.
439-- | Lazy left fold to a stream.
440{-# INLINE foldlS #-}
440{-# INLINE foldlS #-}
src/Streamly/Internal/Memory/Array/Types.hs+1−1
@@ -706,7 +706,7 @@ fromStreamDN :: forall m a. (MonadIO m, Storable a)
706 => Int -> D.Stream m a -> m (Array a)
706 => Int -> D.Stream m a -> m (Array a)
707fromStreamDN limit str = do
707fromStreamDN limit str = do
708 arr <- liftIO $ newArray limit
708 arr <- liftIO $ newArray limit
709− end <- D.foldlM' fwrite (aEnd arr) $ D.take limit str
709+ end <- D.foldlM' fwrite (return $ aEnd arr) $ D.take limit str
710 return $ arr {aEnd = end}
710 return $ arr {aEnd = end}
711
711
712 where
712 where
src/Streamly/Internal/Memory/ArrayStream.hs+3−3
@@ -158,12 +158,12 @@ spliceArraysLenUnsafe :: (MonadIO m, Storable a)
158 => Int -> SerialT m (Array a) -> m (Array a)
158 => Int -> SerialT m (Array a) -> m (Array a)
159spliceArraysLenUnsafe len buffered = do
159spliceArraysLenUnsafe len buffered = do
160 arr <- liftIO $ A.newArray len
160 arr <- liftIO $ A.newArray len
161− end <- S.foldlM' writeArr (aEnd arr) buffered
161+ end <- S.foldlM' writeArr (return $ aEnd arr) buffered
162 return $ arr {aEnd = end}
162 return $ arr {aEnd = end}
163
163
164 where
164 where
165
165
166− writeArr dst Array{..} =
166+ writeArr dst Array{..} = do
167 liftIO $ withForeignPtr aStart $ \src -> do
167 liftIO $ withForeignPtr aStart $ \src -> do
168 let count = aEnd `minusPtr` src
168 let count = aEnd `minusPtr` src
169 A.memcpy (castPtr dst) (castPtr src) count
169 A.memcpy (castPtr dst) (castPtr src) count
@@ -181,7 +181,7 @@ _spliceArraysBuffered s = do
181spliceArraysRealloced :: forall m a. (MonadIO m, Storable a)
181spliceArraysRealloced :: forall m a. (MonadIO m, Storable a)
182 => SerialT m (Array a) -> m (Array a)
182 => SerialT m (Array a) -> m (Array a)
183spliceArraysRealloced s = do
183spliceArraysRealloced s = do
184− idst <- liftIO $ A.newArray (A.bytesToElemCount (undefined :: a)
184+ let idst = liftIO $ A.newArray (A.bytesToElemCount (undefined :: a)
185 (A.mkChunkSizeKB 4))
185 (A.mkChunkSizeKB 4))
186
186
187 arr <- S.foldlM' A.spliceWithDoubling idst s
187 arr <- S.foldlM' A.spliceWithDoubling idst s
src/Streamly/Internal/Prelude.hs+1−1
@@ -1325,7 +1325,7 @@ foldxM = P.foldlMx'
1325--
1325--
1326-- @since 0.2.0
1326-- @since 0.2.0
1327{-# INLINE foldlM' #-}
1327{-# INLINE foldlM' #-}
1328−foldlM' :: Monad m => (b -> a -> m b) -> b -> SerialT m a -> m b
1328+foldlM' :: Monad m => (b -> a -> m b) -> m b -> SerialT m a -> m b
1329foldlM' step begin m = S.foldlM' step begin $ toStreamS m
1329foldlM' step begin m = S.foldlM' step begin $ toStreamS m
1330
1330
1331------------------------------------------------------------------------------
1331------------------------------------------------------------------------------
test/Main.hs+1−1
@@ -706,7 +706,7 @@ checkScanl'Strictness = do
706 `shouldReturn` "success"
706 `shouldReturn` "success"
707
707
708foldlM'StrictCheck :: IORef Int -> SerialT IO Int -> IO ()
708foldlM'StrictCheck :: IORef Int -> SerialT IO Int -> IO ()
709−foldlM'StrictCheck ref = S.foldlM' (\_ _ -> writeIORef ref 1) ()
709+foldlM'StrictCheck ref = S.foldlM' (\_ _ -> writeIORef ref 1) (return ())
710
710
711#ifdef DEVBUILD
711#ifdef DEVBUILD
712foldxMStrictCheck :: IORef Int -> SerialT IO Int -> IO ()
712foldxMStrictCheck :: IORef Int -> SerialT IO Int -> IO ()
test/Prop.hs+1−1
@@ -374,7 +374,7 @@ concurrentFoldlApplication n =
374 -- XXX we should test empty list case as well
374 -- XXX we should test empty list case as well
375 let list = [0..n]
375 let list = [0..n]
376 stream <- run $
376 stream <- run $
377− sourceUnfoldrM1 n |&. S.foldlM' (\xs x -> return (x : xs)) []
377+ sourceUnfoldrM1 n |&. S.foldlM' (\xs x -> return (x : xs)) (return [])
378 listEquals (==) (reverse stream) list
378 listEquals (==) (reverse stream) list
379
379
380concurrentFoldrApplication :: Word8 -> Property
380concurrentFoldrApplication :: Word8 -> Property