所以,我已经使用了几个
Haskell
XML库,包括hexpat和xml-enumerator.在阅读了真实世界Haskell(http://book.realworldhaskell.org/read/io.html)中的IO章节之后,我的印象是,如果我运行以下代码,那么在我浏览它时它将被垃圾收集.
原文链接:https://www.f2er.com/xml/292073.html但是,当我在一个大文件上运行时,内存使用量会随着运行而不断攀升.
runghc parse.hs bigfile.xml
我究竟做错了什么?我的假设错了吗?地图/过滤器是否强制它评估所有内容?
import qualified Data.ByteString.Lazy as BSL import qualified Data.ByteString.Lazy.UTF8 as U import Prelude hiding (readFile) import Text.XML.Expat.SAX import System.Environment (getArgs) main :: IO () main = do args <- getArgs contents <- BSL.readFile (head args) -- putStrLn $U.toString contents let events = parse defaultParSEOptions contents mapM_ print $map getTMSId $filter isEvent events isEvent :: SAXEvent String String -> Bool isEvent (StartElement "event" as) = True isEvent _ = False getTMSId :: SAXEvent String String -> Maybe String getTMSId (StartElement _ as) = lookup "TMSId" as