Work on documenting CSV module and extending duck to support racket-kwargs.
This commit is contained in:
parent
a5afb25dd7
commit
4d81684cff
6 changed files with 53 additions and 16 deletions
|
@ -25,8 +25,12 @@
|
|||
|
||||
(declare (unit util-csv))
|
||||
|
||||
(module
|
||||
(import duck)
|
||||
|
||||
(module*
|
||||
util-csv
|
||||
#:doc ("This module provides a very simple, incomplete and incorrect but fast
|
||||
CSV loader.")
|
||||
(
|
||||
make-csv-line-parser
|
||||
csv-parse
|
||||
|
@ -41,11 +45,17 @@
|
|||
(chicken condition)
|
||||
testing
|
||||
progress
|
||||
util-io)
|
||||
util-io
|
||||
racket-kwargs)
|
||||
|
||||
;; Curry version of line parser with configurable cell separator and
|
||||
;; Curry version of line parser with configurable cell separator and
|
||||
;; string delimiter. Returns a list of lists of strings.
|
||||
(define ((make-csv-line-parser separator string-delimiter) line)
|
||||
(define/doc ((make-csv-line-parser separator string-delimiter) line)
|
||||
("Curried version of fast CSV line parser with given separator and string delimiter.
|
||||
|
||||
* ```separator``` - separator character
|
||||
* ```string-delimiger``` - string quotation character
|
||||
* ```line``` - line to parse")
|
||||
(let loop ((tokens (string->list line))
|
||||
(res '())
|
||||
(state 1))
|
||||
|
@ -86,10 +96,10 @@
|
|||
(cons (cons token (car res)) (cdr res))
|
||||
2)))))))) ; Continue inside quoted data
|
||||
;; Parses given CSV lines list
|
||||
(define (csv-parse-lines lines . args)
|
||||
(let* ((separator (get-keyword #:separator args (lambda () #\;)))
|
||||
(string-delimiter (get-keyword #:string-delimiter args (lambda () #\")))
|
||||
(csv-parse-line (make-csv-line-parser separator string-delimiter))
|
||||
(define* (csv-parse-lines lines
|
||||
#:separator (separator #\;)
|
||||
#:string-delimiter (string-delimiter #\"))
|
||||
(let* ((csv-parse-line (make-csv-line-parser separator string-delimiter))
|
||||
(total (max (sub1 (length lines)) 1)))
|
||||
(let loop ((lines lines)
|
||||
(idx 0)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue