Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@liquidz
Created September 30, 2008 04:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save liquidz/13744 to your computer and use it in GitHub Desktop.
Save liquidz/13744 to your computer and use it in GitHub Desktop.
reading csv funcs for gauche
(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