on master help sign in
Merge pull request #35 from hussein-aitlahcen/zipstream-applicative

fix: Zip/AsyncZip applicative instances

commit68b6736794db0d31ab1bd9e0ed7339ae499462c8 authorHarendra Kumar <harendra.kumar@gmail.com> dateFri 23 Mar 2018, 00:05 (9 years ago) parentf5adde820b2d97
4 files changed+17−4
Changelog.md+5 src/Streamly/Core.hs+4 src/Streamly/Streams.hs+2−2 test/Main.hs+6−2
Changelog.md+5
@@ -1,3 +1,8 @@
1+## Unreleased
2+
3+### Bug Fixes
4+* Fix Zip/AsyncZip applicative instances to handle applicative injection of function like `pure f <*> s1 <*> s2`
5+
16## 0.1.1
27
38### Enhancements
src/Streamly/Core.hs+4
@@ -25,6 +25,7 @@ module Streamly.Core
2525
2626 -- * Construction
2727 , scons
28+ , srepeat
2829 , snil
2930
3031 -- * Composition
@@ -217,6 +218,9 @@ type MonadAsync m = (MonadIO m, MonadBaseControl IO m, MonadThrow m)
217218scons :: a -> Maybe (Stream m a) -> Stream m a
218219scons a r = Stream $ \_ _ yld -> yld a r
219220
221+srepeat :: a -> Stream m a
222+srepeat a = let x = scons a (Just x) in x
223+
220224snil :: Stream m a
221225snil = Stream $ \_ stp _ -> stp
222226
src/Streamly/Streams.hs+2−2
@@ -751,7 +751,7 @@ instance Monad m => Functor (ZipStream m) where
751751 in m Nothing stp yield
752752
753753instance Monad m => Applicative (ZipStream m) where
754− pure a = ZipStream $ scons a Nothing
754+ pure = ZipStream . srepeat
755755 (<*>) = zipWith id
756756
757757instance Streaming ZipStream where
@@ -838,7 +838,7 @@ instance Monad m => Functor (ZipAsync m) where
838838 in m Nothing stp yield
839839
840840instance MonadAsync m => Applicative (ZipAsync m) where
841− pure a = ZipAsync $ scons a Nothing
841+ pure = ZipAsync . srepeat
842842 (<*>) = zipAsyncWith id
843843
844844instance Streaming ZipAsync where
test/Main.hs+6−2
@@ -422,8 +422,12 @@ zipOps z zM app = do
422422 it "Applicative zip" $
423423 let s1 = adapt $ serially $ foldMapWith (<>) return [1..10]
424424 s2 = adapt $ serially $ foldMapWith (<>) return [1..]
425− in (A.toList . app) ((+) <$> s1 <*> s2)
426− `shouldReturn` ([2,4..20] :: [Int])
425+ f = A.toList . app
426+ functorial = f $ (+) <$> s1 <*> s2
427+ applicative = f $ pure (+) <*> s1 <*> s2
428+ expected = ([2,4..20] :: [Int])
429+ in (,) <$> functorial <*> applicative
430+ `shouldReturn` (expected, expected)
427431
428432timed :: Int -> StreamT IO Int
429433timed x = liftIO (threadDelay (x * 100000)) >> return x