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: Add seq-random-elt Date: Fri, 21 Oct 2016 08:05:57 +0200 Message-ID: <878ttime2i.fsf@cassou.me> NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" X-Trace: blaine.gmane.org 1477030045 30286 195.159.176.226 (21 Oct 2016 06:07:25 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Fri, 21 Oct 2016 06:07:25 +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 Fri Oct 21 08:07:20 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 1bxSyp-0004Tn-Qv for ged-emacs-devel@m.gmane.org; Fri, 21 Oct 2016 08:06:59 +0200 Original-Received: from localhost ([::1]:58801 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bxSys-0002r0-30 for ged-emacs-devel@m.gmane.org; Fri, 21 Oct 2016 02:07:02 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:60583) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bxSyI-0002qu-Vh for emacs-devel@gnu.org; Fri, 21 Oct 2016 02:06:28 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bxSyF-000741-SM for emacs-devel@gnu.org; Fri, 21 Oct 2016 02:06:26 -0400 Original-Received: from mail.choca.pics ([62.210.108.126]:42028) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1bxSyF-00072E-ML for emacs-devel@gnu.org; Fri, 21 Oct 2016 02:06:23 -0400 Original-Received: from localhost (localhost.localdomain [IPv6:::1]) by mail.choca.pics (Postfix) with ESMTP id 8D75423284E2; Fri, 21 Oct 2016 08:06:15 +0200 (CEST) Original-Received: from mail.choca.pics ([IPv6:::1]) by localhost (mail.choca.pics [IPv6:::1]) (amavisd-new, port 10032) with ESMTP id n6v_KT4Yxx8e; Fri, 21 Oct 2016 08:06:12 +0200 (CEST) Original-Received: from localhost (localhost.localdomain [IPv6:::1]) by mail.choca.pics (Postfix) with ESMTP id D7787232857E; Fri, 21 Oct 2016 08:06:11 +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 OcLnDYS6SrCE; Fri, 21 Oct 2016 08:06:11 +0200 (CEST) Original-Received: from luz4 (c80-216-236-104.bredband.comhem.se [80.216.236.104]) by mail.choca.pics (Postfix) with ESMTPSA id 7715E232857B; Fri, 21 Oct 2016 08:06:11 +0200 (CEST) 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:208569 Archived-At: --=-=-= Content-Type: text/plain 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 --=-=-= Content-Type: text/x-patch Content-Disposition: attachment; filename=0001-Add-seq-random-elt-to-seq.el.patch >From 87396cb588e7faa6ee64f692290d53aeb9553f1b 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 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 ;; 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)))) ;;; 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 --=-=-=--