iteratee Couple functions for combining Enueratees with Enumerators and Iteratees

Michael Baikov manpacket at gmail.com
Fri Nov 25 06:14:39 GMT 2011


Currently we have two nice functions for combining Enumeratees ><> and
<>< which can be used to produce chains of Enumeratees. I think that
it will be nice to have those functions as well:


-- this implementation relies on unsafeCoerce to keep type signature
nice and clean
-- so it is requred to import Unsafe.Coerce

--| Combines Enumerator which produces stream of s and Enumeratee
which transforms stream of s to stream
-- of s' to into Enumerator which produces stream of s'
($=) :: (Nullable s, Nullable s', Monad m) => Enumerator s m a ->
Enumeratee s s' m b -> Enumerator s' m b
($=) enum enee iter = (unsafeCoerce enum) (enee iter) >>= run



-- this implementation leaves type signature a bit messy, but without
using unsafeCoerce

--| Combines Enumerator which produces stream of s and Enumeratee
which transforms stream of s to stream
-- of s' to into Enumerator which produces stream of s'
($=) :: (Nullable s, Nullable s', Monad m) => Enumerator s m (Iteratee
s' m a) -> Enumeratee s s' m a -> Enumerator s' m a
($=) enum enee iter = enum (enee iter) >>= run


--| Combines Enumeratee from s to s' and Iteratee which consumes s' to
into Iteratee which consumes s
(=$) :: (Nullable s, Nullable s', Monad m) => Enumeratee s s' m a ->
Iteratee s' m a -> Iteratee s m a
(=$) = (.) joinI


(=$) is mostly to keep sources clean, but ($=) can be used to
enumerate any given Iteratee over list of several different data
sources
(but type of the source can be transformed to type of Iteratee using
Enumeratee) just by transforming them to a single type and performing
foldM.



More information about the Iteratee mailing list