{-# LANGUAGE RankNTypes #-}
-- | Adapter module to work with the <https://hackage.haskell.org/package/foldl foldl> package.
--
-- @since 1.1.16
module Data.Conduit.Foldl where

import Data.Conduit
import Control.Monad.Trans.Class (lift)
import qualified Data.Conduit.List as CL

-- | Convert a left fold into a 'Consumer'. This function is intended
-- to be used with @purely@ from the
-- <https://hackage.haskell.org/package/foldl foldl> package.
--
-- @since 1.1.16
sinkFold :: Monad m => (x -> a -> x) -> x -> (x -> b) -> ConduitT a o m b
sinkFold :: (x -> a -> x) -> x -> (x -> b) -> ConduitT a o m b
sinkFold x -> a -> x
combine x
seed x -> b
extract = (x -> b) -> ConduitT a o m x -> ConduitT a o m b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap x -> b
extract ((x -> a -> x) -> x -> ConduitT a o m x
forall (m :: * -> *) b a o.
Monad m =>
(b -> a -> b) -> b -> ConduitT a o m b
CL.fold x -> a -> x
combine x
seed)

-- | Convert a monadic left fold into a 'Consumer'. This function is
-- intended to be used with @impurely@ from the
-- <https://hackage.haskell.org/package/foldl foldl> package.
--
-- @since 1.1.16
sinkFoldM :: Monad m => (x -> a -> m x) -> m x -> (x -> m b) -> ConduitT a o m b
sinkFoldM :: (x -> a -> m x) -> m x -> (x -> m b) -> ConduitT a o m b
sinkFoldM x -> a -> m x
combine m x
seed x -> m b
extract =
  m b -> ConduitT a o m b
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m b -> ConduitT a o m b) -> (x -> m b) -> x -> ConduitT a o m b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. x -> m b
extract (x -> ConduitT a o m b) -> ConduitT a o m x -> ConduitT a o m b
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< (x -> a -> m x) -> x -> ConduitT a o m x
forall (m :: * -> *) b a o.
Monad m =>
(b -> a -> m b) -> b -> ConduitT a o m b
CL.foldM x -> a -> m x
combine (x -> ConduitT a o m x) -> ConduitT a o m x -> ConduitT a o m x
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< m x -> ConduitT a o m x
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m x
seed