@@ -1482,6 +1482,10 @@ partition = partitionBy id
1482-- XXX If we use Refold in it, it can perhaps fuse/be more efficient. For
1482-- XXX If we use Refold in it, it can perhaps fuse/be more efficient. For
1483-- example we can store just the result rather than storing the whole fold in
1483-- example we can store just the result rather than storing the whole fold in
1484-- the Map.
1484-- the Map.
1486+-- Note: There are separate functions to determine Key and Fold from the input
1487+-- because key is to be determined on each input whereas fold is to be
1488+-- determined only once for a key.
1486-- | In a key value stream, fold values corresponding to each key with a key
1490-- | In a key value stream, fold values corresponding to each key with a key
1487-- specific fold. The fold returns the fold result as the second component of
1491-- specific fold. The fold returns the fold result as the second component of
@@ -1497,7 +1501,7 @@ partition = partitionBy id
1497{-# INLINE demuxScanWith #-}
1501{-# INLINE demuxScanWith #-}
1498demuxScanWith :: (Monad m, IsMap f, Traversable f) =>
1502demuxScanWith :: (Monad m, IsMap f, Traversable f) =>
1499 (a -> Key f)
1503 (a -> Key f)
1500− -> (Key f -> m (Fold m a b))
1504+ -> (a -> m (Fold m a b))
1501 -> Fold m a (m (f b), Maybe (Key f, b))
1505 -> Fold m a (m (f b), Maybe (Key f, b))
1502demuxScanWith getKey getFold = fmap extract $ foldlM' step initial
1506demuxScanWith getKey getFold = fmap extract $ foldlM' step initial
@@ -1521,9 +1525,10 @@ demuxScanWith getKey getFold = fmap extract $ foldlM' step initial
1522 step (Tuple' kv _) a = do
1526 step (Tuple' kv _) a = do
1523 let k = getKey a
1527 let k = getKey a
1525 case IsMap.mapLookup k kv of
1528 case IsMap.mapLookup k kv of
1526− Nothing -> runFold kv fld (k, a)
1531+ runFold kv fld (k, a)
1527 Just f -> runFold kv f (k, a)
1532 Just f -> runFold kv f (k, a)
1529 extract (Tuple' kv x) = (Prelude.mapM f kv, x)
1534 extract (Tuple' kv x) = (Prelude.mapM f kv, x)
@@ -1544,7 +1549,7 @@ demuxScanWith getKey getFold = fmap extract $ foldlM' step initial
1544{-# INLINE demuxScanMutWith #-}
1549{-# INLINE demuxScanMutWith #-}
1545demuxScanMutWith :: (MonadIO m, IsMap f, Traversable f) =>
1550demuxScanMutWith :: (MonadIO m, IsMap f, Traversable f) =>
1546 (a -> Key f)
1551 (a -> Key f)
1547− -> (Key f -> m (Fold m a b))
1552+ -> (a -> m (Fold m a b))
1548 -> Fold m a (m (f b), Maybe (Key f, b))
1553 -> Fold m a (m (f b), Maybe (Key f, b))
1549demuxScanMutWith getKey getFold = fmap extract $ foldlM' step initial
1554demuxScanMutWith getKey getFold = fmap extract $ foldlM' step initial
@@ -1586,7 +1591,7 @@ demuxScanMutWith getKey getFold = fmap extract $ foldlM' step initial
1586 let k = getKey a
1591 let k = getKey a
1587 case IsMap.mapLookup k kv of
1592 case IsMap.mapLookup k kv of
1588 Nothing -> do
1593 Nothing -> do
1589− f <- getFold k
1594+ f <- getFold a
1590 initFold kv f (k, a)
1595 initFold kv f (k, a)
1591 Just ref -> do
1596 Just ref -> do
1592 f <- liftIO $ readIORef ref
1597 f <- liftIO $ readIORef ref
@@ -1607,7 +1612,7 @@ demuxScanMutWith getKey getFold = fmap extract $ foldlM' step initial
1608{-# INLINE demuxWith #-}
1613{-# INLINE demuxWith #-}
1609demuxWith :: (Monad m, IsMap f, Traversable f) =>
1614demuxWith :: (Monad m, IsMap f, Traversable f) =>
1610− (a -> Key f) -> (Key f -> m (Fold m a b)) -> Fold m a (f b)
1615+ (a -> Key f) -> (a -> m (Fold m a b)) -> Fold m a (f b)
1611demuxWith getKey getFold =
1616demuxWith getKey getFold =
1613 classifier = demuxScanWith getKey getFold
1618 classifier = demuxScanWith getKey getFold
@@ -1625,7 +1630,7 @@ demuxWith getKey getFold =
1626{-# INLINE demuxMutWith #-}
1631{-# INLINE demuxMutWith #-}
1627demuxMutWith :: (MonadIO m, IsMap f, Traversable f) =>
1632demuxMutWith :: (MonadIO m, IsMap f, Traversable f) =>
1628− (a -> Key f) -> (Key f -> m (Fold m a b)) -> Fold m a (f b)
1633+ (a -> Key f) -> (a -> m (Fold m a b)) -> Fold m a (f b)
1629demuxMutWith getKey getFold =
1634demuxMutWith getKey getFold =
1631 classifier = demuxScanMutWith getKey getFold
1636 classifier = demuxScanMutWith getKey getFold
@@ -1654,7 +1659,7 @@ demuxMutWith getKey getFold =
1654{-# INLINE demux #-}
1659{-# INLINE demux #-}
1655demux :: (Monad m, IsMap f, Traversable f) =>
1660demux :: (Monad m, IsMap f, Traversable f) =>
1656 (Key f -> m (Fold m a b)) -> Fold m (Key f, a) (f b)
1661 (Key f -> m (Fold m a b)) -> Fold m (Key f, a) (f b)
1657−demux f = demuxWith fst (fmap (lmap snd) . f)
1662+demux f = demuxWith fst (\(k, _) -> fmap (lmap snd) (f k))
1659------------------------------------------------------------------------------
1664------------------------------------------------------------------------------
1660-- Classify: Like demux but uses the same fold for all keys.
1665-- Classify: Like demux but uses the same fold for all keys.