@@ -142,6 +142,7 @@ module Streamly.Internal.Data.Stream.StreamD.Nesting
142 -- | Opposite to compact in ArrayStream
142 -- | Opposite to compact in ArrayStream
143 , splitInnerBy
143 , splitInnerBy
144 , splitInnerBySuffix
144 , splitInnerBySuffix
@@ -482,6 +483,59 @@ mergeBy
482 => (a -> a -> Ordering) -> Stream m a -> Stream m a -> Stream m a
483 => (a -> a -> Ordering) -> Stream m a -> Stream m a -> Stream m a
483mergeBy cmp = mergeByM (\a b -> return $ cmp a b)
484mergeBy cmp = mergeByM (\a b -> return $ cmp a b)
486+-------------------------------------------------------------------------------
487+-- Intersection of sorted streams ---------------------------------------------
488+-------------------------------------------------------------------------------
489+{-# INLINE_NORMAL intersectBySorted #-}
491+ :: (MonadIO m, Eq a)
492+ => (a -> a -> Ordering) -> Stream m a -> Stream m a -> Stream m a
493+intersectBySorted cmp (Stream stepa ta) (Stream stepb tb) =
494+ Stream step (Just ta, Just tb, Nothing, Nothing, Nothing)
497+ {-# INLINE_LATE step #-}
500+ step gst (Just sa, sb, Nothing, b, Nothing) = do
503+ Yield a sa' -> Skip (Just sa', sb, Just a, b, Nothing)
504+ Skip sa' -> Skip (Just sa', sb, Nothing, b, Nothing)
508+ step gst (sa, Just sb, a, Nothing, Nothing) = do
511+ Yield b sb' -> Skip (sa, Just sb', a, Just b, Nothing)
512+ Skip sb' -> Skip (sa, Just sb', a, Nothing, Nothing)
516+ -- both the values are available compare it
517+ step _ (sa, sb, Just a, Just b, Nothing) = do
519+ return $ case res of
520+ GT -> Skip (sa, sb, Just a, Nothing, Nothing)
521+ LT -> Skip (sa, sb, Nothing, Just b, Nothing)
522+ EQ -> Yield a (sa, sb, Nothing, Just a, Just b) -- step 4
526+ step gst (Just sa, Just sb, Nothing, Just _, Just b) = do
530+ if a' == b -- match with prev a
531+ then Yield a' (Just sa', Just sb, Nothing, Just b, Just b) --step 1
532+ else Skip (Just sa', Just sb, Just a', Nothing, Nothing)
534+ Skip sa' -> Skip (Just sa', Just sb, Nothing, Nothing, Nothing)
537+ step _ (_, _, _, _, _) = return Stop
485------------------------------------------------------------------------------
539------------------------------------------------------------------------------
486-- Combine N Streams - unfoldMany
540-- Combine N Streams - unfoldMany
487------------------------------------------------------------------------------
541------------------------------------------------------------------------------