all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Add seq-shuffle
@ 2024-09-14  7:48 Hugo Thunnissen
  2024-09-14  8:42 ` Philip Kaludercic
  2024-09-15  1:44 ` Adam Porter
  0 siblings, 2 replies; 14+ messages in thread
From: Hugo Thunnissen @ 2024-09-14  7:48 UTC (permalink / raw)
  To: Emacs-devel@gnu.org


Hi,

I a function to randomize the order of a sequence. It is an
implementation of the Fisher-Yates shuffle algorithm (as I understand it
from wikipedia 🙃). I needed to randomize the order of a large list
(1000000 elements) and noticed that converting it to a vector made an
enormous difference in performance, so I opted to convert the sequence
to a vector before shuffling it.

Is there any interest to add this or a similar function to the seq
library?


(defun seq-shuffle (sequence)
  (cl-assert (sequencep sequence))

  (let* ((vec (seq-into sequence 'vector))
         (length (length vec))
         (n 0)
         (seq-type (type-of sequence)))
    (while (< n length)
      (let ((i (+ n (random (- length n))))
            (tmp (aref vec n)))
        (setf (aref vec n) (aref vec i)
              (aref vec i) tmp))
      (cl-incf n))

    (seq-into vec (if (eq 'cons seq-type) 'list seq-type))))

- Hugo



^ permalink raw reply	[flat|nested] 14+ messages in thread

end of thread, other threads:[~2024-09-18  1:27 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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
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

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.