diff options
author | Damien Cassou <damien@cassou.me> | 2016-10-21 07:53:08 +0200 |
---|---|---|
committer | Nicolas Petton <nicolas@petton.fr> | 2016-10-25 12:32:57 +0200 |
commit | bd22beb6e4054c659e8c31931c421f60e9719c65 (patch) | |
tree | c9afbd41266dc8c13a9807bf825d68cdbc148687 /lisp/emacs-lisp/seq.el | |
parent | 26ccd19269c040ad5960a7567aa5fc88f142c709 (diff) | |
download | emacs-bd22beb6e4054c659e8c31931c421f60e9719c65.tar.gz emacs-bd22beb6e4054c659e8c31931c421f60e9719c65.tar.bz2 emacs-bd22beb6e4054c659e8c31931c421f60e9719c65.zip |
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
Diffstat (limited to 'lisp/emacs-lisp/seq.el')
-rw-r--r-- | lisp/emacs-lisp/seq.el | 9 |
1 files changed, 8 insertions, 1 deletions
diff --git a/lisp/emacs-lisp/seq.el b/lisp/emacs-lisp/seq.el index 85702f4a64d..5c89d2b3007 100644 --- a/lisp/emacs-lisp/seq.el +++ b/lisp/emacs-lisp/seq.el @@ -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 |