diff options
-rwxr-xr-x | lib/guile/read-habit.scm | 37 |
1 files changed, 21 insertions, 16 deletions
diff --git a/lib/guile/read-habit.scm b/lib/guile/read-habit.scm index 7ba7002..b3c7888 100755 --- a/lib/guile/read-habit.scm +++ b/lib/guile/read-habit.scm @@ -18,21 +18,25 @@ (define habits-dir (expand-file "~/org/habits/")) -(define (parse-file port) - (let loop ((values '()) - (line (read-line port))) - (if (eof-object? line) - (reverse values) - (loop (cons (map string->number (string-split line #\:)) values) - (read-line port))))) +(define (iter-lines filename proc) + (call-with-input-file filename + (lambda (port) + (let loop ((values '()) + (line (read-line port))) + (if (eof-object? line) + (reverse values) + (loop (cons (proc line) values) + (read-line port))))))) (define (cli-print file-name) - (map (lambda (row) - (format #t "~a -> ~d~%" - (colorize-string (strftime "%c" (localtime (car row))) 'CYAN 'BOLD) - (cadr row))) - (call-with-input-file (string-append habits-dir file-name) - parse-file))) + (iter-lines + (string-append habits-dir file-name) + (lambda (line) + (let ((row (map string->number (string-split line #\:)))) + (format #t "~a -> ~d~%" + (colorize-string (strftime "%c" (localtime (car row))) 'CYAN 'BOLD) + (cadr row)) + row)))) (define (request-path-components request) (split-and-decode-uri-path (uri-path (request-uri request)))) @@ -53,9 +57,10 @@ ((file-exists? (string-append habits-dir (car path))) (values '((content-type . (application/json))) (scm->json-string (list->vector - (map (lambda (row) (list->vector row)) - (call-with-input-file (string-append habits-dir (car path)) - parse-file)))))) + (iter-lines + (string-append habits-dir (car path)) + (lambda (line) + (list->vector (map string->number (string-split line #\:))))))))) ((string=? "hi" (car path)) (values '((content-type . (application/json))) '(hi))) |