Skip to content

Instantly share code, notes, and snippets.

@danieldk
Created August 10, 2011 09:48
Show Gist options
  • Save danieldk/1136473 to your computer and use it in GitHub Desktop.
Save danieldk/1136473 to your computer and use it in GitHub Desktop.
An intersperse enumeratee
import Control.Monad.Trans.Class (lift)
import Data.Enumerator (Enumeratee, Step(..), Stream(..), runIteratee)
import qualified Data.Enumerator.List as EL
intersperse :: (Monad m) => a -> Enumeratee a a m b
intersperse v = first
where
first (Continue k) = do
h <- EL.head
case h of
Nothing -> return $ Continue k
Just e -> do
newStep <- lift $ runIteratee $ k $ Chunks [e]
loop newStep
first step = return step
loop = EL.concatMap (\x -> [v, x])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment