From 56bd7782cd5ce62baad0d15d7eaa2333e1ca56e0 Mon Sep 17 00:00:00 2001 From: Oscar Najera Date: Sun, 23 Apr 2023 18:47:08 +0200 Subject: Loop lines on read-habit --- lib/guile/read-habit.scm | 37 +++++++++++++++++++++---------------- 1 file changed, 21 insertions(+), 16 deletions(-) (limited to 'lib/guile') 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))) -- cgit v1.2.3