@@ -484,57 +484,47 @@ mergeBy
484484mergeBy cmp = mergeByM (\ a b -> return $ cmp a b)
485485
486486-------------------------------------------------------------------------------
487- -- Intersection of sorted streams ---------------------------------------------
487+ -- Intersection of sorted streams
488488-------------------------------------------------------------------------------
489+
490+ -- Assuming the streams are sorted in ascending order
489491{-# INLINE_NORMAL intersectBySorted #-}
490- intersectBySorted
491- :: (MonadIO m , Eq a )
492+ intersectBySorted :: Monad m
492493 => (a -> a -> Ordering ) -> Stream m a -> Stream m a -> Stream m a
493494intersectBySorted 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
500+ )
495501
496502 where
497- {-# INLINE_LATE step #-}
498503
499- -- step 1
500- step gst (Just sa, sb, Nothing , b, Nothing ) = do
504+ {-# INLINE_LATE step #-}
505+ -- step 1, fetch the first value
506+ step gst (sa, sb, Nothing , b) = do
501507 r <- stepa gst sa
502508 return $ case r of
503- Yield a sa' -> Skip (Just sa', sb, Just a, b, Nothing )
504- Skip sa' -> Skip (Just sa', sb, Nothing , b, Nothing )
509+ Yield a sa' -> Skip (sa', sb, Just a, b) -- step 2/3
510+ Skip sa' -> Skip (sa', sb, Nothing , b)
505511 Stop -> Stop
506512
507- -- step 2
508- step gst (sa, Just sb, a, Nothing , Nothing ) = do
513+ -- step 2, fetch the second value
514+ step gst (sa, sb, a@ ( Just _) , Nothing ) = do
509515 r <- stepb gst sb
510516 return $ case r of
511- Yield b sb' -> Skip (sa, Just sb', a, Just b, Nothing )
512- Skip sb' -> Skip (sa, Just sb', a, Nothing , Nothing )
517+ Yield b sb' -> Skip (sa, sb', a, Just b) -- step 3
518+ Skip sb' -> Skip (sa, sb', a, Nothing )
513519 Stop -> Stop
514520
515- -- step 3
516- -- both the values are available compare it
517- step _ (sa, sb, Just a, Just b, Nothing ) = do
521+ -- step 3, compare the two values
522+ step _ (sa, sb, Just a, Just b) = do
518523 let res = cmp a b
519524 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
523-
524- -- step 4
525- -- Matching element
526- step gst (Just sa, Just sb, Nothing , Just _, Just b) = do
527- r1 <- stepa gst sa
528- return $ case r1 of
529- Yield a' sa' -> 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 )
533-
534- Skip sa' -> Skip (Just sa', Just sb, Nothing , Nothing , Nothing )
535- Stop -> Stop
536-
537- step _ (_, _, _, _, _) = return Stop
525+ GT -> Skip (sa, sb, Just a, Nothing ) -- step 2
526+ LT -> Skip (sa, sb, Nothing , Just b) -- step 1
527+ EQ -> Yield a (sa, sb, Nothing , Just b) -- step 1
538528
539529------------------------------------------------------------------------------
540530-- Combine N Streams - unfoldMany
0 commit comments