From: Adam Faiz <adam.faiz@disroot.org>
To: guile-devel@gnu.org
Cc: Nala Ginrut <nalaginrut@gmail.com>,
Ricardo Wurmus <rekado@elephly.net>,
Maxime Devos <maximedevos@telenet.be>
Subject: [PATCH v2] rdelim: Add new procedure `for-line-in-file`.
Date: Mon, 16 Dec 2024 14:14:17 +0800 [thread overview]
Message-ID: <0c0bc19c-9b46-4da7-b200-3de0355949fa@disroot.org> (raw)
From c8a9904f1b1c09d148de1ec23dc2eb0d433b3141 Mon Sep 17 00:00:00 2001
From: AwesomeAdam54321 <adam.faiz@disroot.org>
Date: Sun, 15 Dec 2024 23:48:30 +0800
Subject: [PATCH v2] 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 | 20 +++++++++++++++++++-
1 file changed, 19 insertions(+), 1 deletion(-)
diff --git a/module/ice-9/rdelim.scm b/module/ice-9/rdelim.scm
index d2cd081d7..9b20c99cb 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,20 @@ 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.
+FILE can either be a filename string or an already opened input port.
+The corresponding port is closed upon completion.
+
+The line provided to PROC is guaranteed to be a string."
+ (let ((port
+ (if (input-port? file)
+ file
+ (open-input-file file))))
+ (let loop ((line (read-line port)))
+ (cond ((eof-object? line)
+ (close-port port))
+ (else
+ (proc line)
+ (loop (read-line port)))))))
--
2.41.0
next reply other threads:[~2024-12-16 6:14 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-12-16 6:14 Adam Faiz [this message]
2024-12-16 7:41 ` [PATCH v2] rdelim: Add new procedure `for-line-in-file` Nala Ginrut
2024-12-16 10:17 ` Maxime Devos
2024-12-16 10:29 ` Nala Ginrut
2024-12-16 10:52 ` Maxime Devos
2024-12-16 11:06 ` Nala Ginrut
2024-12-16 12:00 ` Maxime Devos
2024-12-16 12:33 ` Nala Ginrut
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.gnu.org/software/guile/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=0c0bc19c-9b46-4da7-b200-3de0355949fa@disroot.org \
--to=adam.faiz@disroot.org \
--cc=guile-devel@gnu.org \
--cc=maximedevos@telenet.be \
--cc=nalaginrut@gmail.com \
--cc=rekado@elephly.net \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).