1
0
mirror of https://git.savannah.gnu.org/git/emacs.git synced 2024-11-22 07:09:54 +00:00

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.
This commit is contained in:
Stefan Kangas 2021-09-24 19:41:03 +02:00
parent 8fbf816ccd
commit 35d0675467
11 changed files with 16 additions and 22 deletions

View File

@ -43,7 +43,7 @@
;; ;;
;; (if (eq window-system 'x) ;; (if (eq window-system 'x)
;; (mouse-avoidance-set-pointer-shape ;; (mouse-avoidance-set-pointer-shape
;; (nth (random 4) ;; (seq-random-elt
;; (list x-pointer-man x-pointer-spider ;; (list x-pointer-man x-pointer-spider
;; x-pointer-gobbler x-pointer-gumby)))) ;; 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 ;; Internal variables
(defvar mouse-avoidance-state nil) (defvar mouse-avoidance-state nil)
(defvar mouse-avoidance-pointer-shapes nil) (defvar mouse-avoidance-pointer-shapes nil)
(defvar mouse-avoidance-n-pointer-shapes 0)
(defvar mouse-avoidance-old-pointer-shape nil) (defvar mouse-avoidance-old-pointer-shape nil)
(defvar mouse-avoidance-animating-pointer nil) (defvar mouse-avoidance-animating-pointer nil)
@ -306,11 +305,8 @@ redefine this function to suit your own tastes."
(all-completions "x-pointer-" obarray (all-completions "x-pointer-" obarray
(lambda (x) (lambda (x)
(and (boundp x) (and (boundp x)
(integerp (symbol-value x))))))) (integerp (symbol-value x)))))))))
(setq mouse-avoidance-n-pointer-shapes (seq-random-elt mouse-avoidance-pointer-shapes))
(length mouse-avoidance-pointer-shapes))))
(nth (random mouse-avoidance-n-pointer-shapes)
mouse-avoidance-pointer-shapes))
(defun mouse-avoidance-ignore-p () (defun mouse-avoidance-ignore-p ()
(let ((mp (mouse-position))) (let ((mp (mouse-position)))

View File

@ -570,6 +570,7 @@ SEQUENCE must be a sequence of numbers or markers."
If no element is found, return nil." If no element is found, return nil."
(ignore-errors (seq-elt sequence n))) (ignore-errors (seq-elt sequence n)))
;;;###autoload
(cl-defgeneric seq-random-elt (sequence) (cl-defgeneric seq-random-elt (sequence)
"Return a random element from SEQUENCE. "Return a random element from SEQUENCE.
Signal an error if SEQUENCE is empty." Signal an error if SEQUENCE is empty."

View File

@ -149,8 +149,7 @@ If EXACT is non-nil, don't accept approximate matches."
(cond ((null epa-keyserver) (cond ((null epa-keyserver)
(user-error "Empty keyserver pool")) (user-error "Empty keyserver pool"))
((listp epa-keyserver) ((listp epa-keyserver)
(nth (random (length epa-keyserver)) (seq-random-elt epa-keyserver))
epa-keyserver))
((stringp epa-keyserver) ((stringp epa-keyserver)
epa-keyserver) epa-keyserver)
((error "Invalid type for `epa-keyserver'"))) ((error "Invalid type for `epa-keyserver'")))

View File

@ -824,7 +824,7 @@ As an example:
(ports (if (listp (nth 3 srv)) (ports (if (listp (nth 3 srv))
(erc-ports-list (nth 3 srv)) (erc-ports-list (nth 3 srv))
(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))) (erc :server host :port port)))
;;; The following experimental ;;; The following experimental

View File

@ -103,7 +103,7 @@ PNG format."
(remove nil (mapcar (remove nil (mapcar
(lambda (f) (unless (string-match (or omit "^$") f) f)) (lambda (f) (unless (string-match (or omit "^$") f) f))
(directory-files dir t ext)))) (directory-files dir t ext))))
(file (nth (random (length files)) files))) (file (and files (seq-random-elt files))))
(when file (when file
(funcall fun file))))) (funcall fun file)))))
@ -315,7 +315,7 @@ colors of the displayed X-Faces."
(let* ((possibilities '("%02x0000" "00%02x00" "0000%02x" (let* ((possibilities '("%02x0000" "00%02x00" "0000%02x"
"%02x%02x00" "00%02x%02x" "%02x00%02x")) "%02x%02x00" "00%02x%02x" "%02x00%02x"))
(format (concat "'#%02x%02x%02x' '#" (format (concat "'#%02x%02x%02x' '#"
(nth (random 6) possibilities) (seq-random-elt possibilities)
"'")) "'"))
(values nil)) (values nil))
(dotimes (i 255) (dotimes (i 255)

View File

@ -114,7 +114,7 @@ This is a specialization of `soap-sample-value' for
(cond (cond
((soap-xs-simple-type-enumeration type) ((soap-xs-simple-type-enumeration type)
(let ((enumeration (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) ((soap-xs-simple-type-pattern type)
(format "a string matching %s" (soap-xs-simple-type-pattern type))) (format "a string matching %s" (soap-xs-simple-type-pattern type)))
((soap-xs-simple-type-length-range type) ((soap-xs-simple-type-length-range type)
@ -134,7 +134,7 @@ This is a specialization of `soap-sample-value' for
(t (random 100))))) (t (random 100)))))
((consp (soap-xs-simple-type-base type)) ; an union of values ((consp (soap-xs-simple-type-base type)) ; an union of values
(let ((base (soap-xs-simple-type-base type))) (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-xs-basic-type-p (soap-xs-simple-type-base type))
(soap-sample-value (soap-xs-simple-type-base type)))))) (soap-sample-value (soap-xs-simple-type-base type))))))

View File

@ -1470,7 +1470,7 @@ push him out of it."
(mapc (mapc
(lambda (direction) (put direction 'y_t 0)) (lambda (direction) (put direction 'y_t 0))
landmark-directions) landmark-directions)
(dolist (direction (nth (random 8) landmark-8-directions)) (dolist (direction (seq-random-elt landmark-8-directions))
(put direction 'y_t 1.0)) (put direction 'y_t 1.0))
(landmark-move)) (landmark-move))

View File

@ -153,7 +153,7 @@ You may abort a game by typing \\<mpuz-mode-map>\\[mpuz-offer-abort]."
(index 10) (index 10)
elem) elem)
(while letters (while letters
(setq elem (nth (random index) letters) (setq elem (seq-random-elt letters)
letters (delq elem letters) letters (delq elem letters)
index (1- index)) index (1- index))
(aset mpuz-digit-to-letter index elem) (aset mpuz-digit-to-letter index elem)

View File

@ -596,7 +596,7 @@ If the element is a function or a list of a function and a number,
(forward-line -1) (forward-line -1)
(delete-region (point) (line-beginning-position 2)) (delete-region (point) (line-beginning-position 2))
(goto-char (point-min)) (goto-char (point-min))
(insert (nth (random (length lines)) lines))) (insert (seq-random-elt lines)))
(message (concat (make-string (random (- (frame-width) 5)) ? ) "grrr")) (message (concat (make-string (random (- (frame-width) 5)) ? ) "grrr"))
(sit-for 0.1))))) (sit-for 0.1)))))

View File

@ -930,8 +930,7 @@ non-nil, otherwise in local time."
(not (looking-at "[ \t]+.*<.*>$"))) (not (looking-at "[ \t]+.*<.*>$")))
(setq hit t))))) (setq hit t)))))
(forward-line 1) (forward-line 1)
(insert (nth (random (length new-entries)) (insert (and new-entries (seq-random-elt new-entries))
new-entries)
(if use-hard-newlines hard-newline "\n") (if use-hard-newlines hard-newline "\n")
(if use-hard-newlines hard-newline "\n")) (if use-hard-newlines hard-newline "\n"))
(forward-line -1)))) (forward-line -1))))

View File

@ -6787,8 +6787,7 @@ process sentinels. They shall not disturb each other."
(let ((time (float-time)) (let ((time (float-time))
(default-directory tmp-name) (default-directory tmp-name)
(file (file
(buffer-name (buffer-name (seq-random-elt buffers)))
(nth (random (length buffers)) buffers)))
;; A remote operation in a timer could ;; A remote operation in a timer could
;; confuse Tramp heavily. So we ignore this ;; confuse Tramp heavily. So we ignore this
;; error here. ;; error here.
@ -6853,7 +6852,7 @@ process sentinels. They shall not disturb each other."
;; the buffers. Mix with regular operation. ;; the buffers. Mix with regular operation.
(let ((buffers (copy-sequence buffers))) (let ((buffers (copy-sequence buffers)))
(while buffers (while buffers
(let* ((buf (nth (random (length buffers)) buffers)) (let* ((buf (seq-random-elt buffers))
(proc (get-buffer-process buf)) (proc (get-buffer-process buf))
(file (process-get proc 'foo)) (file (process-get proc 'foo))
(count (process-get proc 'bar))) (count (process-get proc 'bar)))