From 42bde27d2b08ddfb79742d7eed6d9d0c34ea0256 Mon Sep 17 00:00:00 2001 From: Hussein Ait Lahcen Date: Thu, 22 Mar 2018 17:56:46 +0100 Subject: [PATCH 1/2] fix: Zip/AsyncZip applicative instances refact: type consistency refact: introduce srepeat --- src/Streamly/Core.hs | 4 ++++ src/Streamly/Streams.hs | 4 ++-- test/Main.hs | 8 ++++++-- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/Streamly/Core.hs b/src/Streamly/Core.hs index 7ed7362f..8a763111 100644 --- a/src/Streamly/Core.hs +++ b/src/Streamly/Core.hs @@ -25,6 +25,7 @@ module Streamly.Core -- * Construction , scons + , srepeat , snil -- * Composition @@ -217,6 +218,9 @@ type MonadAsync m = (MonadIO m, MonadBaseControl IO m, MonadThrow m) scons :: a -> Maybe (Stream m a) -> Stream m a scons a r = Stream $ \_ _ yld -> yld a r +srepeat :: a -> Stream m a +srepeat a = let x = scons a (Just x) in x + snil :: Stream m a snil = Stream $ \_ stp _ -> stp diff --git a/src/Streamly/Streams.hs b/src/Streamly/Streams.hs index fd3a2201..0e410766 100644 --- a/src/Streamly/Streams.hs +++ b/src/Streamly/Streams.hs @@ -751,7 +751,7 @@ instance Monad m => Functor (ZipStream m) where in m Nothing stp yield instance Monad m => Applicative (ZipStream m) where - pure a = ZipStream $ scons a Nothing + pure = ZipStream . srepeat (<*>) = zipWith id instance Streaming ZipStream where @@ -838,7 +838,7 @@ instance Monad m => Functor (ZipAsync m) where in m Nothing stp yield instance MonadAsync m => Applicative (ZipAsync m) where - pure a = ZipAsync $ scons a Nothing + pure = ZipAsync . srepeat (<*>) = zipAsyncWith id instance Streaming ZipAsync where diff --git a/test/Main.hs b/test/Main.hs index e740b876..f8dba955 100644 --- a/test/Main.hs +++ b/test/Main.hs @@ -422,8 +422,12 @@ zipOps z zM app = do it "Applicative zip" $ let s1 = adapt $ serially $ foldMapWith (<>) return [1..10] s2 = adapt $ serially $ foldMapWith (<>) return [1..] - in (A.toList . app) ((+) <$> s1 <*> s2) - `shouldReturn` ([2,4..20] :: [Int]) + f = A.toList . app + functorial = f $ (+) <$> s1 <*> s2 + applicative = f $ pure (+) <*> s1 <*> s2 + expected = ([2,4..20] :: [Int]) + in (,) <$> functorial <*> applicative + `shouldReturn` (expected, expected) timed :: Int -> StreamT IO Int timed x = liftIO (threadDelay (x * 100000)) >> return x From 20b2d97bc78f953127432151110d7c748b9ba182 Mon Sep 17 00:00:00 2001 From: Hussein Ait Lahcen Date: Thu, 22 Mar 2018 18:31:08 +0100 Subject: [PATCH 2/2] add: 'Unreleased' changelog section docs: typo --- Changelog.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Changelog.md b/Changelog.md index b8c9865a..d2e3d510 100644 --- a/Changelog.md +++ b/Changelog.md @@ -1,3 +1,8 @@ +## Unreleased + +### Bug Fixes +* Fix Zip/AsyncZip applicative instances to handle applicative injection of function like `pure f <*> s1 <*> s2` + ## 0.1.1 ### Enhancements