- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试获取 CSV 文件,对其进行错误检查,然后打印错误消息。
mapM_ (appendFile filePath)(返回[字符串]的errorCheck函数)
这可行,但当我运行它时,但在很长的 CSV 上,它会耗尽内存。我认为问题在于它太懒了,在执行任何操作之前将整个 CSV 加载到内存中。我尝试过使用 BangPatterns 来强制严格,但我不确定我是否正确使用它,因为它没有帮助
我可以提供更多信息或代码,但我不确定什么与我的问题相关
代码:
main = do
safeRead s = catch (readFile s) $ \_ -> return ""
filePath <- safeRead "in.txt"
file <- safeRead filePath
--save the errors
writeFile (createErrorFilePath filePath) (getFileName $ filePath ++ "\n")
let !addToErr = do appendFile (createErrorFilePath filePath)
mapM_ addToErr (map ('\n':) (errorCheckFile (( \(Right x) -> x ) (parseCSV file)) (errorCheckCols (checkCols . head $ (( \(Right x) -> x ) (parseCSV file))) errorMsgs)))
--exit with correct number
exitWith . ExitFailure . exitCtrl $ (errorCheckFile (( \(Right x) -> x ) (parseCSV file)) (errorCheckCols (checkCols $ head $ (( \(Right x) -> x ) (parseCSV file))) errorMsgs))
--decides what value to exit with, depends on if errors, warnings or nothing
exitCtrl :: [String] -> Int
exitCtrl [] = 1
exitCtrl (line:rest)
| ("ERROR:") == take 6 line = 3
-- | elem ("WARNING:") line = 1
| otherwise = exitCtrl rest
--gets a files name given a filepath
getFileName :: String -> String
getFileName filePath = reverse $ takeWhile (/= '\\') (reverse filePath)
--changes the input filepath to create a new error file at the same location
createErrorFilePath :: String -> String
createErrorFilePath pathIn = (reverse $ drop 4 (reverse pathIn)) ++ "_error_log.txt"
--check if any of the columns errored
errorCheckCols :: [Int] -> [String] -> String
errorCheckCols cols errors
| errorPos == Nothing = "All headings are accounted for :)"
| otherwise = errors !! (fromJust errorPos)
where errorPos = findIndex (== 1-2) cols
--This takes the file and if any of the columns don't exists, tells you, otherwise it error checks the database
errorCheckFile :: [[String]] -> String -> [String]
errorCheckFile [] _ = ["ERROR: No Data"]
errorCheckFile (headings:[]) colErrorMsg = ["ERROR: No Data"]
errorCheckFile (headings:info) colErrorMsg
| length headings > 25 = ["ERROR: Too many Columns of data"]
| colErrorMsg == "All headings are accounted for :)" =
checkDB
info
(findCol "example name 1" headings 3)
(findCol "example name 2" headings 5)
(findCol "example name 3" headings 5)
(findCol "example name 4" headings 3)
(findCol "example name 5" headings 3)
(findCol "example name 6" headings 3)
(findCol "example name 7" headings 8)
(findCol "example name 8" headings 3)
(findCol "example name 9" headings 15)
(findCol "example name 10" headings 15)
(findCol "example name 11" headings 3)
(findCol "example name 12" headings 3)
(findCol "example name 13" headings 9)
(findCol "example name 14" headings 16)
(findCol "example name 15" headings 9)
(findCol "example name 16" headings 16)
(findCol "example name 17" headings 3)
(findCol "example name 18" headings 3)
(findCol "example name 19" headings 8)
(findCol "example name 20" headings 3)
((findCol "example name 21" headings 20) + (findCol "example name 21 alt" headings 20) + 1)
(findCol "example name 22" headings 22)
(findCol "example name 23" headings 3)
(findCol "example name 24" headings 3)
(findCol "example name 25" headings 9)
1
| otherwise = [colErrorMsg]
--given the inputs headings, finds the positions if each row by name
checkCols :: [String] -> [Int]
checkCols headings = [(findCol "example name 1" headings 3),
(findCol "example name 2" headings 5),
(findCol "example name 3" headings 5),
(findCol "example name 4" headings 3),
(findCol "example name 5" headings 3),
(findCol "example name 6" headings 3),
(findCol "example name 7" headings 8),
(findCol "example name 8" headings 3),
(findCol "example name 9" headings 15),
(findCol "example name 10" headings 15),
(findCol "example name 11" headings 3),
(findCol "example name 12" headings 3),
(findCol "example name 13" headings 9),
(findCol "example name 14" headings 16),
(findCol "example name 15" headings 9),
(findCol "example name 16" headings 16),
(findCol "example name 17" headings 3),
(findCol "example name 18" headings 3),
(findCol "example name 19" headings 8),
(findCol "example name 20" headings 3),
((findCol "example name 21" headings 20) + (findCol "example name 21 alt" headings 20) + 1),
(findCol "example name 22" headings 22),
(findCol "example name 23" headings 3),
(findCol "example name 24" headings 3),
(findCol "example name 25" headings 9)]
-- [[String]] Intx25 Int(count) returns a [String]
checkDB :: [[String]] -> Int -> Int -> Int -> Int -> Int -> Int -> Int -> Int -> Int -> Int -> Int -> Int -> Int -> Int -> Int -> Int -> Int -> Int -> Int -> Int -> Int -> Int -> Int -> Int -> Int -> Int -> [String]
checkDB [] _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ = ["ERROR: Filename given does not exist"]
checkDB (lastRow:[]) pos1 pos2 pos3 pos4 pos5 pos6 pos7 pos8 pos9 pos10 pos11 pos12 pos13 pos14 pos15 pos16 pos17 pos18 pos19 pos20 pos21 pos22 pos23 pos24 pos25 count
| map (toUpper) (lastRow !! pos4) == "ASD" = checkASD lastRow pos1 pos2 pos3 pos5 pos6 pos7 pos8 pos9 pos10 pos11 pos12 pos13 pos14 pos15 pos16 pos17 pos18 pos19 pos20 pos21 pos22 pos23 count
| map (toUpper) (lastRow !! pos4) == "QWE" || map (toUpper) (lastRow !! pos4) == "DPS" = checkQWE lastRow pos1 pos2 pos3 pos5 pos6 pos7 pos8 pos9 pos10 pos11 pos12 pos13 pos14 pos15 pos16 pos18 pos19 pos24 pos25 pos17 count
| otherwise = ["ERROR: identifier not valid on line " ++ (show count)]
checkDB (firstRow:otherRows) pos1 pos2 pos3 pos4 pos5 pos6 pos7 pos8 pos9 pos10 pos11 pos12 pos13 pos14 pos15 pos16 pos17 pos18 pos19 pos20 pos21 pos22 pos23 pos24 pos25 count
-- | count `mod` 50 == 0 = []
| map (toUpper) (firstRow !! pos4) == "ASD" = checkASD firstRow pos1 pos2 pos3 pos5 pos6 pos7 pos8 pos9 pos10 pos11 pos12 pos13 pos14 pos15 pos16 pos17 pos18 pos19 pos20 pos21 pos22 pos23 count ++ checkDB otherRows pos1 pos2 pos3 pos4 pos5 pos6 pos7 pos8 pos9 pos10 pos11 pos12 pos13 pos14 pos15 pos16 pos17 pos18 pos19 pos20 pos21 pos22 pos23 pos24 pos25 (count+1)
| map (toUpper) (firstRow !! pos4) == "QWE" || map (toUpper) (firstRow !! pos4) == "DPS" = checkQWE firstRow pos1 pos2 pos3 pos5 pos6 pos7 pos8 pos9 pos10 pos11 pos12 pos13 pos14 pos15 pos16 pos18 pos19 pos24 pos25 pos17 count ++ checkDB otherRows pos1 pos2 pos3 pos4 pos5 pos6 pos7 pos8 pos9 pos10 pos11 pos12 pos13 pos14 pos15 pos16 pos17 pos18 pos19 pos20 pos21 pos22 pos23 pos24 pos25 (count+1)
| otherwise = ["ERROR: identifier not valid on line " ++ (show count)] ++ checkDB otherRows pos1 pos2 pos3 pos4 pos5 pos6 pos7 pos8 pos9 pos10 pos11 pos12 pos13 pos14 pos15 pos16 pos17 pos18 pos19 pos20 pos21 pos22 pos23 pos24 pos25 (count+1)
--collection of error checking methods for ASD rows
checkASD :: [String] -> Int -> Int -> Int -> Int -> Int -> Int -> Int -> Int -> Int -> Int -> Int -> Int -> Int -> Int -> Int -> Int -> Int -> Int -> Int -> Int -> Int -> Int -> Int -> [String]
checkASD row pos1 pos2 pos3 pos5 pos6 pos7 pos8 pos9 pos10 pos11 pos12 pos13 pos14 pos15 pos16 pos17 pos18 pos19 pos20 pos21 pos22 pos23 count
| sum ans == 0 = []
| (findIndex (>=20) ans /= Nothing && findIndex (>0) ans /= Nothing) = ["WARNING: At line " ++ (show count) ++ " at column(s) " ++ (show (cols!!(fromJust(findIndex (>=20) ans)) + 1))]
| findIndex (>=20) ans /= Nothing = ["WARNING: At line " ++ (show count) ++ " at column(s) " ++ (show (cols!!(fromJust(findIndex (>=20) ans)) + 1)) ++ "\nERROR: At line " ++ (show count) ++ " at column(s) " ++ unwords( map show( map (+1) (map (cols!!) (findIndices (>0) (init ans)))))]
| otherwise = ["ERROR: At line " ++ (show count) ++ " at column(s) " ++ unwords( map show( map (cols!!) (findIndices (>0) ans))) ]
where
ans = [errorCheck (row!!pos1) ["01","02","03","04","05"],
errorCheck (row!!pos2) ["3","4"],
errorCheck (row!!pos3) ["1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20"],
checkTypeLength (row!!pos5) isDigit 5,
sum [ checkLength (row!!pos6) 12, errorCheckNL (row!!pos6) ],
checkDate (row!!pos7),
checkTypeLength (row!!pos8) checkSex 1,
checkDate (row!!pos9),
checkDate (row!!pos10),
checkLength (row!!pos11) 5,
checkTypeLength (row!!pos12) isDigit 5,
checkMoney (row!!pos13),
checkMoney (row!!pos14),
checkMoney (row!!pos15),
checkMoney (row!!pos16),
checkLength (row!!pos17) 10,
checkLength (row!!pos18) 2,
checkDate (row!!pos19),
checkTypeLength (row!!pos20) isDigit 5,
checkTypeLength (row!!pos21) isDigit 5,
checkLength (row!!pos22) 1,
checkLength (row!!pos23) 1]
cols = [pos1, pos2, pos3, pos5, pos6, pos7, pos8, pos9, pos10, pos11, pos12, pos13, pos14, pos15, pos16, pos17, pos18, pos19, pos20, pos21, pos22, pos23]
--cols is a quick fix for finding where errors have occured
--collection of error checking methods for QWE/DPS rows
checkQWE :: [String] -> Int -> Int -> Int -> Int -> Int -> Int -> Int -> Int -> Int -> Int -> Int -> Int -> Int -> Int -> Int -> Int -> Int -> Int -> Int -> Int -> Int -> [String]
checkQWE row pos1 pos2 pos3 pos5 pos6 pos7 pos8 pos9 pos10 pos11 pos12 pos13 pos14 pos15 pos16 pos18 pos19 pos24 pos25 pos17 count
| sum ans == 0 = []
| (findIndex (>=20) ans /= Nothing && findIndex (>0) ans /= Nothing) = ["WARNING: At line " ++ (show count) ++ " at column(s) " ++ (show (cols!!(fromJust(findIndex (>=20) ans)) + 1))]
| findIndex (>=20) ans /= Nothing = ["WARNING: At line " ++ (show count) ++ " at column(s) " ++ (show (cols!!(fromJust(findIndex (>=20) ans)) + 1)) ++ "\nERROR: At line " ++ (show count) ++ " at column(s) " ++ unwords( map show( map (+1) (map (cols!!) (findIndices (>0) (init ans)))))]
| otherwise = ["ERROR: At line " ++ (show count) ++ " at column(s) " ++ unwords( map show (map (cols!!) (findIndices (>0) ans))) ]
where
ans = [errorCheck (row!!pos1) ["01","02","03","04","05","06","07","08","1","2","3","4","5","6","7","8"],
errorCheck (row!!pos2) ["1","2","3","4"],
errorCheck (row!!pos3) ["1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31","32"],
checkTypeLength (row!!pos5) isDigit 5,
sum [ checkLength (row!!pos6) 12, errorCheckNL (row!!pos6) ],
checkDate (row!!pos7),
checkTypeLength (row!!pos8) checkSex 1,
checkDate (row!!pos9),
checkDate (row!!pos10),
checkLength (row!!pos11) 5,
checkTypeLength (row!!pos12) isDigit 5,
checkMoney (row!!pos13),
checkMoney (row!!pos14),
checkMoney (row!!pos15),
checkMoney (row!!pos16),
checkLength (row!!pos18) 2,
checkDate (row!!pos19),
checkDate (row!!pos24),
checkMoney (row!!pos25),
checkQWEpos17 (row!!pos17)]
cols = [pos1, pos2, pos3, pos5, pos6, pos7, pos8, pos9, pos10, pos11, pos12, pos13, pos14, pos15, pos16, pos18, pos19, pos24, pos25, pos17]
--cols is a quick fix for finding where errors have occured
--checks that a string is 'm' or 'f'
checkSex :: Char -> Bool
checkSex input
| toLower input == 'm' || toLower input == 'f' = True
| otherwise = False
--finds if a row has something other than 1 type and are of a certain length
checkTypeLength :: String -> (Char -> Bool) -> Int -> Int
checkTypeLength [] _ _ = 1
checkTypeLength el isType maxSize
| length el > 0 && length el <= maxSize && (and $ map isType el) = 0
| otherwise = 1
--finds elements that exceed a certain length
checkLength :: String -> Int -> Int
checkLength [] _ = 1
checkLength el maxSize
| length el <= maxSize && length el > 0 = 0
| otherwise = 1
--finds a columns position given its name and the list of column names, else returns -1
findCol :: String -> [String] -> Int -> Int
findCol colName header unique --unique is the number of letters of each word that need to be compared
| findMatch /= Nothing = fromJust findMatch
| otherwise = -1
where findMatch = findIndex (==(map (toLower) (take unique colName))) (map (map toLower) (map (take unique) header))
--finds if a line where the element is not a letter and number ONLY, returns col num or 0
errorCheckNL :: String -> Int
errorCheckNL [] = 1
errorCheckNL (el1:el2:el3:rest)
| isDigit el1 && isDigit el2 && isDigit el3 && and(map isLetter rest) = 0 --return Nothing?
| otherwise = 2
errorCheckNL others = 3
-- given a row, the column num to check, options of what the column and a counter==0, returns row num or 0
errorCheck :: String -> [String] -> Int
errorCheck [] _ = 1
errorCheck el options
| map toLower el `elem` options = 0
| otherwise = 1
--finds if a date is valid given a row, the column num to check and a counter=0, returns row num or 0
checkDate :: String -> Int
checkDate [] = 1
checkDate el
| sepPos1 == Nothing = 1 --error: this is not formated as a date, there's no separator
| sepPos2 == Nothing = 2 --error: this is not formated as a date, there's no separator
| day `notElem` ["1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31"] = 3
| (map toLower month) `notElem` ["jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec"] = 4 --error: month is not valid
| length (tail year) == 4 && and (map isDigit (tail year)) = 22
| length (tail year) == 2 && and (map isDigit (tail year)) = 0
| otherwise = 5
where
sepPos1 = findIndex (== '-') el --maybe
(day, rest) = splitAt (fromJust sepPos1) el
sepPos2 = findIndex (== '-') (tail rest) --maybe
(month, year) = splitAt (fromJust sepPos2) (tail rest)
--checks that quantities of money are presented correctly
checkMoney :: String -> Int
checkMoney [] = 4
checkMoney cash
| decimalPoint == Nothing && checkTypeLength cash isDigit 9 /= 0 = 1 --error: not numbers
| decimalPoint /= Nothing && checkTypeLength (tail cents) isDigit 2 /= 0 = 2 --error: not numbers after the decimal point
| decimalPoint /= Nothing && checkTypeLength euros isDigit 9 /= 0 = 3 --error: not numbers after the decimal point
| otherwise = 0
where
decimalPoint = findIndex (== '.') cash --maybe
(euros, cents) = splitAt (fromJust decimalPoint) cash
最佳答案
这段代码
(( \(Right x) -> x ) (parseCSV file))
表示 parseCSV
几乎不可能增量地解析文件并开始返回部分结果而不消耗整个文件。这是不可能的,如果
parseCSV
的结果应为 Left Something
;如果输入格式正确,则结果应为 Right result
和在完整的输入被消耗之前,我们无法知道解析是否会成功,因此无法确定结果是Left errorMessage
还是Right result
.
此外,您还两次使用解析结果,一次用于将消息附加到日志文件,一次用于确定退出代码。第二次使用保存对其的引用,因此它(或者,在多次评估的情况下,文件内容)无法被垃圾收集。
另一方面,您在四个地方使用了这段代码。根据编译器执行公共(public)子表达式消除的方式,文件最多被解析四次。将解析结果绑定(bind)到名称以避免多次求值会更安全。
如果您知道您的文件格式良好,则可以通过分块解析文件来减少空间使用量,例如逐行且仅遍历一次(记录和确定退出代码相结合)。适合该遍历的模式是 Control.Monad.foldM
,
logAndUpdate file exitCode record = do
appendFile file (message record)
return $! update exitCode (exitInfo record)
主要内容:
exitCode <- foldM (logAndUpdate filename) 0 (parsedRecords)
exitWith $ ExitCode exitCode
如果行是独立的,则可以采用类似的策略,这样格式错误的行的存在不会影响之前和之后格式正确的行的处理,然后每行将被解析为 Either ParseError Record
。如果格式错误的行仅影响后面的行,而不影响前面的行,那么仍然可以通过在遇到格式错误的行时停止解析或切换解析器来实现。
但是,如果格式错误的行使整个文件无效,则可能无法在或多或少的恒定空间中执行此操作。如果预期结构允许的话,可以分两遍完成,一次用于检查格式良好,另一次用于解析。
关于haskell - 使 haskell 程序在(大致)恒定量的内存中运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9966251/
我在具有 2CPU 和 3.75GB 内存 (https://aws.amazon.com/ec2/instance-types/) 的 c3.large Amazon EC2 ubuntu 机器上运
我想通过用户空间中的mmap-ing并将地址发送到内核空间从用户空间写入VGA内存(视频内存,而不是缓冲区),我将使用pfn remap将这些mmap-ed地址映射到vga内存(我将通过 lspci
在 Mathematica 中,如果你想让一个函数记住它的值,它在语法上是很轻松的。例如,这是标准示例 - 斐波那契: fib[1] = 1 fib[2] = 1 fib[n_]:= fib[n] =
我读到动态内存是在运行时在堆上分配的,而静态内存是在编译时在堆栈上分配的,因为编译器知道在编译时必须分配多少内存。 考虑以下代码: int n; cin>>n; int a[n]; 如果仅在运行期间读
我是 Python 的新手,但我之前还不知道这一点。我在 for 循环中有一个基本程序,它从站点请求数据并将其保存到文本文件但是当我检查我的任务管理器时,我发现内存使用量只增加了?长时间运行时,这对我
我正在设计一组数学函数并在 CPU 和 GPU(使用 CUDA)版本中实现它们。 其中一些函数基于查找表。大多数表占用 4KB,其中一些占用更多。基于查找表的函数接受一个输入,选择查找表的一两个条目,
读入一个文件,内存被动态分配给一个字符串,文件内容将被放置在这里。这是在函数内部完成的,字符串作为 char **str 传递。 使用 gdb 我发现在行 **(str+i) = fgetc(aFil
我需要证实一个理论。我正在学习 JSP/Java。 在查看了一个现有的应用程序(我没有写)之后,我注意到一些我认为导致我们的性能问题的东西。或者至少是其中的一部分。 它是这样工作的: 1)用户打开搜索
n我想使用memoization缓存某些昂贵操作的结果,这样就不会一遍又一遍地计算它们。 两个memoise和 R.cache适合我的需要。但是,我发现缓存在调用之间并不可靠。 这是一个演示我看到的问
我目前正在分析一些 javascript shell 代码。这是该脚本中的一行: function having() { memory = memory; setTimeout("F0
我有一种情况,我想一次查询数据库,然后再将整个数据缓存在内存中。 我得到了内存中 Elasticsearch 的建议,我用谷歌搜索了它是什么,以及如何在自己的 spring boot 应用程序中实现它
我正在研究 Project Euler (http://projecteuler.net/problem=14) 的第 14 题。我正在尝试使用内存功能,以便将给定数字的序列长度保存为部分结果。我正在
所以,我一直在做 Java 内存/注意力游戏作业。我还没有达到我想要的程度,它只完成了一半,但我确实让 GUI 大部分工作了......直到我尝试向我的框架添加单选按钮。我认为问题可能是因为我将 JF
我一直在尝试使用 Flask-Cache 的 memoize 功能来仅返回 statusTS() 的缓存结果,除非在另一个请求中满足特定条件,然后删除缓存。 但它并没有被删除,并且 Jinja 模板仍
我对如何使用 & 运算符来减少内存感到非常困惑。 我可以回答下面的问题吗? clase C{ function B(&$a){ $this->a = &$a; $thi
在编写代码时,我遇到了一个有趣的问题。 我有一个 PersonPOJO,其 name 作为其 String 成员之一及其 getter 和 setter class PersonPOJO { priv
在此代码中 public class Base { int length, breadth, height; Base(int l, int b, int h) { l
Definition Structure padding is the process of aligning data members of the structure in accordance
在 JavaScript Ninja 的 secret 中,作者提出了以下方案,用于在没有闭包的情况下内存函数结果。他们通过利用函数是对象这一事实并在函数上定义一个属性来存储过去调用函数的结果来实现这
我正在尝试找出 map 消耗的 RAM 量。所以,我做了以下事情;- Map cr = crPair.collectAsMap(); // 200+ entries System.out.printl
我是一名优秀的程序员,十分优秀!