From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Damien Cassou Newsgroups: gmane.emacs.devel Subject: Re: Add seq-random-elt Date: Tue, 25 Oct 2016 08:52:28 +0200 Message-ID: <87h980apjn.fsf@cassou.me> References: <878ttime2i.fsf@cassou.me> NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" X-Trace: blaine.gmane.org 1477378393 23919 195.159.176.226 (25 Oct 2016 06:53:13 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Tue, 25 Oct 2016 06:53:13 +0000 (UTC) User-Agent: Notmuch/0.22 (http://notmuchmail.org) Emacs/25.1.1 (x86_64-unknown-linux-gnu) Cc: Nicolas Petton To: emacs-devel@gnu.org Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Tue Oct 25 08:53:09 2016 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by blaine.gmane.org with esmtp (Exim 4.84_2) (envelope-from ) id 1byvbV-0004Eo-WC for ged-emacs-devel@m.gmane.org; Tue, 25 Oct 2016 08:52:58 +0200 Original-Received: from localhost ([::1]:52254 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1byvbY-0006UL-DC for ged-emacs-devel@m.gmane.org; Tue, 25 Oct 2016 02:53:00 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:39709) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1byvbR-0006UE-Lz for emacs-devel@gnu.org; Tue, 25 Oct 2016 02:52:55 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1byvbN-0001hl-3M for emacs-devel@gnu.org; Tue, 25 Oct 2016 02:52:53 -0400 Original-Received: from mail.choca.pics ([62.210.108.126]:48350) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1byvbM-0001hG-RD for emacs-devel@gnu.org; Tue, 25 Oct 2016 02:52:49 -0400 Original-Received: from localhost (localhost.localdomain [IPv6:::1]) by mail.choca.pics (Postfix) with ESMTP id 48C9C232BD2E; Tue, 25 Oct 2016 08:52:39 +0200 (CEST) Original-Received: from mail.choca.pics ([IPv6:::1]) by localhost (mail.choca.pics [IPv6:::1]) (amavisd-new, port 10032) with ESMTP id grM1e04aDP9y; Tue, 25 Oct 2016 08:52:32 +0200 (CEST) Original-Received: from localhost (localhost.localdomain [IPv6:::1]) by mail.choca.pics (Postfix) with ESMTP id 63322232BDE8; Tue, 25 Oct 2016 08:52:32 +0200 (CEST) X-Virus-Scanned: amavisd-new at choca.pics Original-Received: from mail.choca.pics ([IPv6:::1]) by localhost (mail.choca.pics [IPv6:::1]) (amavisd-new, port 10026) with ESMTP id RrsqaI4_haVH; Tue, 25 Oct 2016 08:52:32 +0200 (CEST) Original-Received: from luz4 (31-211-216-84.customers.ownit.se [31.211.216.84]) by mail.choca.pics (Postfix) with ESMTPSA id B771B232BD2E; Tue, 25 Oct 2016 08:52:31 +0200 (CEST) In-Reply-To: <878ttime2i.fsf@cassou.me> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 62.210.108.126 X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Original-Sender: "Emacs-devel" Xref: news.gmane.org gmane.emacs.devel:208754 Archived-At: --=-=-= Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Hi, here is v3 which: * signals an error on empty parameter as Davis Herring requested. Compared to v1, this patch also: * adds documentation to sequences.texi as Nicolas requested, and * checks for null parameter as Cl=C3=A9ment requested Best, --=20 Damien Cassou http://damiencassou.seasidehosting.st "Success is the ability to go from one failure to another without losing enthusiasm." --Winston Churchill --=-=-= Content-Type: text/x-patch Content-Disposition: attachment; filename=0001-Add-seq-random-elt-to-seq.el.patch >From 54e3074cfee6a4aafaf009ac58548005419db3c5 Mon Sep 17 00:00:00 2001 From: Damien Cassou 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 | 9 ++++++++- test/lisp/emacs-lisp/seq-tests.el | 17 +++++++++++++++++ 3 files changed, 45 insertions(+), 1 deletion(-) diff --git a/doc/lispref/sequences.texi b/doc/lispref/sequences.texi index 08e5e3a..b6874bf 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 empty, the function signals an error. +@end defun @node Arrays @section Arrays diff --git a/lisp/emacs-lisp/seq.el b/lisp/emacs-lisp/seq.el index 9859f28..133dbd4 100644 --- a/lisp/emacs-lisp/seq.el +++ b/lisp/emacs-lisp/seq.el @@ -4,7 +4,7 @@ ;; Author: Nicolas Petton ;; Keywords: sequences -;; Version: 2.18 +;; Version: 2.19 ;; Package: seq ;; Maintainer: emacs-devel@gnu.org @@ -476,6 +476,13 @@ 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." + (if (seq-empty-p sequence) + (error "Sequence cannot be empty") + (seq-elt sequence (random (seq-length sequence))))) ;;; Optimized implementations for lists diff --git a/test/lisp/emacs-lisp/seq-tests.el b/test/lisp/emacs-lisp/seq-tests.el index c2065c6..6d17b7c 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,21 @@ 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-signal-on-empty () + (should-error (seq-random-elt nil)) + (should-error (seq-random-elt [])) + (should-error (seq-random-elt ""))) + (provide 'seq-tests) ;;; seq-tests.el ends here -- 2.10.0 --=-=-=--