From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Tomas Volf <~@wolfsden.cz> Newsgroups: gmane.lisp.guile.devel Subject: Re: [PATCH v3] rdelim: Add new procedure `for-line-in-file`. Date: Mon, 16 Dec 2024 12:27:03 +0100 Message-ID: <87pllrkgvs.fsf@wolfsden.cz> References: Mime-Version: 1.0 Content-Type: multipart/signed; boundary="=-=-="; micalg=pgp-sha512; protocol="application/pgp-signature" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="33663"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Cc: guile-devel@gnu.org, Nala Ginrut , Ricardo Wurmus , Maxime Devos To: Adam Faiz Original-X-From: guile-devel-bounces+guile-devel=m.gmane-mx.org@gnu.org Mon Dec 16 12:27:39 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 1tN9GD-0008UE-SZ for guile-devel@m.gmane-mx.org; Mon, 16 Dec 2024 12:27:38 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tN9Fq-00087K-Jf; Mon, 16 Dec 2024 06:27:14 -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 <~@wolfsden.cz>) id 1tN9Fo-000878-UV for guile-devel@gnu.org; Mon, 16 Dec 2024 06:27:12 -0500 Original-Received: from wolfsden.cz ([37.205.8.62]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from <~@wolfsden.cz>) id 1tN9Fm-0001rv-Ng for guile-devel@gnu.org; Mon, 16 Dec 2024 06:27:12 -0500 Original-Received: by wolfsden.cz (Postfix, from userid 104) id AD8FF36F6AA; Mon, 16 Dec 2024 11:27:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=wolfsden.cz; s=mail; t=1734348425; bh=aqHxKeUyWQHq/8VDUv+vEbKAPKiml1YSialGTb7BUyw=; h=From:To:Cc:Subject:In-Reply-To:References:Date; b=hM7NVi3cU1xuHEWlXQUol9D3eQgOF71fQe9frcPK5/trnewBkGk9Uco6/qQTaEuwJ qotX9yWWhAi28Zf83unx4yrG2X444atKDw94jeBBFXT87qYPQT2Wn7C8RPfXx/TQ/y k5YK0RHhogT0Qs3JRPExv4BLaMHDY2zVipMwPX+Le5p/6ZkAnJdZHflZDTULN7jnb2 xXxwh4bnvAxwWtuM5i+n2pfzvg7QioxKGwtAgOFZQsp7IMSNKCBbArP2sZc2/gzSoc s00Lf3D2HBlnZUQ9trgxX8/trmjUSh64H27M6pV/B3GZGODQpoZZeyqnA4HDoWpFVc ODhcL2RrE4qnFS8ej6U4ycNtS9c3DHwItIWl2gXeWsDQRo4CMthLgI6hCf1r9ID7BV cnkAERdqluZSFKt2S8r15TyUQLU8yr9ROs1r/QDEs7KwfR+lQhC+EbAVoNTY0xnwT4 X+xwSDgWF2KK4SxhIFMxzFMjxAA14RpvN7BuxoTexAyZIBN4t2Uo6j5rhfA6RZj7kJ PP8oXtu3nAgjhz5Ip81j0xtU59XB9naoK7KnwkEQ5DrEigwoAkFFZcb1RygA5pqySK OMTLzCy40umoW822qXAt7EEc/xcv9FoIKr2nBuXRYkZkGsqCEuVKELcc3HGaFcPP6/ zYiAlFp5zPC6T0pjxLAYcsmY= Original-Received: from localhost (unknown [128.0.188.242]) by wolfsden.cz (Postfix) with ESMTPSA id 670E536F4B7; Mon, 16 Dec 2024 11:27:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=wolfsden.cz; s=mail; t=1734348424; bh=aqHxKeUyWQHq/8VDUv+vEbKAPKiml1YSialGTb7BUyw=; h=From:To:Cc:Subject:In-Reply-To:References:Date; b=nQ3gsQSP2dittlYaLz1gaf4OX54IKE76qWtULXBCWzY4OQHLGH7SlczFHFYw4oYYa OmF03Ib1BYhWtA+bTu/52BFARD2+8XSakve8vymprwXQhDqrfVNnf4Vz8jzUmdbAMc Ep8sTM84UZLTM4lbGIMT6Zz5gJQ1k+qMmGkpX3skf2GmBKOisi3A8D8hGq1BwMDuzD +SCdsxWmhW9fyQjuMqd2z4V6I2m6hkDKxGymiPkucYFQRYY+4hwV5SuwbVV+JLzFcE Dk3fUC8oSK6KjIW471zP6iNvDti4FiH7ZJJHOR7meLg5epo0PF85h9O6tWE2OMKFS2 npv862BI1DQkb3D4j6DKHA0XTZPCj/SreWXIP/fFxc9bTgTHNNPTR8Lk3ti0glOZSR fdLhFDZLRafiksWxefXTMD0nbTufRorlvU4aSM2ksY0pZ0FIF9TP9Awz/Wh5GZQya/ LjL06Wyl2iH+Ui/j6FQUIq6sxpYL2KwLrBnHAe2aC6iQIBmLuYdcJdN+X4ng9jiLFR rOXQhYf7sO6p6qhrS1UZkxtPy6QAt/JCFOVfPz7bsw3ebNLT7mzZbH8lZe99Ot6Nxs NKZxLPcgFWgMMKGfDrFWnn0Frfv9JPulc3K4xobEl5Oonm1jMh0r4Txig/DT/3z9hO wE8zXrs66FQWc7xZV+s0uss8= In-Reply-To: (Adam Faiz's message of "Mon, 16 Dec 2024 16:43:44 +0800") Mail-Followup-To: Adam Faiz , guile-devel@gnu.org, Nala Ginrut , Ricardo Wurmus , Maxime Devos Received-SPF: pass client-ip=37.205.8.62; envelope-from=~@wolfsden.cz; helo=wolfsden.cz 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, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001, SPF_HELO_PASS=-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:22826 Archived-At: --=-=-= Content-Type: text/plain Content-Transfer-Encoding: quoted-printable Adam Faiz writes: > From fe113e9efc08aae2a7e3792a1018c496212bd774 Mon Sep 17 00:00:00 2001 > From: AwesomeAdam54321 > Date: Sun, 15 Dec 2024 23:48:30 +0800 > Subject: [PATCH v3] 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 | 23 ++++++++++++++++++++++- It should also be documented in the manual I would think. > 1 file changed, 22 insertions(+), 1 deletion(-) > > diff --git a/module/ice-9/rdelim.scm b/module/ice-9/rdelim.scm > index d2cd081d7..b4c55c12e 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,23 @@ 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 What about naming it for-delimited-in-file and adding #:delims argument? That would allow reading files that have "lines" terminated with #\nul instead of just #\newline, which would be handy for processing output of shell commands (-z, -0, ...). Delims could default to "\n", so ergonomics of your use case would not be impacted. On completely separate note, having (fold|reduce)-delimited-in-file would be cool too (/me makes a note to write it). > + #:key (encoding #f) (guess-encoding #f)) > + "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 > + #:encoding encoding > + #:guess-encoding guess-encoding)))) > + (let loop ((line (read-line port))) > + (cond ((eof-object? line) > + (close-port port)) I know you were told that it should close the port, but I am not sure about it. It should close the port it opened, but should it also the one it got? This will prevent the same port being processed multiple times, which could be annoying. > + (else > + (proc line) > + (loop (read-line port))))))) Have a nice day, Tomas =2D-=20 There are only two hard things in Computer Science: cache invalidation, naming things and off-by-one errors. --=-=-= Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQJCBAEBCgAsFiEEt4NJs4wUfTYpiGikL7/ufbZ/wakFAmdgDocOHH5Ad29sZnNk ZW4uY3oACgkQL7/ufbZ/wak5RA//S8fI6pW9b9JQ79gH3nsyieOKONXKOSEUDbTm aorYgVYcnXm9sTCqQrKjMuDdfMcG6OrMnPMiw3r1BqWOavaQrctKMX/iKR3soYon EGqDl0qAjRMW6KB6NbF92aJzleUoUGnsNTJfRMj8cBZslDFMIRaB3hLfmTo+YOLO ct9JWKvDfUvrzD/Uv1IyUpjjRjrCk/tIr7LiWjIoGAGtJz4dYsiZwHWvwipGJhWS NRX7nvhTsDr4Wckb0JMvMIy+Q7j3hsOgjEp2tS0OhGMzyG7vZnfrbCGnkMEqRHhc gBHW1cDw7Gs36nkRPEP8PwjHqYMujxBorbJc+UT97LYjkStReU5oG0+VtkxUkkS5 5C/Jt1Eij3OzfWCZ4Z4JCPRKgoKsxJDAV5HxzsUtVjGcWNDjiXu1B41NSR5DpK9/ xdUHlysSa05n5tkEQZFLqEI7qsfY3f8WfcTJB1JOdT2grDpSlWyOT2rFc48EYXYk IXvjKgCuQfqpN6CKM0O1UCCiYVBv8a18yqhzOyw/XoHd0VLNLEc2ZHQgtglHWFeb FD9KYYh1OPGtNkD8Z3zccUqdkGzS8OgaodWgfh0a5cZ4zDQ4TvaKelfLK8Uyr2lZ dHjrfAYT5mLNgKt7E531EYH3fIRg8q4/nxIzfyb4GinvkZxzRHsJHN+yt3m+klDs nL5jNIo= =cUnO -----END PGP SIGNATURE----- --=-=-=--