From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Nala Ginrut Newsgroups: gmane.lisp.guile.devel Subject: Re: [PATCH 1/2] rdelim: Add new procedure `for-rdelim-in-port`. Date: Tue, 17 Dec 2024 01:46:18 +0900 Message-ID: References: Mime-Version: 1.0 Content-Type: multipart/alternative; boundary="000000000000256841062965ec7e" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="32828"; mail-complaints-to="usenet@ciao.gmane.io" Cc: guile-devel@gnu.org, Ricardo Wurmus , Tomas Volf <~@wolfsden.cz>, Maxime Devos To: Adam Faiz Original-X-From: guile-devel-bounces+guile-devel=m.gmane-mx.org@gnu.org Mon Dec 16 17:47:48 2024 Return-path: Envelope-to: guile-devel@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1tNEG3-0008Qa-Jh for guile-devel@m.gmane-mx.org; Mon, 16 Dec 2024 17:47:47 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tNEFj-0001Zk-2u; Mon, 16 Dec 2024 11:47:28 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tNEEt-0001Wy-9u for guile-devel@gnu.org; Mon, 16 Dec 2024 11:46:54 -0500 Original-Received: from mail-pg1-x52d.google.com ([2607:f8b0:4864:20::52d]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1tNEEr-0003bs-FI for guile-devel@gnu.org; Mon, 16 Dec 2024 11:46:35 -0500 Original-Received: by mail-pg1-x52d.google.com with SMTP id 41be03b00d2f7-7fc99fc2b16so2913539a12.3 for ; Mon, 16 Dec 2024 08:46:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1734367590; x=1734972390; darn=gnu.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=Uu1ztNQCKP4frz4TAHJYn7HBu1CH3EYlhvvnhDwDP0k=; b=nJzEjKqxJ7wZeelXyjPLx7DrHMtacUvaXFbFEKTNdj1sRnuAkDovsLwUHpA1RcSAyR OnJptK3f2/AjGsadUDFtEI7oQbc0P7QyRN64kgXIC4FdnY98z3k++5pnWXSSjf026s7a p5oQ60BqCnYS25iJvTUHJ6EX0TitMe0P0OM369rDhQmSb1CsJv18esFxwPO+Y77dpj4H KuIrtMS4Uz/v7Gw5kh+mqoLyRS/1fnHsrz7w8wK0GM7WEzC/7FY7A+IV9AJGiMMovvA9 NlgC42Wp7LQ2Dws/PK6FdF6pZ3dPEgafZRH2hTFd4ozu2TATH4pXLZdKD6kX2YkJeGNT u2IA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1734367590; x=1734972390; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=Uu1ztNQCKP4frz4TAHJYn7HBu1CH3EYlhvvnhDwDP0k=; b=p880xACzY5HwgPsXtnC43ERfDtWj97RdzgoQKLREcZ5Qj7Dw9tzw4ToTkTdx/uPPK6 q3IUj+oqLFjEJ7Iw99VxxcIl7mzhGr/j9lVMT55nOUYMD3tVKT+0V6G+mVtNHo4IPsCS RGJXPcJipzdP0/9G13vuy/kLLelwLDAMQ1G5cQlHYkzyxwv0XGeQ6A0zjVBsHK5+O/hg i2k7j6h1PzqlH9fgEfzXIoMGTaqD0t1Ly6WYjFd3GXmn3V8mFNJj37sj+VcD4CQphwZp FfGBU1ZhiAGAXlOToHuYn8Cywmc3usGfKGZ33O3Bj8mH+xdqQlpnNIxQdRrIh2OvD1da PHAA== X-Gm-Message-State: AOJu0YwpTMwMANhyeXgJyiPt4VWKBSULetzOBU3apzxX998Wm0EY+450 nkF3/1Ok5Dl5rP7q3RnJk+IGjfR8I0qwAUKn677Yyo9YLWOwK9hB8yZrFKpowocK3LnimTtRhqJ 8iUPaVoKCSEQWyOe6vAQ0cW5Vtj4= X-Gm-Gg: ASbGncsf5/MvRM8wkXJGCact6la9bwJHT8ORlUIhyOnQSMNyizLpGx7G6qNJVIyGHuE RAhXoU3dws9X3doCvNtKc4+GfEPf19C3bSYgu X-Google-Smtp-Source: AGHT+IGmBhD8fYVc0x1+PXGmxcHfuj4G83LRe4nImgfYhXavVcMv4VLLQaBjdnNbmmpM9zn5eSJ/kESxucWjKWG53q8= X-Received: by 2002:a17:90b:4a4f:b0:2ee:9902:18b4 with SMTP id 98e67ed59e1d1-2f2900a9878mr19104286a91.27.1734367590571; Mon, 16 Dec 2024 08:46:30 -0800 (PST) In-Reply-To: Received-SPF: pass client-ip=2607:f8b0:4864:20::52d; envelope-from=nalaginrut@gmail.com; helo=mail-pg1-x52d.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, HTML_MESSAGE=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: guile-devel@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "Developers list for Guile, the GNU extensibility library" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guile-devel-bounces+guile-devel=m.gmane-mx.org@gnu.org Original-Sender: guile-devel-bounces+guile-devel=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.lisp.guile.devel:22834 Archived-At: --000000000000256841062965ec7e Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Thanks for the work! I didn=E2=80=99t find anything to be picky about yet in the implementation. Now there are two things could go further. 1. About the port closing, I don=E2=80=99t insist on my previous suggestion= right now. There may be arguments against it by folks. I=E2=80=99m fine with any. 2. To complete a promising patch, it=E2=80=99s better to have test cases. Y= ou may take a look at the existing tests to learn how. Best regards. On Reiwa 6 Dec 17, Tue at 0:22 Adam Faiz wrote: > From 302159fe61d9df526911ead8ea6ad823ad8b0443 Mon Sep 17 00:00:00 2001 > From: AwesomeAdam54321 > Date: Sun, 15 Dec 2024 23:48:30 +0800 > Subject: [PATCH 1/2] rdelim: Add new procedure `for-rdelim-in-port`. > > * module/ice-9/rdelim.scm (for-rdelim-in-port): Add it. > (for-delimited-in-port): Define as a specialised `for-rdelim-in-port`. > (for-line-in-file): Define as a specialised `for-delimited-in-port`. > > These procedures provide a backbone for parsing, with domain-specific > logic implemented as a separate procedure to be passed as an argument. > --- > module/ice-9/rdelim.scm | 35 ++++++++++++++++++++++++++++++++++- > 1 file changed, 34 insertions(+), 1 deletion(-) > > diff --git a/module/ice-9/rdelim.scm b/module/ice-9/rdelim.scm > index d2cd081d7..0bbaaf904 100644 > --- a/module/ice-9/rdelim.scm > +++ b/module/ice-9/rdelim.scm > @@ -23,7 +23,10 @@ > ;;; similar to (scsh rdelim) but somewhat incompatible. > > (define-module (ice-9 rdelim) > - #:export (read-line > + #:export (for-delimited-in-port > + for-line-in-file > + for-rdelim-in-port > + read-line > read-line! > read-delimited > read-delimited! > @@ -206,3 +209,33 @@ characters to read. By default, there is no limit." > line) > (else > (error "unexpected handle-delim value: " handle-delim))))) > + > +(define* (for-rdelim-in-port port proc rdelim-proc > + #:key (stop-pred eof-object?)) > + "Call PROC for every (RDELIM-PROC PORT) in PORT until STOP-PRED return= s > #t. > +RDELIM-PROC has to advance through PORT with every call." > + (let loop ((rdelim (rdelim-proc port))) > + (cond ((stop-pred rdelim) > + (close-port port)) > + (else > + (proc rdelim) > + (loop (rdelim-proc port)))))) > + > +(define* (for-delimited-in-port port proc > + #:key (delims "\n") (handle-delim 'trim)) > + "Call PROC for every delimited line in PORT until the eof-object is > reached." > + (for-rdelim-in-port port proc > + (lambda (port) > + (read-delimited delims port handle-delim)))) > + > +(define* (for-line-in-file file proc > + #:key (encoding #f) (guess-encoding #f)) > + "Call PROC for every line in FILE until the eof-object is reached. > +FILE must be a filename string. > + > +The line provided to PROC is guaranteed to be a string." > + (call-with-input-file > + file > + (lambda (port) > + (for-delimited-in-port port proc)) > + #:encoding encoding #:guess-encoding guess-encoding)) > -- > 2.41.0 > --000000000000256841062965ec7e Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Thanks for the work!
I didn=E2=80= =99t find anything to be picky about yet in the implementation.

Now there are two things could go f= urther.
1. About the port closing, I don=E2=80=99t i= nsist on my previous suggestion right now. There may be arguments against i= t by folks. I=E2=80=99m fine with any.

2. To complete a promising patch, it=E2=80=99s better to hav= e test cases. You may take a look at the existing tests to learn how.=

Best regards.

On Reiwa 6 Dec 17, Tue at 0:22 Adam Faiz <adam.faiz@disroot.org> wrote:
From 302159fe61d9df526911ead8ea6ad823ad8b0443 Mon Sep 17 00:= 00:00 2001
From: AwesomeAdam54321 <adam.faiz@disroot.org>
Date: Sun, 15 Dec 2024 23:48:30 +0800
Subject: [PATCH 1/2] rdelim: Add new procedure `for-rdelim-in-port`.

* module/ice-9/rdelim.scm (for-rdelim-in-port): Add it.
(for-delimited-in-port): Define as a specialised `for-rdelim-in-port`.
(for-line-in-file): Define as a specialised `for-delimited-in-port`.

These procedures provide a backbone for parsing, with domain-specific
logic implemented as a separate procedure to be passed as an argument.
---
=C2=A0module/ice-9/rdelim.scm | 35 ++++++++++++++++++++++++++++++++++-
=C2=A01 file changed, 34 insertions(+), 1 deletion(-)

diff --git a/module/ice-9/rdelim.scm b/module/ice-9/rdelim.scm
index d2cd081d7..0bbaaf904 100644
--- a/module/ice-9/rdelim.scm
+++ b/module/ice-9/rdelim.scm
@@ -23,7 +23,10 @@
=C2=A0;;; similar to (scsh rdelim) but somewhat incompatible.

=C2=A0(define-module (ice-9 rdelim)
-=C2=A0 #:export (read-line
+=C2=A0 #:export (for-delimited-in-port
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 for-line-in-file
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 for-rdelim-in-port
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 read-line
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0read-line!
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0read-delimited
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0read-delimited!
@@ -206,3 +209,33 @@ characters to read.=C2=A0 By default, there is no limi= t."
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 line)
=C2=A0 =C2=A0 =C2=A0 =C2=A0(else
=C2=A0 =C2=A0 =C2=A0 =C2=A0 (error "unexpected handle-delim value: &qu= ot; handle-delim)))))
+
+(define* (for-rdelim-in-port port proc rdelim-proc
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0#:key (stop-pred eof-object?))
+=C2=A0 "Call PROC for every (RDELIM-PROC PORT) in PORT until STOP-PRE= D returns #t.
+RDELIM-PROC has to advance through PORT with every call."
+=C2=A0 (let loop ((rdelim (rdelim-proc port)))
+=C2=A0 =C2=A0 (cond ((stop-pred rdelim)
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0(close-port port))
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (else
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0(proc rdelim)
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0(loop (rdelim-proc port))))))
+
+(define* (for-delimited-in-port port proc
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0#:key (delims "\n") (handle= -delim 'trim))
+=C2=A0 "Call PROC for every delimited line in PORT until the eof-obje= ct is reached."
+=C2=A0 (for-rdelim-in-port port proc
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0(lambda (port)
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0(read-delimited delims port handle-delim))))
+
+(define* (for-line-in-file file proc
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0#:key (encoding #f) (guess-encoding #f))
+=C2=A0 "Call PROC for every line in FILE until the eof-object is reac= hed.
+FILE must be a filename string.
+
+The line provided to PROC is guaranteed to be a string."
+=C2=A0 (call-with-input-file
+=C2=A0 =C2=A0 =C2=A0 file
+=C2=A0 =C2=A0 (lambda (port)
+=C2=A0 =C2=A0 =C2=A0 (for-delimited-in-port port proc))
+=C2=A0 =C2=A0 #:encoding encoding #:guess-encoding guess-encoding))
--
2.41.0
--000000000000256841062965ec7e--