all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Yuri Khan <yuri.v.khan@gmail.com>
To: Emanuel Berg <moasenwood@zoho.eu>,
	help-gnu-emacs <help-gnu-emacs@gnu.org>
Subject: Re: avoid narrow-to-region (was: Re: replace-regexp)
Date: Sun, 9 May 2021 19:27:30 +0700	[thread overview]
Message-ID: <CAP_d_8UyGxJu0noqO77YcWrSM6D+p5WU9=N9X8i8JvUNXS1hPQ@mail.gmail.com> (raw)
In-Reply-To: <875yzsrxd9.fsf@zoho.eu>

On Sun, 9 May 2021 at 17:54, Emanuel Berg via Users list for the GNU
Emacs text editor <help-gnu-emacs@gnu.org> wrote:

> Proposal: Instead of quoting computer science theory, why
> don't _you_ write some Elisp and show us how you think it
> should be done?

Mostly because I don’t usually write code to solve tasks I never
encounter in my own workflow. (Unless specifically employed to write
such code, or doing an interview with a prospective employer.)

> It isn't because it is easier and more comfortable to broadly
> criticize someone or something else, than to be active and
> creative oneself - is it?

I’m not criticizing you, I’m advising you about a possible issue with
your code. At the same time, I’m taking an opportunity to raise
general awareness of sorting predicate requirements.

Now, because you asked nicely, here’s my take on implementing the
random shuffle algorithm.

(defun yk-shuffle-lines (begin end)
  "Reorder lines between BEGIN and END randomly.
If BEGIN or END is in the middle of a line, that line is included.
Any markers inside the region may move
to the beginning or end of their respective lines,
and won’t follow the text they were associated with."
  (interactive "*r")
  (save-excursion
    (let ((nlines (count-lines begin end)))
      (goto-char begin)
      (while (> nlines 1)
        (let* ((begin1 (line-beginning-position))
               (line1 (let ((end1 (line-end-position)))
                        (prog1 (buffer-substring begin1 end1)
                          (delete-region begin1 end1))))
               (_ (forward-line (random nlines)))
               (line2 (let* ((begin2 (line-beginning-position))
                             (end2 (line-end-position)))
                        (prog1 (buffer-substring begin2 end2)
                          (delete-region begin2 end2)))))
          (insert line1)
          (goto-char begin1)
          (insert line2))
        (forward-line)
        (setq nlines (1- nlines))))))

(The structure of let*/let/let* bindings may seem excessively complex,
but I’m taking care here to not expose bindings beyond their validity.
E.g. as soon as (delete-region begin1 end1) is executed, end1 no
longer refers to anything meaningful, so I drop that binding asap.)



  reply	other threads:[~2021-05-09 12:27 UTC|newest]

Thread overview: 41+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-05-06 23:06 replace-regexp Emanuel Berg via Users list for the GNU Emacs text editor
2021-05-07  6:54 ` replace-regexp Tassilo Horn
2021-05-07 18:28   ` replace-regexp Emanuel Berg via Users list for the GNU Emacs text editor
2021-05-08  0:02     ` replace-regexp Emanuel Berg via Users list for the GNU Emacs text editor
2021-05-08  0:16       ` replace-regexp Emanuel Berg via Users list for the GNU Emacs text editor
2021-05-08  5:38       ` replace-regexp Yuri Khan
2021-05-08 13:53         ` replace-regexp Stefan Monnier via Users list for the GNU Emacs text editor
2021-05-08 18:41           ` replace-regexp Emanuel Berg via Users list for the GNU Emacs text editor
2021-05-08 18:50             ` replace-regexp Stefan Monnier via Users list for the GNU Emacs text editor
2021-05-08 21:59               ` replace-regexp Emanuel Berg via Users list for the GNU Emacs text editor
2021-05-08 22:03                 ` replace-regexp Tassilo Horn
2021-05-08 22:25                   ` replace-regexp Emanuel Berg via Users list for the GNU Emacs text editor
2021-05-08 22:54                     ` [External] : replace-regexp Drew Adams
2021-05-09  2:48             ` replace-regexp Emanuel Berg via Users list for the GNU Emacs text editor
2021-05-08 18:46           ` replace-regexp Emanuel Berg via Users list for the GNU Emacs text editor
2021-05-08 21:10             ` replace-regexp Stefan Monnier via Users list for the GNU Emacs text editor
2021-05-08 21:54               ` replace-regexp Emanuel Berg via Users list for the GNU Emacs text editor
2021-05-08 23:11                 ` replace-regexp Stefan Monnier via Users list for the GNU Emacs text editor
2021-05-08 23:16                   ` replace-regexp Emanuel Berg via Users list for the GNU Emacs text editor
2021-05-08 23:46                     ` replace-regexp Stefan Monnier via Users list for the GNU Emacs text editor
2021-05-08 23:51                       ` replace-regexp Emanuel Berg via Users list for the GNU Emacs text editor
2021-05-09  7:38                       ` replace-regexp Emanuel Berg via Users list for the GNU Emacs text editor
2021-05-08 22:59           ` avoid narrow-to-region (was: Re: replace-regexp) Emanuel Berg via Users list for the GNU Emacs text editor
2021-05-09  5:48             ` Yuri Khan
2021-05-09  6:09               ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-05-09  6:34                 ` Yuri Khan
2021-05-09  6:59                   ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-05-09  7:22                     ` Jean Louis
2021-05-09  7:40                       ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-05-09 10:06                     ` Yuri Khan
2021-05-09 10:54                       ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-05-09 12:27                         ` Yuri Khan [this message]
2021-05-09 12:43                           ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-05-09 13:14                           ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-05-09 14:04                             ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-05-09 15:13                               ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-05-21 17:35                                 ` same sound random sort everywhere (was: Re: avoid narrow-to-region (was: Re: replace-regexp)) Emanuel Berg via Users list for the GNU Emacs text editor
2021-05-21 20:12                                   ` Jean Louis
2021-05-21 20:47                                     ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-05-07  8:02 ` replace-regexp Jean Louis
2021-05-07 18:29   ` replace-regexp Emanuel Berg via Users list for the GNU Emacs text editor

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='CAP_d_8UyGxJu0noqO77YcWrSM6D+p5WU9=N9X8i8JvUNXS1hPQ@mail.gmail.com' \
    --to=yuri.v.khan@gmail.com \
    --cc=help-gnu-emacs@gnu.org \
    --cc=moasenwood@zoho.eu \
    /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.