LoginSignup
2
3

More than 5 years have passed since last update.

行カウント

Posted at

http://www.usptomonokai.jp/PAGE=20120814HASKELL
助けを聞いて。

こういう問題にはLazy ByteStringが楽です。
Lazy ByteStringは、4KB位ごとのチャンクになってるので、
Strict ByteStringとほぼ同じぐらいの速度が出ます。

import Control.Monad
import qualified Data.ByteString.Lazy.Char8 as L
import System.Environment

main :: IO ()
main = go False =<< getArgs where
  go _ ("-f": rest) = go True rest
  go _ []    = print . gyo =<< L.getContents
  go _ ["-"] = print . gyo =<< L.getContents
  go f files = forM_ files $ \file -> do
    ln <- return . gyo =<<  L.readFile file
    if f
      then putStrLn $ file ++ " " ++ show ln
      else putStrLn $ show ln ++ " " ++ file

gyo :: L.ByteString -> Int
gyo = length . L.lines
2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3