~ubuntu-branches/ubuntu/precise/haskell-text/precise

« back to all changes in this revision

Viewing changes to tests/benchmarks/FoldLines.hs

  • Committer: Bazaar Package Importer
  • Author(s): Joachim Breitner
  • Date: 2011-04-13 11:38:29 UTC
  • mfrom: (4.1.3 sid)
  • Revision ID: james.westby@ubuntu.com-20110413113829-f4ss61ivg720e5bu
Tags: 0.11.0.6-1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
{-# LANGUAGE BangPatterns #-}
 
2
 
 
3
import System.Environment
 
4
import System.IO
 
5
import qualified Data.Text as T
 
6
import qualified Data.Text.IO as T
 
7
import qualified Data.ByteString as S
 
8
 
 
9
-- Text
 
10
foldLinesT :: (a -> T.Text -> a) -> a -> Handle -> IO a
 
11
foldLinesT f z0 h = go z0
 
12
  where
 
13
    go !z = do
 
14
        eof <- hIsEOF h
 
15
        if eof
 
16
            then return z
 
17
            else do
 
18
                l <- T.hGetLine h
 
19
                let z' = f z l in go z'
 
20
{-# INLINE foldLinesT #-}
 
21
 
 
22
testT :: Handle -> IO Int
 
23
testT = foldLinesT (\n _ -> n + 1) 0
 
24
 
 
25
--ByteString
 
26
foldLinesB :: (a -> S.ByteString -> a) -> a -> Handle -> IO a
 
27
foldLinesB f z0 h = go z0
 
28
  where
 
29
    go !z = do
 
30
        eof <- hIsEOF h
 
31
        if eof
 
32
            then return z
 
33
            else do
 
34
                l <- S.hGetLine h
 
35
                let z' = f z l in go z'
 
36
{-# INLINE foldLinesB #-}
 
37
 
 
38
testB :: Handle -> IO Int
 
39
testB = foldLinesB (\n _ -> n + 1) 0
 
40
 
 
41
main = do
 
42
  (name : file : _) <- getArgs
 
43
  h <- openFile file ReadMode
 
44
  hSetBuffering h (BlockBuffering (Just 16384))
 
45
  case name of
 
46
    "bs" -> testB h
 
47
    "text" -> testT h