on master help sign in
Change the iterateM implementation to use pure seed and add tests for it
commit149d263b352da49abdea4ef157b2d0fc940d7e11 authorAbhiroop Sarkar <asiamgenius@gmail.com> dateSat 24 Mar 2018, 14:58 (9 years ago) parentc7b9463
3 files changed+18−13
Changelog.md+2−1 src/Streamly/Prelude.hs+7−7 test/Main.hs+9−5
Changelog.md+2−1
@@ -1,4 +1,5 @@
11## Unreleased
2+* Add `iterate`, `iterateM` stream operations
23
34### Bug Fixes
45* Fix Zip/AsyncZip applicative instances to handle applicative injection of function like `pure f <*> s1 <*> s2`
@@ -7,7 +8,7 @@
78
89### Enhancements
910* Make `cons` right associative and provide an operator form `.:` for it
10−* Add `iterateM`, `iterate`, `null`, `tail`, `reverse`, `replicateM`, `scan` stream operations
11+* Add `null`, `tail`, `reverse`, `replicateM`, `scan` stream operations
1112* Improve performance of some stream operations (`foldl`, `dropWhile`)
1213
1314### Bug Fixes
src/Streamly/Prelude.hs+7−7
@@ -126,16 +126,16 @@ each = Prelude.foldr cons nil
126126-- | Iterate a pure function from a seed value, streaming the results forever
127127iterate :: Streaming t => (a -> a) -> a -> t m a
128128iterate step = fromStream . go
129− where
130− go s = scons s (Just (go (step s)))
129+ where
130+ go s = scons s (Just (go (step s)))
131131
132132-- | Iterate a monadic function from a seed value, streaming the results forever
133−iterateM :: (Streaming t, Monad m) => (a -> m a) -> m a -> t m a
133+iterateM :: (Streaming t, Monad m) => (a -> m a) -> a -> t m a
134134iterateM step = fromStream . go
135− where
136− go ms = Stream $ \_ _ yld -> do
137− s <- ms
138− yld s (Just (go (step s)))
135+ where
136+ go s = Stream $ \_ _ yld -> do
137+ a <- step s
138+ yld s (Just (go a))
139139
140140-- | Read lines from an IO Handle into a stream of Strings.
141141fromHandle :: (Streaming t, MonadIO m) => IO.Handle -> t m String
test/Main.hs+9−5
@@ -651,11 +651,15 @@ streamOperations (stream, list, len) = do
651651 return $ str == lst
652652 `shouldReturn` True
653653
654− it "iterate" $ do
655− let lst = take len $ iterate (+ 1) 0
656− str <- A.toList . serially . (A.take len) $ (A.iterate (+ 1) (0 :: Int))
657− return $ str == lst
658− `shouldReturn` True
654+ it "iterate" $
655+ (A.toList . serially . (A.take len) $ (A.iterate (+ 1) (0 :: Int)))
656+ `shouldReturn` (take len $ iterate (+ 1) 0)
657+
658+ it "iterateM" $ do
659+ let addM = (\ y -> return (y + 1))
660+ A.toList . serially . (A.take len) $ A.iterateM addM (0 :: Int)
661+ `shouldReturn` (take len $ iterate (+ 1) 0)
662+
659663
660664 -- Filtering
661665 it "filter all out" $ transform (A.filter (> len)) (filter (> len))