@@ -1847,22 +1847,26 @@ partitionBy f = partitionByM (return . f)
18471847-- @
18481848--
18491849-- @since 0.7.0
1850−demux_ :: (Monad m, Ord k) => Map k (Fold m a ()) -> Fold m (k, a) ()
1851−demux_ kv = Fold step initial extract
1850+demux_ :: (Monad m, Ord k) => (a -> k) -> Map k (Fold m a ()) -> Fold m a ()
1851+demux_ f kv = Fold step initial extract
18521852
18531853 where
18541854
18551855 initial = return ()
1856− step () (k, a) =
1856+ step () a =
18571857 -- XXX should we raise an exception in Nothing case?
18581858 -- Ideally we should enforce that it is a total map over k so that look
18591859 -- up never fails
1860− case Map.lookup k kv of
1860+ case Map.lookup (f a) kv of
18611861 Nothing -> return ()
18621862 Just (Fold step' initial' extract') ->
18631863 initial' >>= \x -> step' x a >>= extract'
18641864 extract = return
18651865
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.
1869+--
18661870-- | Split the input stream based on a key field and fold each split using the
18671871-- given fold. Useful for map/reduce, bucketizing the input in different bins
18681872-- or for generating histograms.
@@ -1874,14 +1878,15 @@ demux_ kv = Fold step initial extract
18741878-- @
18751879--
18761880-- @since 0.7.0
1877−classify :: (Monad m, Ord k) => Fold m a b -> Fold m (k, a) (Map k b)
1878−classify (Fold step initial extract) = Fold step' initial' extract'
1881+classify :: (Monad m, Ord k) => (a -> k) -> Fold m a b -> Fold m a (Map k b)
1882+classify f (Fold step initial extract) = Fold step' initial' extract'
18791883
18801884 where
18811885
18821886 initial' = return Map.empty
1883− step' kv (k, a) =
1884− case Map.lookup k kv of
1887+ step' kv a =
1888+ let k = f a
1889+ in case Map.lookup k kv of
18851890 Nothing -> do
18861891 x <- initial
18871892 r <- step x a