Skip to content

Commit

Permalink
Optimized rewrite rule for $$ foldM
Browse files Browse the repository at this point in the history
This is *not* intended to be a final implementation. While this *is*
optimal, the goal is to have a higher-level helper function which
implements this kind of rewrite rule, and then use that function in all
relevant functions in .List.
  • Loading branch information
snoyberg committed Aug 10, 2014
1 parent b5e7669 commit e2aca7d
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion conduit/Data/Conduit/List.hs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,23 @@ foldM f =
next a = CI.PipeM $ do
!accum' <- f accum a
return (loop accum')
{-# INLINEABLE foldM #-}
{-# INLINEABLE [1] foldM #-}

connectFoldM :: Monad m => Source m a -> (b -> a -> m b) -> b -> m b
connectFoldM (CI.ConduitM src0) f =
go src0
where
go (CI.Done ()) b = return b
go (CI.HaveOutput src _ a) b = do
!b' <- f b a
go src b'
go (CI.NeedInput _ c) b = go (c ()) b
go (CI.Leftover src ()) b = go src b
go (CI.PipeM msrc) b = do
src <- msrc
go src b
{-# INLINE connectFoldM #-}
{-# RULES "$$ foldM" forall src f b. src $$ foldM f b = connectFoldM src f b #-}

-- | A monoidal strict left fold.
--
Expand Down

0 comments on commit e2aca7d

Please sign in to comment.