Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add evalListT to get results from ListT as a list #184

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/Pipes.hs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ module Pipes (
-- * ListT
, ListT(..)
, runListT
, evalListT
, Enumerable(..)

-- * Utilities
Expand Down Expand Up @@ -78,7 +79,9 @@ import Control.Monad.Writer (MonadWriter(..))
import Control.Monad.Zip (MonadZip(..))
import Pipes.Core
import Pipes.Internal (Proxy(..))
import Pipes.Lift (writerP, execWriterP)
import qualified Data.Foldable as F
import qualified Control.Monad as M

#if MIN_VERSION_base(4,8,0)
import Control.Applicative (Alternative(..))
Expand Down Expand Up @@ -566,6 +569,14 @@ runListT :: Monad m => ListT m a -> m ()
runListT l = runEffect (enumerate (l >> mzero))
{-# INLINABLE runListT #-}

-- | Run a self-contained `ListT` computation and accumulate the produced values as a list in the underlying monad
evalListT :: Monad m => ListT m a -> m [a]
evalListT listT =
runEffect
$ execWriterP
$ (writerP $ (\() -> ((), [])) <$> enumerate listT)
>-> (M.forever $ pass ((\element -> ((), (element :))) <$> await))

{-| 'Enumerable' generalizes 'Data.Foldable.Foldable', converting effectful
containers to 'ListT's.

Expand Down