@@ -484,57 +484,47 @@ mergeBy
484mergeBy cmp = mergeByM (\a b -> return $ cmp a b)
484mergeBy cmp = mergeByM (\a b -> return $ cmp a b)
486-------------------------------------------------------------------------------
486-------------------------------------------------------------------------------
487−-- Intersection of sorted streams ---------------------------------------------
487+-- Intersection of sorted streams
488-------------------------------------------------------------------------------
488-------------------------------------------------------------------------------
490+-- Assuming the streams are sorted in ascending order
489{-# INLINE_NORMAL intersectBySorted #-}
491{-# INLINE_NORMAL intersectBySorted #-}
490−intersectBySorted
492+intersectBySorted :: Monad m
491− :: (MonadIO m, Eq a)
492 => (a -> a -> Ordering) -> Stream m a -> Stream m a -> Stream m a
493 => (a -> a -> Ordering) -> Stream m a -> Stream m a -> Stream m a
493intersectBySorted cmp (Stream stepa ta) (Stream stepb tb) =
494intersectBySorted cmp (Stream stepa ta) (Stream stepb tb) =
494− Stream step (Just ta, Just tb, Nothing, Nothing, Nothing)
495+ Stream step
496+ ( ta -- left stream state
497+ , tb -- right stream state
498+ , Nothing -- left value
499+ , Nothing -- right value
497− {-# INLINE_LATE step #-}
499− -- step 1
504+ {-# INLINE_LATE step #-}
505+ -- step 1, fetch the first value
500− step gst (Just sa, sb, Nothing, b, Nothing) = do
506+ step gst (sa, sb, Nothing, b) = do
501 r <- stepa gst sa
507 r <- stepa gst sa
502 return $ case r of
508 return $ case r of
503− Yield a sa' -> Skip (Just sa', sb, Just a, b, Nothing)
509+ Yield a sa' -> Skip (sa', sb, Just a, b) -- step 2/3
504− Skip sa' -> Skip (Just sa', sb, Nothing, b, Nothing)
510+ Skip sa' -> Skip (sa', sb, Nothing, b)
505 Stop -> Stop
511 Stop -> Stop
507− -- step 2
513+ -- step 2, fetch the second value
508− step gst (sa, Just sb, a, Nothing, Nothing) = do
514+ step gst (sa, sb, a@(Just _), Nothing) = do
509 r <- stepb gst sb
515 r <- stepb gst sb
510 return $ case r of
516 return $ case r of
511− Yield b sb' -> Skip (sa, Just sb', a, Just b, Nothing)
517+ Yield b sb' -> Skip (sa, sb', a, Just b) -- step 3
512− Skip sb' -> Skip (sa, Just sb', a, Nothing, Nothing)
518+ Skip sb' -> Skip (sa, sb', a, Nothing)
513 Stop -> Stop
519 Stop -> Stop
515− -- step 3
521+ -- step 3, compare the two values
516− -- both the values are available compare it
517− step _ (sa, sb, Just a, Just b, Nothing) = do
522+ step _ (sa, sb, Just a, Just b) = do
518 let res = cmp a b
523 let res = cmp a b
519 return $ case res of
524 return $ case res of
520− GT -> Skip (sa, sb, Just a, Nothing, Nothing)
525+ GT -> Skip (sa, sb, Just a, Nothing) -- step 2
521− LT -> Skip (sa, sb, Nothing, Just b, Nothing)
526+ LT -> Skip (sa, sb, Nothing, Just b) -- step 1
522− EQ -> Yield a (sa, sb, Nothing, Just a, Just b) -- step 4
527+ EQ -> Yield a (sa, sb, Nothing, Just b) -- step 1
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
539------------------------------------------------------------------------------
529------------------------------------------------------------------------------
540-- Combine N Streams - unfoldMany
530-- Combine N Streams - unfoldMany