Add seq-random-elt to seq.el
* lisp/emacs-lisp/seq.el (seq-random-elt): Add function to return a random element from it's sequence parameter. * test/lisp/emacs-lisp/seq-tests.el (test-seq-random-elt-take-all test-seq-random-elt-return-nil): Test the new function * doc/lispref/sequences.texi: Document the new function
This commit is contained in:
parent
26ccd19269
commit
bd22beb6e4
3 changed files with 45 additions and 1 deletions
|
@ -4,7 +4,7 @@
|
|||
|
||||
;; Author: Nicolas Petton <nicolas@petton.fr>
|
||||
;; Keywords: sequences
|
||||
;; Version: 2.18
|
||||
;; Version: 2.19
|
||||
;; Package: seq
|
||||
|
||||
;; Maintainer: emacs-devel@gnu.org
|
||||
|
@ -476,6 +476,13 @@ SEQUENCE must be a sequence of numbers or markers."
|
|||
"Return element of SEQUENCE at the index N.
|
||||
If no element is found, return nil."
|
||||
(ignore-errors (seq-elt sequence n)))
|
||||
|
||||
(cl-defgeneric seq-random-elt (sequence)
|
||||
"Return a random element from SEQUENCE.
|
||||
Return nil if SEQUENCE is nil."
|
||||
(if (seq-empty-p sequence)
|
||||
(error "Sequence cannot be empty")
|
||||
(seq-elt sequence (random (seq-length sequence)))))
|
||||
|
||||
|
||||
;;; Optimized implementations for lists
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue