@@ -1847,22 +1847,26 @@ partitionBy f = partitionByM (return . f)
1849-- @since 0.7.0
1849-- @since 0.7.0
1850−demux_ :: (Monad m, Ord k) => Map k (Fold m a ()) -> Fold m (k, a) ()
1850+demux_ :: (Monad m, Ord k) => (a -> k) -> Map k (Fold m a ()) -> Fold m a ()
1851−demux_ kv = Fold step initial extract
1851+demux_ f kv = Fold step initial extract
1855 initial = return ()
1855 initial = return ()
1856− step () (k, a) =
1856+ step () a =
1857 -- XXX should we raise an exception in Nothing case?
1857 -- XXX should we raise an exception in Nothing case?
1858 -- Ideally we should enforce that it is a total map over k so that look
1858 -- Ideally we should enforce that it is a total map over k so that look
1859 -- up never fails
1859 -- up never fails
1860− case Map.lookup k kv of
1860+ case Map.lookup (f a) kv of
1861 Nothing -> return ()
1861 Nothing -> return ()
1862 Just (Fold step' initial' extract') ->
1862 Just (Fold step' initial' extract') ->
1863 initial' >>= \x -> step' x a >>= extract'
1863 initial' >>= \x -> step' x a >>= extract'
1864 extract = return
1864 extract = return
1866+-- XXX instead of a Map we could yield the results as a pure stream as they
1867+-- complete. We could then concatMap the fold results to implement the
1868+-- windowing combinators.
1866-- | Split the input stream based on a key field and fold each split using the
1870-- | Split the input stream based on a key field and fold each split using the
1867-- given fold. Useful for map/reduce, bucketizing the input in different bins
1871-- given fold. Useful for map/reduce, bucketizing the input in different bins
1868-- or for generating histograms.
1872-- or for generating histograms.
@@ -1874,14 +1878,15 @@ demux_ kv = Fold step initial extract
1876-- @since 0.7.0
1880-- @since 0.7.0
1877−classify :: (Monad m, Ord k) => Fold m a b -> Fold m (k, a) (Map k b)
1881+classify :: (Monad m, Ord k) => (a -> k) -> Fold m a b -> Fold m a (Map k b)
1878−classify (Fold step initial extract) = Fold step' initial' extract'
1882+classify f (Fold step initial extract) = Fold step' initial' extract'
1882 initial' = return Map.empty
1886 initial' = return Map.empty
1883− step' kv (k, a) =
1887+ step' kv a =
1884− case Map.lookup k kv of
1889+ in case Map.lookup k kv of
1885 Nothing -> do
1890 Nothing -> do
1886 x <- initial
1891 x <- initial
1887 r <- step x a
1892 r <- step x a