changed the names for batch-io functions following SK's suggstion
This commit is contained in:
parent
3d5e933d9e
commit
67d804af62
|
@ -13,23 +13,23 @@
|
|||
read-file ;; String -> 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-as-lines ;; String -> [Listof String]
|
||||
read-lines ;; String -> [Listof String]
|
||||
;; 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-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-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)
|
||||
;; 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)
|
||||
;; 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
|
||||
|
@ -54,33 +54,33 @@
|
|||
(def-reader (read-file f)
|
||||
(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)))
|
||||
|
||||
(def-reader (read-as-lines f)
|
||||
(def-reader (read-lines f)
|
||||
(read-chunks f read-line reverse))
|
||||
|
||||
(def-reader (read-as-words f)
|
||||
(read-as-words/line/internal f append))
|
||||
(def-reader (read-words f)
|
||||
(read-words/line/internal f append))
|
||||
|
||||
(def-reader (read-as-words/line f)
|
||||
(def-reader (read-words/line f)
|
||||
;; String -> [Listof [Listof String]]
|
||||
;; 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)))
|
||||
(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))
|
||||
(def-reader (read-csv-file f)
|
||||
(read-csv-file/func f))
|
||||
|
||||
(def-reader (read-as-csv/rows f row)
|
||||
(check-proc 'read-as-cvs row 1 "one argument" "row")
|
||||
(read-as-csv/func f row))
|
||||
(def-reader (read-csv-file/rows f row)
|
||||
(check-proc 'read-csv-file row 1 "one argument" "row")
|
||||
(read-csv-file/func f row))
|
||||
|
||||
;; -----------------------------------------------------------------------------
|
||||
;; writer
|
||||
|
@ -98,7 +98,7 @@
|
|||
;; auxiliaries
|
||||
|
||||
;; 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)
|
||||
(make-csv-reader o '((strip-leading-whitespace? . #t)
|
||||
(strip-trailing-whitespace? . #t)))))
|
||||
|
|
|
@ -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
|
||||
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)
|
||||
(read-as-1strings "data.txt")
|
||||
(read-1strings "data.txt")
|
||||
]
|
||||
Note how this function reproduces all parts of the file faithfully,
|
||||
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)
|
||||
(read-as-lines "data.txt")
|
||||
(read-lines "data.txt")
|
||||
]
|
||||
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
|
||||
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)
|
||||
(read-as-words "data.txt")
|
||||
(read-words "data.txt")
|
||||
]
|
||||
This time, however, the extra leading space of the second line of
|
||||
@scheme["data.txt"] has disappeared in the result. The space is considered
|
||||
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)
|
||||
(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.
|
||||
}
|
||||
|
||||
@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)
|
||||
(read-as-csv "data.csv")
|
||||
(read-csv-file "data.csv")
|
||||
]
|
||||
where the file named @scheme["data.csv"] has this shape:
|
||||
@(file-is "data.csv")
|
||||
|
@ -103,16 +103,16 @@ length. Here the third line of the file turns into a row of three
|
|||
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
|
||||
produces it as list of rows, each constructed via @scheme[s]}
|
||||
|
||||
@examples[#:eval (examples-batch-io)
|
||||
(read-as-csv/rows "data.csv" (lambda (x) x))
|
||||
(read-as-csv/rows "data.csv" length)
|
||||
(read-csv-file/rows "data.csv" (lambda (x) x))
|
||||
(read-csv-file/rows "data.csv" length)
|
||||
]
|
||||
The first example shows how @scheme[read-as-csv] is just a short form
|
||||
for @scheme[read-as-csv/rows]; the second one simply counts the
|
||||
The first example shows how @scheme[read-csv-file] is just a short form
|
||||
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.
|
||||
In many cases, the function argument is used to construct a structure from
|
||||
a row.}
|
||||
|
|
Loading…
Reference in New Issue
Block a user