-
Notifications
You must be signed in to change notification settings - Fork 1
/
csv-to-vector.rkt
45 lines (38 loc) · 1.51 KB
/
csv-to-vector.rkt
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
33
34
35
36
37
38
39
40
41
42
43
44
45
#lang racket
(require csv-reading)
(require rackunit)
(provide all-rows
DEFAULT-DATA-READER-MAKER)
;; the data-reader will be able to understand the specific CSV format
(define DEFAULT-DATA-READER-MAKER
(make-csv-reader-maker
'((seperator-chars #\,)
(strip-leading-whitespace . true)
(strip-trailing-whitespace . true))))
(define (all-rows a-file-path
#:a-reader-maker [a-reader-maker DEFAULT-DATA-READER-MAKER]
#:column-converters [column-converters empty])
(define next-row
(a-reader-maker (open-input-file a-file-path)))
(define (convert-row row column-converters)
(define (col-iter remaining-columns remaining-column-converters)
(cond [(empty? remaining-columns) empty]
[else (cond [(empty? remaining-column-converters)
(cons (first remaining-columns)
(col-iter (rest remaining-columns) empty))]
[else (cons ((first remaining-column-converters) (first remaining-columns))
(col-iter (rest remaining-columns)
(rest remaining-column-converters)))])]))
(col-iter row column-converters))
(define (row-iter)
(let ([row (next-row)])
(cond [(empty? row) empty]
[else (cons (list->vector (convert-row row column-converters))
(row-iter))])))
(list->vector (row-iter)))
#|
====
USAGE EXAMPLE:
(define data-set (all-rows FILE-PATH))
====
|#