bug eliminated from _words_ reading

This commit is contained in:
Matthias Felleisen 2010-04-16 17:37:59 -04:00
parent 17c5ed073c
commit 6d1d3da1fe
2 changed files with 32 additions and 13 deletions

View File

@ -9,22 +9,22 @@
(provide
;; all reader functions consume the name of a file f:
;; -- f must be a file name (string) in the same folder as the program
read-file ;; String -> String
;; read the specified file as a string
read-as-1strings ;; String -> [Listof 1String]
;; read the specified file as a list of 1strings (characters)
read-as-lines ;; String -> [Listof String]
;; read the specified file as a list of strings, one per line
read-as-words ;; String -> [Listof String]
;; read the specified file as a list of white-space separated tokens
read-as-words/line ;; String -> [Listof [Listof String]]
;; read the specified file as a list of lines, each line as a list of words
read-as-csv ;; String -> [Listof [Listof (U Any)]]
;; -- f must be formated as a a file with comma-separated values (Any)
;; read the specified file as a list of lists---one per line---of values (Any)
@ -33,7 +33,7 @@
;; -- f must be formated as a a file with comma-separated values (Any)
;; read the specified file as a file of comma-separated values, apply the second
;; argument to each row, i.e., list of CSV on one line
write-file ;; String String -> Boolean
;; write the second argument to specified file in the same folder as the program
;; produce false, if f exists
@ -61,13 +61,19 @@
(read-chunks f read-line reverse))
(def-reader (read-as-words f)
(define lines (read-chunks f read-line reverse))
(foldr (lambda (f r) (append (split f) r)) '() lines))
(read-as-words/line/internal f append))
(def-reader (read-as-words/line f)
;; String -> [Listof [Listof String]]
;; read the specified file as a list of lines, each line as a list of words
(map split (read-chunks f read-line reverse)))
(read-as-words/line/internal f cons))
(define (read-as-words/line/internal f combine)
(define lines (read-chunks f read-line (lambda (x) x)))
(foldl (lambda (f r)
(define fst (filter (compose not (curry string=? "")) (split f)))
(if (empty? fst) r (combine fst r)))
'() lines))
(def-reader (read-as-csv f)
(read-as-csv/func f))

View File

@ -45,6 +45,19 @@ eos
(write-file file test2)
(check-equal? (read-as-1strings file) as-1strings2 "as-lines 2")
(define test2-a-as-list '("test1" "" "test2"))
(define test2-a
(apply string-append
(list (first test2-as-list)
(string #\newline)
(string #\newline)
(second test2-as-list))))
(write-file file test2-a)
(check-equal? (read-as-lines file) test2-a-as-list "as-lines 2-a")
(check-equal? (read-as-words file) '("test1" "test2") "as-words 2-a")
(define test3 #<< eos
word1, word2
word3, word4
@ -53,13 +66,13 @@ eos
(write-file file test3)
(check-equal? (read-as-words file) '("word1," "word2" "word3," "word4")
"as-words")
"as-words")
(check-equal? (read-as-words/line file) '(("word1," "word2") ("word3," "word4"))
"as-words")
"as-words")
(check-equal? (read-as-csv file) '(("word1" "word2") ("word3" "word4"))
"as-cvs 1")
(check-equal? (read-as-csv/rows file length) '(2 2)
"as-words/rows")
"as-cvs 1")
(check-equal? (read-as-csv/rows file length) '(2 2)
"as-csv/rows")
(check-exn exn:fail:contract? (lambda () (write-file 0 1)))