unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: Emanuel Berg <incal@dataswamp.org>
To: emacs-devel@gnu.org
Cc: Philip Kaludercic <philipk@posteo.net>
Subject: Re: Add seq-shuffle
Date: Mon, 16 Sep 2024 07:08:44 +0200	[thread overview]
Message-ID: <87msk8p4mb.fsf@dataswamp.org> (raw)
In-Reply-To: 875xqy4oel.fsf@posteo.net

Philip Kaludercic wrote:

>> (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))
>
> If you are already using cl-lib, then you can simplify this with
>
>   (cl-rotatef (aref vec n) (aref vec i))

(seq-sort (lambda (_ __) (zerop (random 2))) seq)

-- 
underground experts united
https://dataswamp.org/~incal




  reply	other threads:[~2024-09-16  5:08 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 [this message]
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

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

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

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