Created
September 30, 2008 04:42
-
-
Save liquidz/13744 to your computer and use it in GitHub Desktop.
reading csv funcs for gauche
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(use file.util) | |
(use simply) | |
; =make-csv-row-name-hash | |
(define (make-csv-row-name-hash row);{{{ | |
(let1 hs (make-hash-table) | |
(let loop((ls row) (i 0)) | |
(cond | |
[(null? ls) | |
hs | |
] | |
[else | |
(when (string? (car ls)) | |
(hash-table-put! hs (string->symbol (car ls)) i) | |
) | |
(loop (cdr ls) (++ i)) | |
] | |
) | |
) | |
) | |
) | |
;}}} | |
; =csv->list | |
(define (csv->list file-name has-row-names?);{{{ | |
(let1 file-list (r fold (lambda (x res) | |
(cons (string-split x #/,/) res) | |
) '() (file->list read-line file-name)) | |
(cond | |
; 1行目がフィールド名の場合 | |
[(eq? #t has-row-names?) | |
; 1行目のフィールド名をハッシュ化 | |
(let1 row-name-hs (make-csv-row-name-hash (car file-list)) | |
(r fold (lambda (x res) | |
; フィールド名でrowを取れる無名関数 | |
(cons (lambda (label) | |
(ref x (hash-table-get row-name-hs label)) | |
) res) | |
) | |
'() (cdr file-list)) | |
) | |
] | |
; 全てがデータの場合 | |
[else | |
(r fold (lambda (x res) | |
; rowをindex番号で取れる無名関数 | |
(cons (lambda (index) | |
(ref x index) | |
) res) | |
) '() file-list) | |
] | |
) | |
) | |
) | |
;}}} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment