Haskell: How to use attoparsec in order to read a nested list from a ByteString

妖精的绣舞 提交于 2019-12-01 08:37:40

问题


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:


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)


来源:https://stackoverflow.com/questions/19898602/haskell-how-to-use-attoparsec-in-order-to-read-a-nested-list-from-a-bytestring

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!