Skip to content

@adinapoli /gist:3899017

Embed URL


Subversion checkout URL

You can clone with
Download ZIP
Iteratee attempt
{-# LANGUAGE OverloadedStrings #-}
import Data.ByteString (ByteString)
import qualified Data.ByteString.Char8 as B
import Data.Enumerator (Iteratee, Enumeratee, ($=), (=$), (=$=), (==<<))
import qualified Data.Enumerator as E
import qualified Data.Enumerator.Binary as EB
import qualified Data.Enumerator.Text as ET
import qualified Data.Enumerator.List as EL
import Data.Text
import System.Environment
countCharBS :: (Monad m) => Char -> Iteratee ByteString m Integer
countCharBS needle = loop 0
where loop n = EL.head >>= check n
check n Nothing = return n
check n (Just t) = loop (n + toInteger (B.count needle t))
countLines :: (Monad m) => Iteratee Text m Integer
countLines = E.length
convertUtf8 :: (Monad m) => Enumeratee ByteString Text m b
convertUtf8 = ET.decode ET.utf8
main :: IO ()
main = do
args <- getArgs
case args of
(fname:needle:[]) ->
case needle of
(c:"") -> do
print "Switching to single char scan..."
cnt <- $ EB.enumFile fname ==<< countCharBS c
print cnt
_ -> do
cnt <- $ (EB.enumFile fname $= convertUtf8) ==<<
print cnt
_ -> print "Error in accessing file."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.