all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Damien Cassou <damien@cassou.me>
To: emacs-devel@gnu.org
Cc: Nicolas Petton <nicolas@petton.fr>
Subject: Re: Add seq-random-elt
Date: Sun, 23 Oct 2016 19:06:52 +0200	[thread overview]
Message-ID: <8737jn570z.fsf@cassou.me> (raw)
In-Reply-To: <878ttime2i.fsf@cassou.me>

[-- Attachment #1: Type: text/plain, Size: 341 bytes --]

Hi,

here is a new patch which both:

* adds documentation to sequences.texi as Nicolas requested, and

* checks for null parameter as Clément requested

Best,

-- 
Damien Cassou
http://damiencassou.seasidehosting.st

"Success is the ability to go from one failure to another without
losing enthusiasm." --Winston Churchill

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Add-seq-random-elt-to-seq.el.patch --]
[-- Type: text/x-patch, Size: 3215 bytes --]

From 77984f5705f478275b865e7c3105ceca9380007e Mon Sep 17 00:00:00 2001
From: Damien Cassou <damien@cassou.me>
Date: Fri, 21 Oct 2016 07:53:08 +0200
Subject: [PATCH] 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
---
 doc/lispref/sequences.texi        | 20 ++++++++++++++++++++
 lisp/emacs-lisp/seq.el            |  8 +++++++-
 test/lisp/emacs-lisp/seq-tests.el | 15 +++++++++++++++
 3 files changed, 42 insertions(+), 1 deletion(-)

diff --git a/doc/lispref/sequences.texi b/doc/lispref/sequences.texi
index 08e5e3a..f06a615 100644
--- a/doc/lispref/sequences.texi
+++ b/doc/lispref/sequences.texi
@@ -1037,6 +1037,26 @@ Sequence Functions
 @end example
 @end defmac
 
+@defun seq-random-elt sequence
+  This function returns an element of @var{sequence} taken at random. If @var{sequence} is nil, the function returns nil.
+
+@example
+@group
+(seq-random-elt [1 2 3 4])
+@result{} 3
+(seq-random-elt [1 2 3 4])
+@result{} 2
+(seq-random-elt [1 2 3 4])
+@result{} 4
+(seq-random-elt [1 2 3 4])
+@result{} 2
+(seq-random-elt [1 2 3 4])
+@result{} 1
+@end group
+@end example
+
+  If @var{sequence} is nil, the function returns nil.
+@end defun
 
 @node Arrays
 @section Arrays
diff --git a/lisp/emacs-lisp/seq.el b/lisp/emacs-lisp/seq.el
index 9859f28..9ece928 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,12 @@ seq--elt-safe
   "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."
+  (when sequence
+    (seq-elt sequence (random (seq-length sequence)))))
 \f
 
 ;;; Optimized implementations for lists
diff --git a/test/lisp/emacs-lisp/seq-tests.el b/test/lisp/emacs-lisp/seq-tests.el
index c2065c6..a6f26be 100644
--- a/test/lisp/emacs-lisp/seq-tests.el
+++ b/test/lisp/emacs-lisp/seq-tests.el
@@ -28,6 +28,7 @@
 
 (require 'ert)
 (require 'seq)
+(require 'map)
 
 (defmacro with-test-sequences (spec &rest body)
   "Successively bind VAR to a list, vector, and string built from SEQ.
@@ -371,5 +372,19 @@ test-sequences-oddp
     (should (equal (seq-sort-by #'seq-length #'> seq)
                    ["xxx" "xx" "x"]))))
 
+(ert-deftest test-seq-random-elt-take-all ()
+  (let ((seq '(a b c d e))
+        (count '()))
+    (should (= 0 (map-length count)))
+    (dotimes (_ 1000)
+      (let ((random-elt (seq-random-elt seq)))
+        (map-put count
+                 random-elt
+                 (map-elt count random-elt 0))))
+    (should (= 5 (map-length count)))))
+
+(ert-deftest test-seq-random-elt-return-nil ()
+  (should (null (seq-random-elt nil))))
+
 (provide 'seq-tests)
 ;;; seq-tests.el ends here
-- 
2.10.0


  parent reply	other threads:[~2016-10-23 17:06 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-10-21  6:05 Add seq-random-elt Damien Cassou
2016-10-21  8:08 ` Nicolas Petton
2016-10-21  9:06 ` Nicolas Petton
2016-10-21 18:57 ` Stefan Monnier
2016-10-21 20:52   ` Nicolas Petton
2016-10-21 21:30     ` Stefan Monnier
2016-10-22  8:13       ` Michael Heerdegen
2016-10-22 12:42       ` Ted Zlatanov
2016-10-22 14:53       ` Clément Pit--Claudel
2016-10-22 14:54 ` Clément Pit--Claudel
2016-10-23 17:06 ` Damien Cassou [this message]
2016-10-24 11:03   ` Nicolas Petton
2016-10-24 13:07     ` Stefan Monnier
2016-10-24 16:24   ` Davis Herring
2016-10-24 16:54     ` Clément Pit--Claudel
2016-10-25  6:52 ` Damien Cassou
2016-10-25 10:33   ` Nicolas Petton
2016-10-25 18:58     ` Eli Zaretskii
2016-10-25 19:32       ` Nicolas Petton

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

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=8737jn570z.fsf@cassou.me \
    --to=damien@cassou.me \
    --cc=emacs-devel@gnu.org \
    --cc=nicolas@petton.fr \
    /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 external index

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

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.