@@ -685,19 +685,57 @@ longest :: -- Monad m =>
685 Fold m x a -> Fold m x a -> Fold m x a
685 Fold m x a -> Fold m x a -> Fold m x a
686longest _f1 _f2 = undefined
686longest _f1 _f2 = undefined
688+data ConcatMapState m sa a c
690+ | forall s. C (s -> a -> m (Step s c)) !s (s -> m c)
688-- | Map a 'Fold' returning function on the result of a 'Fold'.
692-- | Map a 'Fold' returning function on the result of a 'Fold'.
690−-- Compare with 'Monad' instance method '>>='. This implementation allows
694+-- Compare with 'Monad' instance method '>>='.
691−-- stream fusion but has quadratic complexity. This can fuse with other
692−-- operations and can be much faster than 'Monad' instance for small number
693−-- (less than 8) of compositions.
695−-- /Unimplemented/
696+-- >>> Stream.fold (concatMap (flip Fold.takeLE Fold.sum) (Fold.rmapM (return . fromJust) Fold.head)) $ Stream.fromList [10,9..1]
697{-# INLINE concatMap #-}
701{-# INLINE concatMap #-}
698−concatMap :: -- Monad m =>
699− (b -> Fold m a c) -> Fold m a b -> Fold m a c
702+concatMap :: Monad m => (b -> Fold m a c) -> Fold m a b -> Fold m a c
700−concatMap _func _fld = undefined
703+concatMap f (Fold stepa initiala extracta) = Fold stepc initialc extractc
708+ Partial s -> return $ Partial (B s)
709+ Done b -> initInnerFold (f b)
714+ Partial s1 -> return $ Partial (B s1)
715+ Done b -> initInnerFold (f b)
717+ stepc (C stepInner s extractInner) a = do
720+ Partial sc -> Partial (C stepInner sc extractInner)
726+ extractc (C _ sInner extractInner) = extractInner sInner
728+ initInnerFold (Fold step i e) = do
731+ Partial s -> Partial (C step s e)
734+ initExtract (Fold _ i e) = do
702-- | Combines the outputs of the folds (the type @b@) using their 'Semigroup'
740-- | Combines the outputs of the folds (the type @b@) using their 'Semigroup'
703-- instances.
741-- instances.