@@ -24,14 +24,18 @@ module Streamly.Internal.Data.Unfold.Type
24 , ConcatState (..)
24 , ConcatState (..)
27 , apSequence
28 , apSequence
28 , apDiscardSnd
29 , apDiscardSnd
33 , concatMapM
36 , concatMapM
34 , concatMap
37 , concatMap
36 , zipWithM
40 , zipWithM
@@ -144,11 +148,13 @@ apDiscardSnd (Unfold _step1 _inject1) (Unfold _step2 _inject2) = undefined
144data Cross a s1 b s2 = CrossOuter a s1 | CrossInner a s1 b s2
148data Cross a s1 b s2 = CrossOuter a s1 | CrossInner a s1 b s2
146-- | Create a cross product (vector product or cartesian product) of the
150-- | Create a cross product (vector product or cartesian product) of the
147−-- output streams of two unfolds.
151+-- output streams of two unfolds using a monadic combining function.
149−{-# INLINE_NORMAL cross #-}
154+{-# INLINE_NORMAL crossWithM #-}
155+crossWithM :: Monad m =>
150−cross :: Monad m => Unfold m a b -> Unfold m a c -> Unfold m a (b, c)
156+ (b -> c -> m d) -> Unfold m a b -> Unfold m a c -> Unfold m a d
151−cross (Unfold step1 inject1) (Unfold step2 inject2) = Unfold step inject
157+crossWithM f (Unfold step1 inject1) (Unfold step2 inject2) = Unfold step inject
@@ -168,10 +174,36 @@ cross (Unfold step1 inject1) (Unfold step2 inject2) = Unfold step inject
169 step (CrossInner a s1 b s2) = do
175 step (CrossInner a s1 b s2) = do
170 r <- step2 s2
176 r <- step2 s2
171− return $ case r of
177+ case r of
172− Yield c s -> Yield (b, c) (CrossInner a s1 b s)
178+ Yield c s -> f b c >>= \d -> return $ Yield d (CrossInner a s1 b s)
173− Skip s -> Skip (CrossInner a s1 b s)
179+ Skip s -> return $ Skip (CrossInner a s1 b s)
174− Stop -> Skip (CrossOuter a s1)
180+ Stop -> return $ Skip (CrossOuter a s1)
182+-- | Like 'crossWithM' but uses a pure combining function.
184+-- > crossWith f = crossWithM (\b c -> return $ f b c)
187+{-# INLINE crossWith #-}
188+crossWith :: Monad m =>
189+ (b -> c -> d) -> Unfold m a b -> Unfold m a c -> Unfold m a d
190+crossWith f = crossWithM (\b c -> return $ f b c)
192+-- | See 'crossWith'.
194+-- > cross = crossWith (,)
196+-- To cross the streams from a tuple we can write:
199+-- crossProduct :: Monad m => Unfold m a b -> Unfold m c d -> Unfold m (a, c) (b, d)
200+-- crossProduct u1 u2 = cross (lmap fst u1) (lmap snd u2)
204+{-# INLINE_NORMAL cross #-}
205+cross :: Monad m => Unfold m a b -> Unfold m a c -> Unfold m a (b, c)
206+cross = crossWith (,)
176apply :: Monad m => Unfold m a (b -> c) -> Unfold m a b -> Unfold m a c
208apply :: Monad m => Unfold m a (b -> c) -> Unfold m a b -> Unfold m a c
177apply u1 u2 = fmap (\(a, b) -> a b) (cross u1 u2)
209apply u1 u2 = fmap (\(a, b) -> a b) (cross u1 u2)