emacs-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

master 35d0675: Prefer seq-random-elt to nth+random


From: Stefan Kangas
Subject: master 35d0675: Prefer seq-random-elt to nth+random
Date: Fri, 24 Sep 2021 14:02:19 -0400 (EDT)

branch: master
commit 35d0675467e61aff30c21544f87f55b1b1a2cfd3
Author: Stefan Kangas <stefan@marxist.se>
Commit: Stefan Kangas <stefan@marxist.se>

    Prefer seq-random-elt to nth+random
    
    * lisp/emacs-lisp/seq.el (seq-random-elt): Autoload.
    * lisp/avoid.el (mouse-avoidance-random-shape):
    * lisp/epa-ks.el (epa-ks--query-url):
    * lisp/erc/erc-networks.el (erc-server-select):
    * lisp/gnus/gnus-fun.el (gnus--random-face-with-type)
    (gnus-fun-ppm-change-string):
    * lisp/net/soap-inspect.el (soap-sample-value-for-xs-simple-type):
    * lisp/obsolete/landmark.el (landmark-random-move):
    * lisp/play/mpuz.el (mpuz-build-random-perm):
    * lisp/play/zone.el (zone-pgm-stress):
    * lisp/vc/add-log.el (add-change-log-entry):
    * test/lisp/net/tramp-tests.el
    (tramp-test44-asynchronous-requests): Prefer seq-random-elt to
    nth+random.
---
 lisp/avoid.el                | 10 +++-------
 lisp/emacs-lisp/seq.el       |  1 +
 lisp/epa-ks.el               |  3 +--
 lisp/erc/erc-networks.el     |  2 +-
 lisp/gnus/gnus-fun.el        |  4 ++--
 lisp/net/soap-inspect.el     |  4 ++--
 lisp/obsolete/landmark.el    |  2 +-
 lisp/play/mpuz.el            |  2 +-
 lisp/play/zone.el            |  2 +-
 lisp/vc/add-log.el           |  3 +--
 test/lisp/net/tramp-tests.el |  5 ++---
 11 files changed, 16 insertions(+), 22 deletions(-)

diff --git a/lisp/avoid.el b/lisp/avoid.el
index d3afecf..03707d1 100644
--- a/lisp/avoid.el
+++ b/lisp/avoid.el
@@ -43,7 +43,7 @@
 ;;
 ;; (if (eq window-system 'x)
 ;;     (mouse-avoidance-set-pointer-shape
-;;          (nth (random 4)
+;;          (seq-random-elt
 ;;               (list x-pointer-man x-pointer-spider
 ;;                     x-pointer-gobbler x-pointer-gumby))))
 ;;
@@ -125,7 +125,6 @@ TOP-OR-BOTTOM-POS: Distance from top or bottom edge of 
frame or window."
 ;; Internal variables
 (defvar mouse-avoidance-state nil)
 (defvar mouse-avoidance-pointer-shapes nil)
-(defvar mouse-avoidance-n-pointer-shapes 0)
 (defvar mouse-avoidance-old-pointer-shape nil)
 (defvar mouse-avoidance-animating-pointer nil)
 
@@ -306,11 +305,8 @@ redefine this function to suit your own tastes."
                      (all-completions "x-pointer-" obarray
                                       (lambda (x)
                                          (and (boundp x)
-                                              (integerp (symbol-value x)))))))
-       (setq mouse-avoidance-n-pointer-shapes
-             (length mouse-avoidance-pointer-shapes))))
-  (nth (random mouse-avoidance-n-pointer-shapes)
-       mouse-avoidance-pointer-shapes))
+                                               (integerp (symbol-value 
x)))))))))
+  (seq-random-elt mouse-avoidance-pointer-shapes))
 
 (defun mouse-avoidance-ignore-p ()
   (let ((mp (mouse-position)))
diff --git a/lisp/emacs-lisp/seq.el b/lisp/emacs-lisp/seq.el
index ae59882..baafc51 100644
--- a/lisp/emacs-lisp/seq.el
+++ b/lisp/emacs-lisp/seq.el
@@ -570,6 +570,7 @@ SEQUENCE must be a sequence of numbers or markers."
 If no element is found, return nil."
   (ignore-errors (seq-elt sequence n)))
 
+;;;###autoload
 (cl-defgeneric seq-random-elt (sequence)
   "Return a random element from SEQUENCE.
 Signal an error if SEQUENCE is empty."
diff --git a/lisp/epa-ks.el b/lisp/epa-ks.el
index ebdb127..35caa1a 100644
--- a/lisp/epa-ks.el
+++ b/lisp/epa-ks.el
@@ -149,8 +149,7 @@ If EXACT is non-nil, don't accept approximate matches."
           (cond ((null epa-keyserver)
                  (user-error "Empty keyserver pool"))
                 ((listp epa-keyserver)
-                 (nth (random (length epa-keyserver))
-                      epa-keyserver))
+                 (seq-random-elt epa-keyserver))
                 ((stringp epa-keyserver)
                  epa-keyserver)
                 ((error "Invalid type for `epa-keyserver'")))
diff --git a/lisp/erc/erc-networks.el b/lisp/erc/erc-networks.el
index 1c7742a..678c596 100644
--- a/lisp/erc/erc-networks.el
+++ b/lisp/erc/erc-networks.el
@@ -824,7 +824,7 @@ As an example:
         (ports (if (listp (nth 3 srv))
                    (erc-ports-list (nth 3 srv))
                  (list (nth 3 srv))))
-        (port (nth (random (length ports)) ports)))
+         (port (and ports (seq-random-elt ports))))
     (erc :server host :port port)))
 
 ;;; The following experimental
diff --git a/lisp/gnus/gnus-fun.el b/lisp/gnus/gnus-fun.el
index 8bca4ff..bfc57a2 100644
--- a/lisp/gnus/gnus-fun.el
+++ b/lisp/gnus/gnus-fun.el
@@ -103,7 +103,7 @@ PNG format."
             (remove nil (mapcar
                          (lambda (f) (unless (string-match (or omit "^$") f) 
f))
                          (directory-files dir t ext))))
-           (file (nth (random (length files)) files)))
+           (file (and files (seq-random-elt files))))
       (when file
         (funcall fun file)))))
 
@@ -315,7 +315,7 @@ colors of the displayed X-Faces."
   (let* ((possibilities '("%02x0000" "00%02x00" "0000%02x"
                          "%02x%02x00" "00%02x%02x" "%02x00%02x"))
         (format (concat "'#%02x%02x%02x' '#"
-                        (nth (random 6) possibilities)
+                         (seq-random-elt possibilities)
                         "'"))
         (values nil))
   (dotimes (i 255)
diff --git a/lisp/net/soap-inspect.el b/lisp/net/soap-inspect.el
index b994b0e..5207ca8 100644
--- a/lisp/net/soap-inspect.el
+++ b/lisp/net/soap-inspect.el
@@ -114,7 +114,7 @@ This is a specialization of `soap-sample-value' for
    (cond
     ((soap-xs-simple-type-enumeration type)
      (let ((enumeration (soap-xs-simple-type-enumeration type)))
-       (nth (random (length enumeration)) enumeration)))
+       (and enumeration (seq-random-elt enumeration))))
     ((soap-xs-simple-type-pattern type)
      (format "a string matching %s" (soap-xs-simple-type-pattern type)))
     ((soap-xs-simple-type-length-range type)
@@ -134,7 +134,7 @@ This is a specialization of `soap-sample-value' for
         (t (random 100)))))
     ((consp (soap-xs-simple-type-base type)) ; an union of values
      (let ((base (soap-xs-simple-type-base type)))
-       (soap-sample-value (nth (random (length base)) base))))
+       (soap-sample-value (and base (seq-random-elt base)))))
     ((soap-xs-basic-type-p (soap-xs-simple-type-base type))
      (soap-sample-value (soap-xs-simple-type-base type))))))
 
diff --git a/lisp/obsolete/landmark.el b/lisp/obsolete/landmark.el
index cc4fd19..83e7649 100644
--- a/lisp/obsolete/landmark.el
+++ b/lisp/obsolete/landmark.el
@@ -1470,7 +1470,7 @@ push him out of it."
   (mapc
    (lambda (direction) (put direction 'y_t 0))
    landmark-directions)
-  (dolist (direction (nth (random 8) landmark-8-directions))
+  (dolist (direction (seq-random-elt landmark-8-directions))
     (put direction 'y_t 1.0))
   (landmark-move))
 
diff --git a/lisp/play/mpuz.el b/lisp/play/mpuz.el
index ff174fe..df2b6fc 100644
--- a/lisp/play/mpuz.el
+++ b/lisp/play/mpuz.el
@@ -153,7 +153,7 @@ You may abort a game by typing 
\\<mpuz-mode-map>\\[mpuz-offer-abort]."
        (index 10)
        elem)
     (while letters
-      (setq elem    (nth (random index) letters)
+      (setq elem    (seq-random-elt letters)
            letters (delq elem letters)
            index   (1- index))
       (aset mpuz-digit-to-letter index elem)
diff --git a/lisp/play/zone.el b/lisp/play/zone.el
index 27aa48f..a5d4ac9 100644
--- a/lisp/play/zone.el
+++ b/lisp/play/zone.el
@@ -596,7 +596,7 @@ If the element is a function or a list of a function and a 
number,
          (forward-line -1)
          (delete-region (point) (line-beginning-position 2))
          (goto-char (point-min))
-         (insert (nth (random (length lines)) lines)))
+         (insert (seq-random-elt lines)))
        (message (concat (make-string (random (- (frame-width) 5)) ? ) "grrr"))
        (sit-for 0.1)))))
 
diff --git a/lisp/vc/add-log.el b/lisp/vc/add-log.el
index 2e20284..1290d7e 100644
--- a/lisp/vc/add-log.el
+++ b/lisp/vc/add-log.el
@@ -930,8 +930,7 @@ non-nil, otherwise in local time."
                            (not (looking-at "[ \t]+.*<.*>$")))
                          (setq hit t)))))
             (forward-line 1)
-          (insert (nth (random (length new-entries))
-                       new-entries)
+          (insert (and new-entries (seq-random-elt new-entries))
                   (if use-hard-newlines hard-newline "\n")
                   (if use-hard-newlines hard-newline "\n"))
           (forward-line -1))))
diff --git a/test/lisp/net/tramp-tests.el b/test/lisp/net/tramp-tests.el
index 9f0264a..f105c6f 100644
--- a/test/lisp/net/tramp-tests.el
+++ b/test/lisp/net/tramp-tests.el
@@ -6787,8 +6787,7 @@ process sentinels.  They shall not disturb each other."
                     (let ((time (float-time))
                           (default-directory tmp-name)
                           (file
-                           (buffer-name
-                            (nth (random (length buffers)) buffers)))
+                           (buffer-name (seq-random-elt buffers)))
                          ;; A remote operation in a timer could
                          ;; confuse Tramp heavily.  So we ignore this
                          ;; error here.
@@ -6853,7 +6852,7 @@ process sentinels.  They shall not disturb each other."
             ;; the buffers.  Mix with regular operation.
             (let ((buffers (copy-sequence buffers)))
               (while buffers
-                (let* ((buf (nth (random (length buffers)) buffers))
+                (let* ((buf (seq-random-elt buffers))
                        (proc (get-buffer-process buf))
                        (file (process-get proc 'foo))
                        (count (process-get proc 'bar)))



reply via email to

[Prev in Thread] Current Thread [Next in Thread]