* [PATCH v1] rdelim: Add new procedure `for-line-in-file`.
@ 2024-12-16 3:05 Adam Faiz
2024-12-16 3:55 ` Nala Ginrut
0 siblings, 1 reply; 2+ messages in thread
From: Adam Faiz @ 2024-12-16 3:05 UTC (permalink / raw)
To: guile-devel; +Cc: Ricardo Wurmus, Maxime Devos
From 3c7e1af1f4812d0e4c5d10e841cf9546e5ba2887 Mon Sep 17 00:00:00 2001
From: AwesomeAdam54321 <adam.faiz@disroot.org>
Date: Sun, 15 Dec 2024 23:48:30 +0800
Subject: [PATCH v1] rdelim: Add new procedure `for-line-in-file`.
* module/ice-9/rdelim.scm (for-line-in-file): Add it.
This procedure makes it convenient to do per-line processing of a text
file.
---
module/ice-9/rdelim.scm | 12 +++++++++++-
1 file changed, 11 insertions(+), 1 deletion(-)
diff --git a/module/ice-9/rdelim.scm b/module/ice-9/rdelim.scm
index d2cd081d7..833639219 100644
--- a/module/ice-9/rdelim.scm
+++ b/module/ice-9/rdelim.scm
@@ -23,7 +23,8 @@
;;; similar to (scsh rdelim) but somewhat incompatible.
(define-module (ice-9 rdelim)
- #:export (read-line
+ #:export (for-line-in-file
+ read-line
read-line!
read-delimited
read-delimited!
@@ -206,3 +207,12 @@ characters to read. By default, there is no limit."
line)
(else
(error "unexpected handle-delim value: " handle-delim)))))
+
+(define (for-line-in-file file proc)
+ "Call PROC for every line in FILE until the eof-object is reached.
+The line provided to PROC is guaranteed to be a string."
+ (while #t
+ (let ((line (read-line file)))
+ (if (string? line)
+ (proc line)
+ (break)))))
--
2.41.0
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH v1] rdelim: Add new procedure `for-line-in-file`.
2024-12-16 3:05 [PATCH v1] rdelim: Add new procedure `for-line-in-file` Adam Faiz
@ 2024-12-16 3:55 ` Nala Ginrut
0 siblings, 0 replies; 2+ messages in thread
From: Nala Ginrut @ 2024-12-16 3:55 UTC (permalink / raw)
To: Adam Faiz; +Cc: guile-devel, Ricardo Wurmus, Maxime Devos
[-- Attachment #1: Type: text/plain, Size: 1943 bytes --]
Hi Adam!
I was confused with the name "file" here since it acts more like a port
rather than a filename. If it's expected to be a opened port passed from
the caller, it's better rename to "port".
In your context, I think you want to check if it's the end of file, so you
may use eof-object?
To make your patch more useful, IMHO, you may support filename string and
file port both. And close the port properly when it's end.
Best regards.
On Mon, Dec 16, 2024, 12:06 Adam Faiz <adam.faiz@disroot.org> wrote:
> From 3c7e1af1f4812d0e4c5d10e841cf9546e5ba2887 Mon Sep 17 00:00:00 2001
> From: AwesomeAdam54321 <adam.faiz@disroot.org>
> Date: Sun, 15 Dec 2024 23:48:30 +0800
> Subject: [PATCH v1] rdelim: Add new procedure `for-line-in-file`.
>
> * module/ice-9/rdelim.scm (for-line-in-file): Add it.
>
> This procedure makes it convenient to do per-line processing of a text
> file.
> ---
> module/ice-9/rdelim.scm | 12 +++++++++++-
> 1 file changed, 11 insertions(+), 1 deletion(-)
>
> diff --git a/module/ice-9/rdelim.scm b/module/ice-9/rdelim.scm
> index d2cd081d7..833639219 100644
> --- a/module/ice-9/rdelim.scm
> +++ b/module/ice-9/rdelim.scm
> @@ -23,7 +23,8 @@
> ;;; similar to (scsh rdelim) but somewhat incompatible.
>
> (define-module (ice-9 rdelim)
> - #:export (read-line
> + #:export (for-line-in-file
> + read-line
> read-line!
> read-delimited
> read-delimited!
> @@ -206,3 +207,12 @@ characters to read. By default, there is no limit."
> line)
> (else
> (error "unexpected handle-delim value: " handle-delim)))))
> +
> +(define (for-line-in-file file proc)
> + "Call PROC for every line in FILE until the eof-object is reached.
> +The line provided to PROC is guaranteed to be a string."
> + (while #t
> + (let ((line (read-line file)))
> + (if (string? line)
> + (proc line)
> + (break)))))
> --
> 2.41.0
>
>
[-- Attachment #2: Type: text/html, Size: 2657 bytes --]
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2024-12-16 3:55 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-12-16 3:05 [PATCH v1] rdelim: Add new procedure `for-line-in-file` Adam Faiz
2024-12-16 3:55 ` Nala Ginrut
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).