Skip to content

Instantly share code, notes, and snippets.

@h-hirai
Created August 27, 2011 09:39
Show Gist options
  • Save h-hirai/1175194 to your computer and use it in GitHub Desktop.
Save h-hirai/1175194 to your computer and use it in GitHub Desktop.
practice of using enumerator
{-# LANGUAGE OverloadedStrings #-}
-- http://d.hatena.ne.jp/kazu-yamamoto/20110216/1297845048
import Control.Monad.IO.Class (liftIO)
import qualified Data.ByteString as BS
import qualified Data.ByteString.Char8 as B -- for OverloadedString
import Data.Enumerator (($$), (<==<), ($=), (=$))
import qualified Data.Enumerator as E
import Data.Enumerator.Binary as EB
import qualified Data.Enumerator.List as EL
import Data.Maybe
consumer :: E.Iteratee BS.ByteString IO ()
consumer = do
mw <- EB.head
case mw of
Nothing -> return ()
Just w -> do
liftIO . putStr $ "XXX "
liftIO . BS.putStrLn . BS.singleton $ w
consumer
listFeeder :: E.Enumerator BS.ByteString IO a
listFeeder = E.enumList 1 ["12", "34"]
fileFeeder :: E.Enumerator BS.ByteString IO a
fileFeeder = EB.enumFile "FILE"
consumer2 :: E.Iteratee BS.ByteString IO ()
consumer2 = do
mw <- EB.head
case mw of
Nothing -> return ()
Just w -> do
liftIO . putStr $ "YYY "
liftIO . BS.putStrLn . BS.singleton $ w
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment