Created
August 27, 2011 09:39
-
-
Save h-hirai/1175194 to your computer and use it in GitHub Desktop.
practice of using enumerator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# 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