all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Add seq-random-elt
@ 2016-10-21  6:05 Damien Cassou
  2016-10-21  8:08 ` Nicolas Petton
                   ` (5 more replies)
  0 siblings, 6 replies; 19+ messages in thread
From: Damien Cassou @ 2016-10-21  6:05 UTC (permalink / raw)
  To: emacs-devel; +Cc: Nicolas Petton

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

Hi,

attached patch adds seq-random-elt to seq.el and its unit test to
seq-tests.el.

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: 2232 bytes --]

From 87396cb588e7faa6ee64f692290d53aeb9553f1b 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
  the new function
---
 lisp/emacs-lisp/seq.el            |  6 +++++-
 test/lisp/emacs-lisp/seq-tests.el | 12 ++++++++++++
 2 files changed, 17 insertions(+), 1 deletion(-)

diff --git a/lisp/emacs-lisp/seq.el b/lisp/emacs-lisp/seq.el
index 9859f28..6ef5b08 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,10 @@ 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."
+  (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..7c1b5e7 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,16 @@ Evaluate BODY for each created sequence.
     (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)))))
+
 (provide 'seq-tests)
 ;;; seq-tests.el ends here
-- 
2.10.0


^ permalink raw reply related	[flat|nested] 19+ messages in thread

end of thread, other threads:[~2016-10-25 19:32 UTC | newest]

Thread overview: 19+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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
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

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.