Haskell lazy I/O and closing files

前端 未结 7 848
攒了一身酷
攒了一身酷 2020-12-07 23:35

I\'ve written a small Haskell program to print the MD5 checksums of all files in the current directory (searched recursively). Basically a Haskell version of md5deep

7条回答
  •  轮回少年
    2020-12-08 00:04

    Edit: my assumption was that the user was opening thousands of very small files, it turns out they are very large. Laziness will be essential.

    Well, you'll need to use a different IO mechanism. Either:

    • Strict IO (process the files with Data.ByteString or System.IO.Strict
    • or, Iteratee IO (for experts only at the moment).

    I'd also strongly recommend not using 'unpack', as that destroys the benefit of using bytestrings.

    For example, you can replace your lazy IO with System.IO.Strict, yielding:

    import qualified System.IO.Strict as S
    
    getList :: FilePath -> IO [String]
    getList p = mapM getFileLine =<< getRecursiveContents p
        where
            getFileLine path = liftM (\c -> (hex (hash c)) ++ " " ++ path)
                                     (S.readFile path)
    

提交回复
热议问题