From 1d94a97ea3db883ee5cbb34ff47022cb708b0b76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc-Andr=C3=A9=20B=C3=A9langer?= Date: Sat, 17 Sep 2022 20:34:30 -0400 Subject: [PATCH] Better path handling and performance. --- examples/csv.scm | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/examples/csv.scm b/examples/csv.scm index f7439f2..1ad75fb 100644 --- a/examples/csv.scm +++ b/examples/csv.scm @@ -3,20 +3,26 @@ \import csv +(define python-open \open) +(define csv.reader \csv.reader) + (define (read-csv path) - \f=open(`path) - \reader=csv.reader(f) - (let loop ((acc '())) + (let* ((f (python-open path)) + (reader (csv.reader f)) + (acc '())) (with-exception-catcher (lambda (e) ;; The exception will be a pair (PyObject* . repr(PyObject*)) - \f.close() + \(`f).close() (if \isinstance(`(car e), StopIteration) (reverse acc) ;; Return the result - (write e))) ;; Propagate the exception - ;; Iterate using __next__() until StopIteration is raised - (lambda () (loop (cons \reader.__next__() acc)))))) + (raise e))) ;; Propagate the exception + (lambda () + (let loop () + ;; Iterate using __next__() until StopIteration is raised + (set! acc (cons \(`reader).__next__() acc)) + (loop)))))) -(pretty-print (read-csv (path-expand "~~userlib/github.com/gambit/python/@/examples/data.csv"))) +(path-expand "data.csv" (path-directory (this-source-file))) ;; (("A" "B" "C") ("1" "2" "3"))