all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Platon Pronko <platon7pronko@gmail.com>
To: Joshua Lambert <jlambert.lis.tech@gmail.com>
Cc: help-gnu-emacs@gnu.org
Subject: Re: Iterating over buffer lines
Date: Sun, 18 Jun 2023 09:17:14 +0800	[thread overview]
Message-ID: <50e7547c-03ab-4c5d-4c38-83c09194684e@gmail.com> (raw)
In-Reply-To: <CAGGu9j1F1iDCWsF6q+-HwV2=g4SwU=myWT=m4YCC8=bpRZoBYA@mail.gmail.com>

On 2023-06-17 11:28, Joshua Lambert wrote:
> OK. See below. I'm trying to avoid reusing most of the code in
> function my-act-on-region-by-line. There are multiple functions that
> correct data errors in a csv file and I want to be able to
> interactively run one of those corrections at a time. The resulting
> string will be inserted at the beginning of each line. That all works,
> I'm just trying not to have so much code repetition.
> 
> (defun my-act-on-region-by-line (some-function &optional beg end)
>    "Perform a function on the current line or each line of the region.
> Each SOME-FUNCTION must return a string."
>    (let ((beg2 (if (region-active-p)
>                          beg
>                        (line-beginning-position)))
>            (end2 (if (region-active-p)
>                           end
>                        (line-end-position))))
>      (save-excursion
>        (save-restriction
>           (narrow-to-region beg2 end2)
>           (goto-char (point-min))
>           (while (not (eobp))
>              (goto-char (line-beginning-position))
>              (insert some-chosen-function) ;; Inserts text after a
> function transforms it.
>              (insert my-separator)
>              (forward-line)))))
> 
> (defun my-paste-corrected-spacing (&optional field-num beg end)
>    "Correct spacing in a string and paste it at the beginning of a line.
> Interactively, BEG and END are the region."
>    (interactive "*p\nr")
>    (my-act-on-region-by-line (some-function-to-correct-spacing field-num)
>     beg
>     end))
> 
> (defun some-function-to-correct-spacing (field-num)
>    "Function that creates a correctly spaced string."
>    ........
> 

The code you pasted doesn't seem to work, for example because some-chosen-function doesn't exist. So I have trouble understanding what your problem is - is the pasted code fragment the example of duplication, or your approach to reducing duplication in code that I don't see?

If it's example of duplication (between my-paste-corrected-spacing and some other similar functions), then it looks like the duplication is minimal - function name, docstring, and underlying function are different. So actual duplication is 2-3 lines - `(&optional field-num beg end)`, `(interactive "*p\nr")`, and `my-act-on-region-by-line` parts. You can create a macro to reduce that duplication even further, but it seems that we are hitting diminishing returns here.

If the code as shown is already an attempt to reduce duplication then it looks like you are almost there - you just need to use `(apply some-function)` to call the function that you pass to my-act-on-region-by-line.



      parent reply	other threads:[~2023-06-18  1:17 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-06-16 22:19 Iterating over buffer lines Joshua Lambert
2023-06-17  0:10 ` Platon Pronko
2023-06-17  3:28   ` Joshua Lambert
2023-06-17  4:48     ` tomas
     [not found]       ` <CAGGu9j1gub_8kb92kkD30QmxLmZz8XJrwhyAPtYONE3OLe_zew@mail.gmail.com>
2023-06-17 19:17         ` tomas
2023-06-18  1:17     ` Platon Pronko [this message]

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

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=50e7547c-03ab-4c5d-4c38-83c09194684e@gmail.com \
    --to=platon7pronko@gmail.com \
    --cc=help-gnu-emacs@gnu.org \
    --cc=jlambert.lis.tech@gmail.com \
    /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.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.