@@ -54,8 +54,8 @@ import Prelude hiding (const, map, concatMap, zipWith)
56-- >>> import qualified Streamly.Prelude as Stream
56-- >>> import qualified Streamly.Prelude as Stream
57−-- >>> import qualified Streamly.Internal.Data.Stream.IsStream as Stream
57+-- >>> import qualified Streamly.Data.Fold as Fold
58−-- >>> import qualified Streamly.Internal.Data.Unfold as Unfold
58+-- >>> import qualified Streamly.Data.Unfold as Unfold
60------------------------------------------------------------------------------
60------------------------------------------------------------------------------
61-- Monadic Unfolds
61-- Monadic Unfolds
@@ -125,7 +125,7 @@ yieldM m = Unfold step inject
126-- | Discards the unfold input and always returns the argument of 'yield'.
126-- | Discards the unfold input and always returns the argument of 'yield'.
127yield :: Applicative m => b -> Unfold m a b
127yield :: Applicative m => b -> Unfold m a b
128−yield = yieldM . pure
128+yield = yieldM Prelude.. pure
130-- | Outer product discarding the first element.
130-- | Outer product discarding the first element.
@@ -218,7 +218,7 @@ apply u1 u2 = fmap (\(a, b) -> a b) (cross u1 u2)
219instance Monad m => Applicative (Unfold m a) where
219instance Monad m => Applicative (Unfold m a) where
220 {-# INLINE pure #-}
220 {-# INLINE pure #-}
221− pure = const Prelude.. return
221+ pure = yield
223 {-# INLINE (<*>) #-}
223 {-# INLINE (<*>) #-}
224 (<*>) = apply
224 (<*>) = apply
@@ -314,8 +314,6 @@ instance Monad m => Monad (Unfold m a) where
314-- Category
314-- Category
315-------------------------------------------------------------------------------
315-------------------------------------------------------------------------------
317−-- XXX change it to yieldM or change yieldM in Prelude to singletonM
319-- | Lift a monadic function into an unfold generating a singleton stream.
317-- | Lift a monadic function into an unfold generating a singleton stream.
321{-# INLINE singletonM #-}
319{-# INLINE singletonM #-}
@@ -397,20 +395,25 @@ instance Monad m => Category (Unfold m) where
399-------------------------------------------------------------------------------
397-------------------------------------------------------------------------------
400−-- Arrow
398+-- Zipping
401-------------------------------------------------------------------------------
399-------------------------------------------------------------------------------
401+-- | Distribute the input to two unfolds and then zip the outputs to a single
402+-- stream using a monadic zip function.
403−-- | Stops as soon as any of the unfolds stops.
404+-- Stops as soon as any of the unfolds stops.
404{-# INLINE_NORMAL zipWithM #-}
407{-# INLINE_NORMAL zipWithM #-}
405zipWithM :: Monad m
408zipWithM :: 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
407zipWithM f (Unfold step1 inject1) (Unfold step2 inject2) = Unfold step inject
410zipWithM f (Unfold step1 inject1) (Unfold step2 inject2) = Unfold step inject
411− inject (x, y) = do
414+ inject x = do
412 s1 <- inject1 x
415 s1 <- inject1 x
413− s2 <- inject2 y
416+ s2 <- inject2 x
414 return (s1, s2, Nothing)
417 return (s1, s2, Nothing)
416 {-# INLINE_LATE step #-}
419 {-# INLINE_LATE step #-}
@@ -431,23 +434,27 @@ zipWithM f (Unfold step1 inject1) (Unfold step2 inject2) = Unfold step inject
431 Skip s -> return $ Skip (s1, s, Just x)
434 Skip s -> return $ Skip (s1, s, Just x)
432 Stop -> return Stop
435 Stop -> return Stop
434−-- | Divide the input into two unfolds and then zip the outputs to a single
437+-- | Like 'zipWithM' but with a pure zip function.
437−-- @
439+-- >>> square = fmap (\x -> x * x) Unfold.fromList
438−-- S.mapM_ print
440+-- >>> cube = fmap (\x -> x * x * x) Unfold.fromList
439−-- $ S.unfoldMany (UF.zipWith (,) UF.identity (UF.singleton sqrt))
441+-- >>> u = Unfold.zipWith (,) square cube
440−-- $ S.map (\x -> (x,x))
442+-- >>> Unfold.fold u Fold.toList [1..5]
441−-- $ S.fromList [1..10]
443+-- [(1,1),(4,8),(9,27),(16,64),(25,125)]
445+-- > zipWith f = zipWithM (\a b -> return $ f a b)
444-- /Pre-release/
447-- /Pre-release/
446{-# INLINE zipWith #-}
449{-# INLINE zipWith #-}
447zipWith :: Monad m
450zipWith :: 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
449zipWith f = zipWithM (\a b -> return (f a b))
452zipWith f = zipWithM (\a b -> return (f a b))
454+-------------------------------------------------------------------------------
456+-------------------------------------------------------------------------------
452-- XXX There are multiple ways of combining the outputs of two unfolds, we
459-- XXX There are multiple ways of combining the outputs of two unfolds, we
453-- could zip, merge, append and more. What is the preferred way for Arrow
460-- could zip, merge, append and more. What is the preferred way for Arrow
@@ -464,5 +471,5 @@ instance Monad m => Arrow (Unfold m) where
464 arr = singleton
471 arr = singleton
466 {-# INLINE (***) #-}
473 {-# INLINE (***) #-}
467− (***) = zipWith (,)
474+ u1 *** u2 = zipWith (,) (lmap fst u1) (lmap snd u2)