on master help sign in
Change signatures of zipWith/zipWithM
commit8af1c368c60a4ca2ade856e463f4823daa7d3d37 authorHarendra Kumar <harendra@composewell.com> dateWed 24 Mar 2021, 02:38 (6 years ago) parentc5b2ad3
5 files changed+32−45
benchmark/Streamly/Benchmark/Data/Unfold.hs+3−3 src/Streamly/Data/Unfold.hs−1 src/Streamly/Internal/Data/Unfold.hs−19 src/Streamly/Internal/Data/Unfold/Type.hs+28−21 test/Streamly/Test/Data/Unfold.hs+1−1
benchmark/Streamly/Benchmark/Data/Unfold.hs+3−3
@@ -384,7 +384,7 @@ dropWhileMFalse size start =
384384{-# INLINE zipWith #-}
385385zipWith :: Monad m => Int -> Int -> m ()
386386zipWith size start =
387− drainProductDefault (size + start) (UF.zipWith (+)) (start, start + 1)
387+ drainProductDefault (size + start) (UF.zipWith (+)) start
388388
389389{-# INLINE zipWithM #-}
390390zipWithM :: Monad m => Int -> Int -> m ()
@@ -392,12 +392,12 @@ zipWithM size start =
392392 drainProductDefault
393393 (size + start)
394394 (UF.zipWithM (\a b -> return $ a + b))
395− (start, start + 1)
395+ start
396396
397397{-# INLINE teeZipWith #-}
398398teeZipWith :: Monad m => Int -> Int -> m ()
399399teeZipWith size start =
400− drainProductDefault (size + start) (UF.teeZipWith (+)) start
400+ drainProductDefault (size + start) (UF.zipWith (+)) start
401401
402402-------------------------------------------------------------------------------
403403-- Applicative
src/Streamly/Data/Unfold.hs−1
@@ -94,7 +94,6 @@ module Streamly.Data.Unfold
9494 , dropWhileM
9595
9696 -- ** Zipping
97− , zipWithM
9897 , zipWith
9998
10099 -- ** Cross Product
src/Streamly/Internal/Data/Unfold.hs−19
@@ -159,7 +159,6 @@ module Streamly.Internal.Data.Unfold
159159 -- ** Zipping
160160 , zipWithM
161161 , zipWith
162− , teeZipWith
163162
164163 -- ** Cross product
165164 , crossWithM
@@ -881,24 +880,6 @@ fromProducer = Unfold step (return . FromSVarRead)
881880
882881 step (FromSVarInit _) = undefined
883882
884−-------------------------------------------------------------------------------
885−-- Zipping
886−-------------------------------------------------------------------------------
887−
888−-- | Distribute the input to two unfolds and then zip the outputs to a single
889−-- stream.
890−--
891−-- @
892−-- S.mapM_ print $ S.unfoldMany (UF.teeZipWith (,) UF.identity (UF.singleton sqrt)) $ S.fromList [1..10]
893−-- @
894−--
895−-- /Pre-release/
896−--
897−{-# INLINE_NORMAL teeZipWith #-}
898−teeZipWith :: Monad m
899− => (a -> b -> c) -> Unfold m x a -> Unfold m x b -> Unfold m x c
900−teeZipWith f unf1 unf2 = lmap (\x -> (x,x)) $ zipWith f unf1 unf2
901−
902883------------------------------------------------------------------------------
903884-- Exceptions
904885------------------------------------------------------------------------------
src/Streamly/Internal/Data/Unfold/Type.hs+28−21
@@ -54,8 +54,8 @@ import Prelude hiding (const, map, concatMap, zipWith)
5454-- $setup
5555-- >>> :m
5656-- >>> import qualified Streamly.Prelude as Stream
57−-- >>> import qualified Streamly.Internal.Data.Stream.IsStream as Stream
58−-- >>> import qualified Streamly.Internal.Data.Unfold as Unfold
57+-- >>> import qualified Streamly.Data.Fold as Fold
58+-- >>> import qualified Streamly.Data.Unfold as Unfold
5959
6060------------------------------------------------------------------------------
6161-- Monadic Unfolds
@@ -125,7 +125,7 @@ yieldM m = Unfold step inject
125125
126126-- | Discards the unfold input and always returns the argument of 'yield'.
127127yield :: Applicative m => b -> Unfold m a b
128−yield = yieldM . pure
128+yield = yieldM Prelude.. pure
129129
130130-- | Outer product discarding the first element.
131131--
@@ -218,7 +218,7 @@ apply u1 u2 = fmap (\(a, b) -> a b) (cross u1 u2)
218218--
219219instance Monad m => Applicative (Unfold m a) where
220220 {-# INLINE pure #-}
221− pure = const Prelude.. return
221+ pure = yield
222222
223223 {-# INLINE (<*>) #-}
224224 (<*>) = apply
@@ -314,8 +314,6 @@ instance Monad m => Monad (Unfold m a) where
314314-- Category
315315-------------------------------------------------------------------------------
316316
317−-- XXX change it to yieldM or change yieldM in Prelude to singletonM
318−--
319317-- | Lift a monadic function into an unfold generating a singleton stream.
320318--
321319{-# INLINE singletonM #-}
@@ -397,20 +395,25 @@ instance Monad m => Category (Unfold m) where
397395-}
398396
399397-------------------------------------------------------------------------------
400−-- Arrow
398+-- Zipping
401399-------------------------------------------------------------------------------
402400
403−-- | Stops as soon as any of the unfolds stops.
401+-- | Distribute the input to two unfolds and then zip the outputs to a single
402+-- stream using a monadic zip function.
403+--
404+-- Stops as soon as any of the unfolds stops.
405+--
406+-- /Pre-release/
404407{-# INLINE_NORMAL zipWithM #-}
405408zipWithM :: Monad m
406− => (a -> b -> m c) -> Unfold m x a -> Unfold m y b -> Unfold m (x, y) c
409+ => (b -> c -> m d) -> Unfold m a b -> Unfold m a c -> Unfold m a d
407410zipWithM f (Unfold step1 inject1) (Unfold step2 inject2) = Unfold step inject
408411
409412 where
410413
411− inject (x, y) = do
414+ inject x = do
412415 s1 <- inject1 x
413− s2 <- inject2 y
416+ s2 <- inject2 x
414417 return (s1, s2, Nothing)
415418
416419 {-# INLINE_LATE step #-}
@@ -431,23 +434,27 @@ zipWithM f (Unfold step1 inject1) (Unfold step2 inject2) = Unfold step inject
431434 Skip s -> return $ Skip (s1, s, Just x)
432435 Stop -> return Stop
433436
434−-- | Divide the input into two unfolds and then zip the outputs to a single
435−-- stream.
437+-- | Like 'zipWithM' but with a pure zip function.
436438--
437−-- @
438−-- S.mapM_ print
439−-- $ S.unfoldMany (UF.zipWith (,) UF.identity (UF.singleton sqrt))
440−-- $ S.map (\x -> (x,x))
441−-- $ S.fromList [1..10]
442−-- @
439+-- >>> square = fmap (\x -> x * x) Unfold.fromList
440+-- >>> cube = fmap (\x -> x * x * x) Unfold.fromList
441+-- >>> u = Unfold.zipWith (,) square cube
442+-- >>> Unfold.fold u Fold.toList [1..5]
443+-- [(1,1),(4,8),(9,27),(16,64),(25,125)]
444+--
445+-- > zipWith f = zipWithM (\a b -> return $ f a b)
443446--
444447-- /Pre-release/
445448--
446449{-# INLINE zipWith #-}
447450zipWith :: Monad m
448− => (a -> b -> c) -> Unfold m x a -> Unfold m y b -> Unfold m (x, y) c
451+ => (b -> c -> d) -> Unfold m a b -> Unfold m a c -> Unfold m a d
449452zipWith f = zipWithM (\a b -> return (f a b))
450453
454+-------------------------------------------------------------------------------
455+-- Arrow
456+-------------------------------------------------------------------------------
457+
451458{-
452459-- XXX There are multiple ways of combining the outputs of two unfolds, we
453460-- could zip, merge, append and more. What is the preferred way for Arrow
@@ -464,5 +471,5 @@ instance Monad m => Arrow (Unfold m) where
464471 arr = singleton
465472
466473 {-# INLINE (***) #-}
467− (***) = zipWith (,)
474+ u1 *** u2 = zipWith (,) (lmap fst u1) (lmap snd u2)
468475-}
test/Streamly/Test/Data/Unfold.hs+1−1
@@ -319,7 +319,7 @@ zipWithM =
319319 unf2 = UF.enumerateFromToIntegral 20
320320 fA = applyFun2 f :: Int -> Int -> Int
321321 fM a b = modify (+ 1) >> return (fA a b)
322− unf = UF.zipWithM fM unf1 unf2
322+ unf = UF.zipWithM fM (UF.lmap fst unf1) (UF.lmap snd unf2)
323323 lst = Prelude.zipWith fA [1 .. 10] [1 .. 20]
324324 in testUnfoldMD unf (1, 1) 0 10 lst
325325