changed the names for batch-io functions following SK's suggstion

This commit is contained in:
Matthias Felleisen 2010-04-27 10:26:24 -04:00
parent 3d5e933d9e
commit 67d804af62
2 changed files with 35 additions and 35 deletions

View File

@ -13,23 +13,23 @@
read-file ;; String -> String read-file ;; String -> String
;; read the specified file as a string ;; read the specified file as a string
read-as-1strings ;; String -> [Listof 1String] read-1strings ;; String -> [Listof 1String]
;; read the specified file as a list of 1strings (characters) ;; read the specified file as a list of 1strings (characters)
read-as-lines ;; String -> [Listof String] read-lines ;; String -> [Listof String]
;; read the specified file as a list of strings, one per line ;; read the specified file as a list of strings, one per line
read-as-words ;; String -> [Listof String] read-words ;; String -> [Listof String]
;; read the specified file as a list of white-space separated tokens ;; read the specified file as a list of white-space separated tokens
read-as-words/line ;; String -> [Listof [Listof String]] read-words/line ;; String -> [Listof [Listof String]]
;; read the specified file as a list of lines, each line as a list of words ;; read the specified file as a list of lines, each line as a list of words
read-as-csv ;; String -> [Listof [Listof (U Any)]] read-csv-file ;; String -> [Listof [Listof (U Any)]]
;; -- f must be formated as a a file with comma-separated values (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) ;; read the specified file as a list of lists---one per line---of values (Any)
read-as-csv/rows ;; String ([Listof Any] -> X) -> [Listof X] read-csv-file/rows ;; String ([Listof Any] -> X) -> [Listof X]
;; -- f must be formated as a a file with comma-separated values (Any) ;; -- 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 ;; 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 ;; argument to each row, i.e., list of CSV on one line
@ -54,33 +54,33 @@
(def-reader (read-file f) (def-reader (read-file f)
(list->string (read-chunks f read-char drop-last-newline))) (list->string (read-chunks f read-char drop-last-newline)))
(def-reader (read-as-1strings f) (def-reader (read-1strings f)
(map string (read-chunks f read-char drop-last-newline))) (map string (read-chunks f read-char drop-last-newline)))
(def-reader (read-as-lines f) (def-reader (read-lines f)
(read-chunks f read-line reverse)) (read-chunks f read-line reverse))
(def-reader (read-as-words f) (def-reader (read-words f)
(read-as-words/line/internal f append)) (read-words/line/internal f append))
(def-reader (read-as-words/line f) (def-reader (read-words/line f)
;; String -> [Listof [Listof String]] ;; String -> [Listof [Listof String]]
;; read the specified file as a list of lines, each line as a list of words ;; read the specified file as a list of lines, each line as a list of words
(read-as-words/line/internal f cons)) (read-words/line/internal f cons))
(define (read-as-words/line/internal f combine) (define (read-words/line/internal f combine)
(define lines (read-chunks f read-line (lambda (x) x))) (define lines (read-chunks f read-line (lambda (x) x)))
(foldl (lambda (f r) (foldl (lambda (f r)
(define fst (filter (compose not (curry string=? "")) (split f))) (define fst (filter (compose not (curry string=? "")) (split f)))
(if (empty? fst) r (combine fst r))) (if (empty? fst) r (combine fst r)))
'() lines)) '() lines))
(def-reader (read-as-csv f) (def-reader (read-csv-file f)
(read-as-csv/func f)) (read-csv-file/func f))
(def-reader (read-as-csv/rows f row) (def-reader (read-csv-file/rows f row)
(check-proc 'read-as-cvs row 1 "one argument" "row") (check-proc 'read-csv-file row 1 "one argument" "row")
(read-as-csv/func f row)) (read-csv-file/func f row))
;; ----------------------------------------------------------------------------- ;; -----------------------------------------------------------------------------
;; writer ;; writer
@ -98,7 +98,7 @@
;; auxiliaries ;; auxiliaries
;; String [([Listof X] -> Y)] -> [Listof Y] ;; String [([Listof X] -> Y)] -> [Listof Y]
(define (read-as-csv/func f [row (lambda (x) x)]) (define (read-csv-file/func f [row (lambda (x) x)])
(local ((define (reader o) (local ((define (reader o)
(make-csv-reader o '((strip-leading-whitespace? . #t) (make-csv-reader o '((strip-leading-whitespace? . #t)
(strip-trailing-whitespace? . #t))))) (strip-trailing-whitespace? . #t)))))

View File

@ -56,45 +56,45 @@ assuming the file named @scheme["data.txt"] has this shape:
Note how the leading space in the second line translates into the space Note how the leading space in the second line translates into the space
between the newline indicator and the word @scheme["good"] in the result.} between the newline indicator and the word @scheme["good"] in the result.}
@item{@reading[read-as-1strings (listof 1string?)]{a list of one-char strings, one per character} @item{@reading[read-1strings (listof 1string?)]{a list of one-char strings, one per character}
@examples[#:eval (examples-batch-io) @examples[#:eval (examples-batch-io)
(read-as-1strings "data.txt") (read-1strings "data.txt")
] ]
Note how this function reproduces all parts of the file faithfully, Note how this function reproduces all parts of the file faithfully,
including spaces and newlines.} including spaces and newlines.}
@item{@reading[read-as-lines (listof string?)]{a list of strings, one per line} @item{@reading[read-lines (listof string?)]{a list of strings, one per line}
@examples[#:eval (examples-batch-io) @examples[#:eval (examples-batch-io)
(read-as-lines "data.txt") (read-lines "data.txt")
] ]
when @scheme["data.txt"] is the name of the same file as in the preceding when @scheme["data.txt"] is the name of the same file as in the preceding
item. And again, the leading space of the second line shows up in the item. And again, the leading space of the second line shows up in the
second string in the list.} second string in the list.}
@item{@reading[read-as-words (listof string?)]{a list of strings, one per white-space separated token in the file} @item{@reading[read-words (listof string?)]{a list of strings, one per white-space separated token in the file}
@examples[#:eval (examples-batch-io) @examples[#:eval (examples-batch-io)
(read-as-words "data.txt") (read-words "data.txt")
] ]
This time, however, the extra leading space of the second line of This time, however, the extra leading space of the second line of
@scheme["data.txt"] has disappeared in the result. The space is considered @scheme["data.txt"] has disappeared in the result. The space is considered
a part of the separator that surrounds the word @scheme["good"]. a part of the separator that surrounds the word @scheme["good"].
} }
@item{@reading[read-as-words/line (listof string?)]{a list of lists, one per line; each line is represented as a list of white-space separated tokens} @item{@reading[read-words/line (listof string?)]{a list of lists, one per line; each line is represented as a list of white-space separated tokens}
@examples[#:eval (examples-batch-io) @examples[#:eval (examples-batch-io)
(read-as-words/line "data.txt") (read-words/line "data.txt")
] ]
The results is similar to the one that @scheme[read-as-words] produces, The results is similar to the one that @scheme[read-words] produces,
except that the organization of the file into lines is preserved. except that the organization of the file into lines is preserved.
} }
@item{@reading[read-as-csv (listof (listof any/c))]{a list of lists of comma-separated values} @item{@reading[read-csv-file (listof (listof any/c))]{a list of lists of comma-separated values}
@examples[#:eval (examples-batch-io) @examples[#:eval (examples-batch-io)
(read-as-csv "data.csv") (read-csv-file "data.csv")
] ]
where the file named @scheme["data.csv"] has this shape: where the file named @scheme["data.csv"] has this shape:
@(file-is "data.csv") @(file-is "data.csv")
@ -103,16 +103,16 @@ length. Here the third line of the file turns into a row of three
elements. elements.
} }
@item{@defproc[(@read-as-csv/rows [f (and/c string? exists?)][s @item{@defproc[(@read-csv-file/rows [f (and/c string? exists?)][s
(-> (listof any/c) X?)]) (listof X?)]{reads the content of file @scheme[f] and (-> (listof any/c) X?)]) (listof X?)]{reads the content of file @scheme[f] and
produces it as list of rows, each constructed via @scheme[s]} produces it as list of rows, each constructed via @scheme[s]}
@examples[#:eval (examples-batch-io) @examples[#:eval (examples-batch-io)
(read-as-csv/rows "data.csv" (lambda (x) x)) (read-csv-file/rows "data.csv" (lambda (x) x))
(read-as-csv/rows "data.csv" length) (read-csv-file/rows "data.csv" length)
] ]
The first example shows how @scheme[read-as-csv] is just a short form The first example shows how @scheme[read-csv-file] is just a short form
for @scheme[read-as-csv/rows]; the second one simply counts the for @scheme[read-csv-file/rows]; the second one simply counts the
number of separated tokens and the result is just a list of numbers. number of separated tokens and the result is just a list of numbers.
In many cases, the function argument is used to construct a structure from In many cases, the function argument is used to construct a structure from
a row.} a row.}