Higher-level functions to interact with the elements of a stream. Most of these are based on list functions.
Note that these functions all deal with individual elements of a stream as a
sort of "black box", where there is no introspection of the contained
elements. Values such as ByteString
and Text
will likely need to be
treated specially to deal with their contents properly (Word8
and Char
,
respectively). See the Data.Conduit.Binary and Data.Conduit.Text
modules.
- sourceList :: Resource m => [a] -> Source m a
- fold :: Resource m => (b -> a -> b) -> b -> Sink a m b
- take :: Resource m => Int -> Sink a m [a]
- drop :: Resource m => Int -> Sink a m ()
- head :: Resource m => Sink a m (Maybe a)
- peek :: Resource m => Sink a m (Maybe a)
- consume :: Resource m => Sink a m [a]
- sinkNull :: Resource m => Sink a m ()
- foldM :: Resource m => (b -> a -> m b) -> b -> Sink a m b
- mapM_ :: Resource m => (a -> m ()) -> Sink a m ()
- map :: Monad m => (a -> b) -> Conduit a m b
- concatMap :: Monad m => (a -> [b]) -> Conduit a m b
- isolate :: Resource m => Int -> Conduit a m a
- filter :: Resource m => (a -> Bool) -> Conduit a m a
- mapM :: Monad m => (a -> m b) -> Conduit a m b
- concatMapM :: Monad m => (a -> m [b]) -> Conduit a m b
Sources
sourceList :: Resource m => [a] -> Source m aSource
Convert a list into a source.
Sinks
Pure
take :: Resource m => Int -> Sink a m [a]Source
Take some values from the stream and return as a list. If you want to
instead create a conduit that pipes data to another sink, see isolate
.
This function is semantically equivalent to:
take i = isolate i =$ consume
drop :: Resource m => Int -> Sink a m ()Source
Ignore a certain number of values in the stream. This function is semantically equivalent to:
drop i = take i >> return ()
However, drop
is more efficient as it does not need to hold values in
memory.
peek :: Resource m => Sink a m (Maybe a)Source
Look at the next value in the stream, if available. This function will not change the state of the stream.
consume :: Resource m => Sink a m [a]Source
Consume all values from the stream and return as a list. Note that this will pull all values into memory. For a lazy variant, see Data.Conduit.Lazy.
sinkNull :: Resource m => Sink a m ()Source
Ignore the remainder of values in the source. Particularly useful when
combined with isolate
.
Monadic
mapM_ :: Resource m => (a -> m ()) -> Sink a m ()Source
Apply the action to all values in the stream.
Pure
concatMap :: Monad m => (a -> [b]) -> Conduit a m bSource
Apply a transformation to all values in a stream, concatenating the output values.
isolate :: Resource m => Int -> Conduit a m aSource
Ensure that the inner sink consumes no more than the given number of
values. Note this this does not ensure that the sink consumes all of those
values. To get the latter behavior, combine with sinkNull
, e.g.:
src $$ do x <- isolate count =$ do x <- someSink sinkNull return x someOtherSink ...
filter :: Resource m => (a -> Bool) -> Conduit a m aSource
Keep only values in the stream passing a given predicate.
Monadic
mapM :: Monad m => (a -> m b) -> Conduit a m bSource
Apply a monadic transformation to all values in a stream.
If you do not need the transformed values, and instead just want the monadic
side-effects of running the action, see mapM_
.
concatMapM :: Monad m => (a -> m [b]) -> Conduit a m bSource
Apply a monadic transformation to all values in a stream, concatenating the output values.