on master help sign in
Merge pull request #35 from hussein-aitlahcen/zipstream-applicative
68b6736Harendra Kumar
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+
1## 0.1.1
6## 0.1.1
2
7
3### Enhancements
8### Enhancements
src/Streamly/Core.hs+4
@@ -25,6 +25,7 @@ module Streamly.Core
25
25
26 -- * Construction
26 -- * Construction
27 , scons
27 , scons
28+ , srepeat
28 , snil
29 , snil
29
30
30 -- * Composition
31 -- * Composition
@@ -217,6 +218,9 @@ type MonadAsync m = (MonadIO m, MonadBaseControl IO m, MonadThrow m)
217scons :: a -> Maybe (Stream m a) -> Stream m a
218scons :: a -> Maybe (Stream m a) -> Stream m a
218scons a r = Stream $ \_ _ yld -> yld a r
219scons a r = Stream $ \_ _ yld -> yld a r
219
220
221+srepeat :: a -> Stream m a
222+srepeat a = let x = scons a (Just x) in x
223+
220snil :: Stream m a
224snil :: Stream m a
221snil = Stream $ \_ stp _ -> stp
225snil = Stream $ \_ stp _ -> stp
222
226
src/Streamly/Streams.hs+2−2
@@ -751,7 +751,7 @@ instance Monad m => Functor (ZipStream m) where
751 in m Nothing stp yield
751 in m Nothing stp yield
752
752
753instance Monad m => Applicative (ZipStream m) where
753instance Monad m => Applicative (ZipStream m) where
754− pure a = ZipStream $ scons a Nothing
754+ pure = ZipStream . srepeat
755 (<*>) = zipWith id
755 (<*>) = zipWith id
756
756
757instance Streaming ZipStream where
757instance Streaming ZipStream where
@@ -838,7 +838,7 @@ instance Monad m => Functor (ZipAsync m) where
838 in m Nothing stp yield
838 in m Nothing stp yield
839
839
840instance MonadAsync m => Applicative (ZipAsync m) where
840instance MonadAsync m => Applicative (ZipAsync m) where
841− pure a = ZipAsync $ scons a Nothing
841+ pure = ZipAsync . srepeat
842 (<*>) = zipAsyncWith id
842 (<*>) = zipAsyncWith id
843
843
844instance Streaming ZipAsync where
844instance Streaming ZipAsync where
test/Main.hs+6−2
@@ -422,8 +422,12 @@ zipOps z zM app = do
422 it "Applicative zip" $
422 it "Applicative zip" $
423 let s1 = adapt $ serially $ foldMapWith (<>) return [1..10]
423 let s1 = adapt $ serially $ foldMapWith (<>) return [1..10]
424 s2 = adapt $ serially $ foldMapWith (<>) return [1..]
424 s2 = adapt $ serially $ foldMapWith (<>) return [1..]
425− in (A.toList . app) ((+) <$> s1 <*> s2)
425+ f = A.toList . app
426+ functorial = f $ (+) <$> s1 <*> s2
427+ applicative = f $ pure (+) <*> s1 <*> s2
426− `shouldReturn` ([2,4..20] :: [Int])
428+ expected = ([2,4..20] :: [Int])
429+ in (,) <$> functorial <*> applicative
430+ `shouldReturn` (expected, expected)
427
431
428timed :: Int -> StreamT IO Int
432timed :: Int -> StreamT IO Int
429timed x = liftIO (threadDelay (x * 100000)) >> return x
433timed x = liftIO (threadDelay (x * 100000)) >> return x