unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: Joshua Lambert <jlambert.lis.tech@gmail.com>
To: Platon Pronko <platon7pronko@gmail.com>
Cc: help-gnu-emacs@gnu.org
Subject: Re: Iterating over buffer lines
Date: Fri, 16 Jun 2023 22:28:45 -0500	[thread overview]
Message-ID: <CAGGu9j1F1iDCWsF6q+-HwV2=g4SwU=myWT=m4YCC8=bpRZoBYA@mail.gmail.com> (raw)
In-Reply-To: <c5d0c647-cd6a-ef8f-fb2e-a26dfa50cb95@gmail.com>

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."
  ........

On Fri, Jun 16, 2023 at 7:10 PM Platon Pronko <platon7pronko@gmail.com> wrote:
>
> On 2023-06-17 06:19, Joshua Lambert wrote:
> > I have created some functions that make changes to one buffer line of
> > a csv file. Let's call those functions a, b and c. Each of those take
> > one or more arguments. I want to create a separate interactive
> > function that does what function a, b, and c do, but on every line of
> > the region. I successfully created a function (act-on-region-by-line)
> > to go through all lines in a region and then call function a, function
> > b, or function c. But, It seems redundant to have multiple functions
> > that have 9-15 lines similar and one line different, the one that
> > specifies function a, b or c. What is the best way to be efficient in
> > this situation?
> >
> > I have attempted to pass function b as a parameter, but I am slow to
> > understand how to do that. Is that the best way, or is there another?
> >
> > Thanks,
> > J Lambert
> >
>
> It's a bit difficult to understand what the problem is without seeing the code. Can you show the code for your latest attempt?
>
> Yes, passing function as a parameter seems to be the best way. And partial function application might be useful here as well.
>
> --
> Best regards,
> Platon Pronko
> PGP 2A62D77A7A2CB94E
>



  reply	other threads:[~2023-06-17  3:28 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 [this message]
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

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/emacs/

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

  git send-email \
    --in-reply-to='CAGGu9j1F1iDCWsF6q+-HwV2=g4SwU=myWT=m4YCC8=bpRZoBYA@mail.gmail.com' \
    --to=jlambert.lis.tech@gmail.com \
    --cc=help-gnu-emacs@gnu.org \
    --cc=platon7pronko@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.
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).