Haskell: How to use attoparsec in order to read a

2020-03-27 05:15发布

I have a text file (~ 300 MB large) with a nested list, similar to this one:

[[4, 9, 11, 28, 30, 45, 55, 58, 61, 62, 63, 69, 74, 76, 77, 82, 87, 92, 93, 94, 95], [4, 9, 11, 28, 30, 45, 55, 58, 61, 62, 63, 69, 74, 76, 77, 82, 87, 92, 93, 94],[4, 9, 11, 28, 30, 45, 55, 58, 61, 62, 63, 69, 74, 76, 77, 82, 85, 87, 92, 93, 94, 95]]

Here is my program to read the file into a haskell Integer list:

import qualified Data.ByteString as ByteStr

main :: IO ()

-- HOW to do the same thing but using ByteStr.readFile for file access?
main = do fContents <- readFile filePath 
          let numList = readNums fContents
          putStrLn (show nums)

This works for small text files, but I want to use ByteString to read the file quickly. I found out that there is no read function for ByteString, instead you should write your own parser in attoparsec, since it supports parsing ByteStrings.

How can I use attoparsec to parse the nested list?

1条回答
做自己的国王
2楼-- · 2020-03-27 05:37

The data seems to be in JSON format, so you can use Data.Aeson decode function which works on ByteString

import qualified Data.ByteString.Lazy as BL
import Data.Aeson
import Data.Maybe

main = do fContents <- BL.readFile filePath 
          let numList = decode fContents :: Maybe [[Int]]
          putStrLn (show $ fromJust numList)
查看更多
登录 后发表回答