Re: [PATCH 1/2] rdelim: Add new procedure `for-rdelim-in-port`.

Nala Ginrut <[email protected]>
Newsgroups gmane.lisp.guile.devel
Message-ID <CAPjoZod7Q3sGmfuS+v8mO8gEzOKD9z8fCONcBrKoaK1ZY4DiOQ@mail.gmail.com>
Thanks for the work!
I didn’t 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’t insist on my previous suggestion right
now. There may be arguments against it by folks. I’m fine with any.

2. To complete a promising patch, it’s better to have 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 <[email protected]> wrote:

> From 302159fe61d9df526911ead8ea6ad823ad8b0443 Mon Sep 17 00:00:00 2001
> From: AwesomeAdam54321 <[email protected]>
> 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 returns
> #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
>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.