Safe Haskell | None |
---|---|
Language | Haskell2010 |
Monadic Iteratees: incremental input parsers, processors and transformers
This module provides many basic iteratees from which more complicated
iteratees can be built. In general these iteratees parallel those in
Data.List
, with some additions.
- isFinished :: Nullable s => Iteratee s m Bool
- stream2list :: (Monad m, Nullable s, ListLike s el) => Iteratee s m [el]
- stream2stream :: (Monad m, Nullable s, Monoid s) => Iteratee s m s
- dropWhileStream :: ListLike s el => (el -> Bool) -> Iteratee s m ()
- dropStream :: (Nullable s, ListLike s el) => Int -> Iteratee s m ()
- headStream :: ListLike s el => Iteratee s m el
- tryHead :: ListLike s el => Iteratee s m (Maybe el)
- lastStream :: (ListLike s el, Nullable s) => Iteratee s m el
- heads :: (Monad m, Nullable s, ListLike s el, Eq el) => s -> Iteratee s m Int
- peekStream :: ListLike s el => Iteratee s m (Maybe el)
- roll :: (Monad m, Nullable s, ListLike s el, ListLike s' s) => Int -> Int -> Iteratee s m s'
- lengthStream :: (Num a, ListLike s el) => Iteratee s m a
- chunkLength :: ListLike s el => Iteratee s m (Maybe Int)
- takeFromChunk :: (Nullable s, ListLike s el) => Int -> Iteratee s m s
- breakStream :: ListLike s el => (el -> Bool) -> Iteratee s m s
- breakE :: (ListLike s el, NullPoint s) => (el -> Bool) -> Enumeratee s s m a
- takeStream :: (Monad m, Nullable s, ListLike s el) => Int -> Enumeratee s s m a
- takeUpTo :: (Monad m, Nullable s, ListLike s el) => Int -> Enumeratee s s m a
- takeWhileE :: (ListLike s el, NullPoint s) => (el -> Bool) -> Enumeratee s s m a
- mapStream :: (ListLike (s el) el, ListLike (s el') el', NullPoint (s el)) => (el -> el') -> Enumeratee (s el) (s el') m a
- concatMapStream :: (Monad m, ListLike s a, NullPoint s) => (a -> t) -> Enumeratee s t m r
- concatMapStreamM :: (Monad m, ListLike s a, NullPoint s) => (a -> m t) -> Enumeratee s t m r
- mapMaybeStream :: (ListLike s a, NullPoint s, ListLike t b) => (a -> Maybe b) -> Enumeratee s t m r
- rigidMapStream :: (ListLike s el, NullPoint s) => (el -> el) -> Enumeratee s s m a
- filterStream :: (NullPoint s, ListLike s el) => (el -> Bool) -> Enumeratee s s m a
- filterStreamM :: (Monad m, ListLike s a, Nullable s) => (a -> m Bool) -> Enumeratee s s m r
- groupStreamBy :: (Monad m, ListLike l t, Nullable l) => (t -> t -> Bool) -> m (Iteratee l m t2) -> Enumeratee l [t2] m a
- groupStreamOn :: (Monad m, ListLike l e, Eq t1, Nullable l) => (e -> t1) -> (t1 -> m (Iteratee l m t2)) -> Enumeratee l [(t1, t2)] m a
- mergeStreams :: (ListLike s1 el1, ListLike s2 el2, Nullable s1, Nullable s2, Monad m) => (el1 -> el2 -> b) -> Enumeratee s2 b (Iteratee s1 m) a
- mergeByChunks :: (Nullable c2, Nullable c1, ListLike c1 el1, ListLike c2 el2, Monad m) => (c1 -> c2 -> c3) -> (c1 -> c3) -> (c2 -> c3) -> Enumeratee c2 c3 (Iteratee c1 m) a
- foldStream :: ListLike s el => (a -> el -> a) -> a -> Iteratee s m a
- enumPureNChunk :: (Monad m, ListLike s el) => s -> Int -> Enumerator s m a
- enumWith :: (Monad m, Nullable s, ListLike s el) => Iteratee s m a -> Iteratee s m b -> Iteratee s m (a, b)
- zipStreams :: (Monad m, Nullable s, ListLike s el) => Iteratee s m a -> Iteratee s m b -> Iteratee s m (a, b)
- zipStreams3 :: (Monad m, Nullable s, ListLike s el) => Iteratee s m a -> Iteratee s m b -> Iteratee s m c -> Iteratee s m (a, b, c)
- zipStreams4 :: (Monad m, Nullable s, ListLike s el) => Iteratee s m a -> Iteratee s m b -> Iteratee s m c -> Iteratee s m d -> Iteratee s m (a, b, c, d)
- zipStreams5 :: (Monad m, Nullable s, ListLike s el) => Iteratee s m a -> Iteratee s m b -> Iteratee s m c -> Iteratee s m d -> Iteratee s m e -> Iteratee s m (a, b, c, d, e)
- sequenceStreams_ :: (Monad m, ListLike s el, Nullable s) => [Iteratee s m a] -> Iteratee s m ()
- countConsumed :: forall a s el m n. (Monad m, ListLike s el, Nullable s, Integral n) => Iteratee s m a -> Iteratee s m (a, n)
- mapStreamM :: (Monad m, ListLike (s el) el, ListLike (s el') el', NullPoint (s el)) => (el -> m el') -> Enumeratee (s el) (s el') m a
- mapStreamM_ :: (Monad m, Nullable s, ListLike s el) => (el -> m b) -> Iteratee s m ()
- foldStreamM :: (Monad m, Nullable s, ListLike s a) => (b -> a -> m b) -> b -> Iteratee s m b
- module Bio.Iteratee.Iteratee
Iteratees
Iteratee Utilities
stream2list :: (Monad m, Nullable s, ListLike s el) => Iteratee s m [el] Source #
Read a stream to the end and return all of its elements as a list. This iteratee returns all data from the stream *strictly*.
stream2stream :: (Monad m, Nullable s, Monoid s) => Iteratee s m s Source #
Read a stream to the end and return all of its elements as a stream. This iteratee returns all data from the stream *strictly*.
Basic Iteratees
dropWhileStream :: ListLike s el => (el -> Bool) -> Iteratee s m () Source #
Skip all elements while the predicate is true.
The analogue of List.dropWhile
dropStream :: (Nullable s, ListLike s el) => Int -> Iteratee s m () Source #
Drop n elements of the stream, if there are that many.
The analogue of List.drop
headStream :: ListLike s el => Iteratee s m el Source #
Attempt to read the next element of the stream and return it Raise a (recoverable) error if the stream is terminated.
The analogue of List.head
Because head
can raise an error, it shouldn't be used when constructing
iteratees for convStream
. Use tryHead
instead.
tryHead :: ListLike s el => Iteratee s m (Maybe el) Source #
Similar to headStream
, except it returns Nothing
if the stream
is terminated.
lastStream :: (ListLike s el, Nullable s) => Iteratee s m el Source #
Attempt to read the last element of the stream and return it Raise a (recoverable) error if the stream is terminated
The analogue of List.last
heads :: (Monad m, Nullable s, ListLike s el, Eq el) => s -> Iteratee s m Int Source #
Given a sequence of characters, attempt to match them against
the characters on the stream. Return the count of how many
characters matched. The matched characters are removed from the
stream.
For example, if the stream contains abd
, then (heads abc
)
will remove the characters ab
and return 2.
peekStream :: ListLike s el => Iteratee s m (Maybe el) Source #
Look ahead at the next element of the stream, without removing
it from the stream.
Return Just c
if successful, return Nothing
if the stream is
terminated by EOF
.
:: (Monad m, Nullable s, ListLike s el, ListLike s' s) | |
=> Int | length of chunk (t) |
-> Int | amount to consume (d) |
-> Iteratee s m s' |
Return a chunk of t
elements length while consuming d
elements
from the stream. Useful for creating a 'rolling average' with
convStream
.
lengthStream :: (Num a, ListLike s el) => Iteratee s m a Source #
Return the total length of the remaining part of the stream.
This forces evaluation of the entire stream.
The analogue of List.length
chunkLength :: ListLike s el => Iteratee s m (Maybe Int) Source #
Get the length of the current chunk, or Nothing
if EOF
.
This function consumes no input.
takeFromChunk :: (Nullable s, ListLike s el) => Int -> Iteratee s m s Source #
Take n
elements from the current chunk, or the whole chunk if
n
is greater.
Nested iteratee combinators
breakStream :: ListLike s el => (el -> Bool) -> Iteratee s m s Source #
Takes an element predicate and returns the (possibly empty) prefix of the stream. None of the characters in the string satisfy the character predicate. If the stream is not terminated, the first character of the remaining stream satisfies the predicate.
N.B. breakE
should be used in preference to breakStream
.
breakStream
will retain all data until the predicate is met, which may
result in a space leak.
The analogue of List.break
breakE :: (ListLike s el, NullPoint s) => (el -> Bool) -> Enumeratee s s m a Source #
Takes an element predicate and an iteratee, running the iteratee on all elements of the stream until the predicate is met.
the following rule relates break
to breakE
break
pred === joinI
(breakE
pred stream2stream)
breakE
should be used in preference to break
whenever possible.
:: (Monad m, Nullable s, ListLike s el) | |
=> Int | number of elements to consume |
-> Enumeratee s s m a |
Read n elements from a stream and apply the given iteratee to the stream of the read elements. Unless the stream is terminated early, we read exactly n elements, even if the iteratee has accepted fewer.
The analogue of List.take
takeUpTo :: (Monad m, Nullable s, ListLike s el) => Int -> Enumeratee s s m a Source #
Read n elements from a stream and apply the given iteratee to the
stream of the read elements. If the given iteratee accepted fewer
elements, we stop.
This is the variation of takeStream
with the early termination
of processing of the outer stream once the processing of the inner stream
finished early.
Iteratees composed with takeUpTo
will consume only enough elements to
reach a done state. Any remaining data will be available in the outer
stream.
> let iter = do h <- joinI $ takeUpTo 5 I.head t <- stream2list return (h,t) > enumPureNChunk [1..10::Int] 3 iter >>= run >>= print (1,[2,3,4,5,6,7,8,9,10]) > enumPureNChunk [1..10::Int] 7 iter >>= run >>= print (1,[2,3,4,5,6,7,8,9,10])
in each case, I.head
consumes only one element, returning the remaining
4 elements to the outer stream
takeWhileE :: (ListLike s el, NullPoint s) => (el -> Bool) -> Enumeratee s s m a Source #
Takes an element predicate and an iteratee, running the iteratee on all elements of the stream while the predicate is met.
This is preferred to takeWhile
.
mapStream :: (ListLike (s el) el, ListLike (s el') el', NullPoint (s el)) => (el -> el') -> Enumeratee (s el) (s el') m a Source #
Map a function over an Iteratee
.
This one is reimplemented and differs from the the one in
Data.Iteratee.ListLike in so far that it doesn't pass on an EOF
received in the input, which is the expected behavior.
concatMapStream :: (Monad m, ListLike s a, NullPoint s) => (a -> t) -> Enumeratee s t m r Source #
Apply a function to the elements of a stream, concatenate the results into a stream. No giant intermediate list is produced.
concatMapStreamM :: (Monad m, ListLike s a, NullPoint s) => (a -> m t) -> Enumeratee s t m r Source #
Apply a monadic function to the elements of a stream, concatenate the results into a stream. No giant intermediate list is produced.
mapMaybeStream :: (ListLike s a, NullPoint s, ListLike t b) => (a -> Maybe b) -> Enumeratee s t m r Source #
rigidMapStream :: (ListLike s el, NullPoint s) => (el -> el) -> Enumeratee s s m a Source #
Map the stream rigidly.
Like mapStream
, but the element type cannot change.
This function is necessary for ByteString
and similar types
that cannot have LooseMap
instances, and may be more efficient.
filterStream :: (NullPoint s, ListLike s el) => (el -> Bool) -> Enumeratee s s m a Source #
Creates an enumeratee
with only elements from the stream that
satisfy the predicate function. The outer stream is completely consumed.
The analogue of List.filter
XXX filterStream :: (ListLike s a, NullPoint s) => (a -> Bool) -> Enumeratee s s m r
filterStreamM :: (Monad m, ListLike s a, Nullable s) => (a -> m Bool) -> Enumeratee s s m r Source #
Apply a monadic filter predicate to an Iteratee
.
groupStreamBy :: (Monad m, ListLike l t, Nullable l) => (t -> t -> Bool) -> m (Iteratee l m t2) -> Enumeratee l [t2] m a Source #
Grouping on Iteratee
s. groupStreamBy cmp inner outer
executes
inner
to obtain an Iteratee
i
, then passes elements e
to i
as long as cmp e0 e
, where e0
is some preceeding element, is
true. Else, the result of run i
is passed to outer
and
groupStreamBy
restarts. At end of input, the resulting outer
is
returned.
groupStreamOn :: (Monad m, ListLike l e, Eq t1, Nullable l) => (e -> t1) -> (t1 -> m (Iteratee l m t2)) -> Enumeratee l [(t1, t2)] m a Source #
Grouping on Iteratee
s. groupStreamOn proj inner outer
executes
inner (proj e)
, where e
is the first input element, to obtain an
Iteratee
i
, then passes elements e
to i
as long as proj e
produces the same result. If proj e
changes or the input ends, the
pair of proj e
and the result of run i
is passed to outer
. At
end of input, the resulting outer
is returned.
mergeStreams :: (ListLike s1 el1, ListLike s2 el2, Nullable s1, Nullable s2, Monad m) => (el1 -> el2 -> b) -> Enumeratee s2 b (Iteratee s1 m) a Source #
mergeStreams
offers another way to nest iteratees: as a monad stack.
This allows for the possibility of interleaving data from multiple
streams.
-- print each element from a stream of lines. logger :: (MonadIO m) => Iteratee [ByteString] m () logger = mapStreamM_ (liftIO . putStrLn . B.unpack) -- combine alternating lines from two sources -- To see how this was derived, follow the types from -- 'ileaveLines logger' and work outwards. run =<< enumFile 10 "file1" (joinI $ enumLinesBS $ ( enumFile 10 "file2" . joinI . enumLinesBS $ joinI (ileaveLines logger)) >>= run) ileaveLines :: (Functor m, Monad m) => Enumeratee [ByteString] [ByteString] (Iteratee [ByteString] m) [ByteString] ileaveLines = mergeStreams (\l1 l2 -> [B.pack "f1:\n\t" ,l1 ,B.pack "f2:\n\t" ,l2 ]
:: (Nullable c2, Nullable c1, ListLike c1 el1, ListLike c2 el2, Monad m) | |
=> (c1 -> c2 -> c3) | merge function |
-> (c1 -> c3) | |
-> (c2 -> c3) | |
-> Enumeratee c2 c3 (Iteratee c1 m) a |
A version of mergeStreams which operates on chunks instead of elements.
mergeByChunks offers more control than mergeStreams
.
mergeStreams
terminates when the first stream terminates, however
mergeByChunks will continue until both streams are exhausted.
mergeByChunks
guarantees that both chunks passed to the merge
function will have the same number of elements, although that number
may vary between calls.
Folds
foldStream :: ListLike s el => (a -> el -> a) -> a -> Iteratee s m a Source #
Left-associative fold that is strict in the accumulator.
This function should be used in preference to foldl
whenever possible.
The analogue of List.foldl'
.
Enumerators
Basic enumerators
enumPureNChunk :: (Monad m, ListLike s el) => s -> Int -> Enumerator s m a Source #
The pure n-chunk enumerator
It passes a given stream of elements to the iteratee in n
-sized chunks.
Enumerator Combinators
enumWith :: (Monad m, Nullable s, ListLike s el) => Iteratee s m a -> Iteratee s m b -> Iteratee s m (a, b) Source #
Enumerate over two iteratees in parallel as long as the first iteratee is still consuming input. The second iteratee will be terminated with EOF when the first iteratee has completed. An example use is to determine how many elements an iteratee has consumed:
snd <$> enumWith (dropWhile (<5)) length
Compare to zipStreams
zipStreams :: (Monad m, Nullable s, ListLike s el) => Iteratee s m a -> Iteratee s m b -> Iteratee s m (a, b) Source #
Enumerate two iteratees over a single stream simultaneously.
Compare to List.zip
.
zipStreams3 :: (Monad m, Nullable s, ListLike s el) => Iteratee s m a -> Iteratee s m b -> Iteratee s m c -> Iteratee s m (a, b, c) Source #
zipStreams4 :: (Monad m, Nullable s, ListLike s el) => Iteratee s m a -> Iteratee s m b -> Iteratee s m c -> Iteratee s m d -> Iteratee s m (a, b, c, d) Source #
zipStreams5 :: (Monad m, Nullable s, ListLike s el) => Iteratee s m a -> Iteratee s m b -> Iteratee s m c -> Iteratee s m d -> Iteratee s m e -> Iteratee s m (a, b, c, d, e) Source #
sequenceStreams_ :: (Monad m, ListLike s el, Nullable s) => [Iteratee s m a] -> Iteratee s m () Source #
Enumerate a list of iteratees over a single stream simultaneously and discard the results. This is a different behavior than Prelude's sequence_ which runs iteratees in the list one after the other.
Compare to Prelude.sequence_
.
countConsumed :: forall a s el m n. (Monad m, ListLike s el, Nullable s, Integral n) => Iteratee s m a -> Iteratee s m (a, n) Source #
Transform an iteratee into one that keeps track of how much data it consumes.
Monadic functions
mapStreamM :: (Monad m, ListLike (s el) el, ListLike (s el') el', NullPoint (s el)) => (el -> m el') -> Enumeratee (s el) (s el') m a Source #
Map a monadic function over an Iteratee
.
mapStreamM_ :: (Monad m, Nullable s, ListLike s el) => (el -> m b) -> Iteratee s m () Source #
Map a monadic function over the elements of the stream and ignore the result.
foldStreamM :: (Monad m, Nullable s, ListLike s a) => (b -> a -> m b) -> b -> Iteratee s m b Source #
Fold a monadic function over an Iteratee
.
Re-exported modules
module Bio.Iteratee.Iteratee