all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Philip Kaludercic <philipk@posteo.net>
To: Adam Porter <adam@alphapapa.net>
Cc: incal@dataswamp.org,  emacs-devel@gnu.org
Subject: Re: Add seq-shuffle
Date: Tue, 17 Sep 2024 18:37:18 +0000	[thread overview]
Message-ID: <87a5g62kkh.fsf@posteo.net> (raw)
In-Reply-To: <14bc4905-38b4-459d-8522-7cc33bbadd18@alphapapa.net> (Adam Porter's message of "Mon, 16 Sep 2024 14:17:14 -0500")

Adam Porter <adam@alphapapa.net> writes:

> Hi Emanuel,
>
> That's a neat and concise solution. It seems to produce a decently
> random sorting, but I'd guess that it may not perform well for large
> sequences.  For example:
>
>     (let (comparisons)
>       (list :result (seq-sort (lambda (a b)
>                                 (push (cons a b) comparisons)
>                                 (zerop (random 2)))
>                               (number-sequence 0 10))
>             :num-comparisons (length comparisons)))
>     ;; (:result (6 0 9 1 5 10 8 2 7 3 4) :num-comparisons 26)
>
> In testing that expression repeatedly, I see that the number of
> comparisons varies between about 23 and 26.
>
> Thanks for pointing it out, though.  It's good to know that it exists
> and in what circumstances it could be useful.

IIRC the approach is related to the "Naïve method" mentioned on
Wikipedia [0].  I think that this variation of your code demonstrates
that not all elements are considered equally often:

  (let ((comparisons '()))
    (list :result
          (seq-sort (lambda (a b)
                              (cl-incf (alist-get a comparisons 0))
                              (cl-incf (alist-get b comparisons 0))
                              (zerop (random 2)))
                            (number-sequence 0 10))
          :num-comparisons
          (seq-sort (lambda (x y)
                      (< (cdr x) (cdr y)))
                    comparisons)))
  ;; (:result
  ;;  (9 0 1 7 4 10 5 6 8 3 2)
  ;;  :num-comparisons
  ;;  ((0 . 2) (10 . 3) (8 . 3) (9 . 4) (7 . 4) (6 . 4) (4 . 5) (2 . 5) (3 . 7) (1 . 7) (5 . 8)))

While acceptable as a personal hack, it a proper solution should do
Fisher–Yates.  Adding a "shuffle" function to Emacs would also eliminate
the excuse to use suboptimal fallbacks.

[0] https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle#Na%C3%AFve_method

>
> --Adam
>
>

-- 
	Philip Kaludercic on siskin



  parent reply	other threads:[~2024-09-17 18:37 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-09-14  7:48 Add seq-shuffle Hugo Thunnissen
2024-09-14  8:42 ` Philip Kaludercic
2024-09-16  5:08   ` Emanuel Berg
2024-09-16 19:17     ` Adam Porter
2024-09-17  4:09       ` Emanuel Berg
2024-09-17 18:37       ` Philip Kaludercic [this message]
2024-09-17 22:26         ` Adam Porter
2024-09-18  1:27         ` Emanuel Berg
2024-09-17 18:53     ` Yuri Khan
2024-09-15  1:44 ` Adam Porter
2024-09-15  6:27   ` Eli Zaretskii
2024-09-15  6:45     ` Adam Porter
2024-09-15 11:58       ` Stefan Kangas
2024-09-15  7:48   ` Hugo Thunnissen

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=87a5g62kkh.fsf@posteo.net \
    --to=philipk@posteo.net \
    --cc=adam@alphapapa.net \
    --cc=emacs-devel@gnu.org \
    --cc=incal@dataswamp.org \
    /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.