From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED.blaine.gmane.org!not-for-mail From: Michael Heerdegen Newsgroups: gmane.emacs.devel Subject: Enhance seq-min and seq-max (was: stream.el) Date: Sat, 15 Jun 2019 02:25:06 +0200 Message-ID: <87a7ejelul.fsf_-_@web.de> References: <87o932d0qh.fsf@petton.fr> <87blz1ozp5.fsf@web.de> <87tvcsab8e.fsf@petton.fr> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: blaine.gmane.org; posting-host="blaine.gmane.org:195.159.176.226"; logging-data="96939"; mail-complaints-to="usenet@blaine.gmane.org" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.0.50 (gnu/linux) Cc: Emacs Devel To: Nicolas Petton Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Sat Jun 15 02:36:44 2019 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([209.51.188.17]) by blaine.gmane.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.89) (envelope-from ) id 1hbwgW-000P4t-Mj for ged-emacs-devel@m.gmane.org; Sat, 15 Jun 2019 02:36:44 +0200 Original-Received: from localhost ([::1]:57780 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hbwgV-00072k-3V for ged-emacs-devel@m.gmane.org; Fri, 14 Jun 2019 20:36:43 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:58944) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hbwfu-0006s5-9P for emacs-devel@gnu.org; Fri, 14 Jun 2019 20:36:07 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hbwVW-0006hn-4r for emacs-devel@gnu.org; Fri, 14 Jun 2019 20:25:23 -0400 Original-Received: from mout.web.de ([212.227.15.4]:60413) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1hbwVV-0006Yp-0B for emacs-devel@gnu.org; Fri, 14 Jun 2019 20:25:22 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=web.de; s=dbaedf251592; t=1560558307; bh=XP4WTMNWSga2o2i6KbdGukWkzqXc1DBojmF22/se1N0=; h=X-UI-Sender-Class:From:To:Cc:Subject:References:Date:In-Reply-To; b=i2j9Kuy2y9ZgROYZFGmsDqa2d8YtW2umqT7i3EsRZgFnkO1WmkkicwwKs3q5PuMcW nepFtjmigqZBc07A/56AZr4naXXHM/BsonVauTrktH3uC06RczTzLn5seE40REc+gR 6jY84/bSP2D0Kr1u5DleceILu9XUZEXFB1ChLvBA= X-UI-Sender-Class: c548c8c5-30a9-4db5-a2e7-cb6cb037b8f9 Original-Received: from drachen.dragon ([188.98.106.25]) by smtp.web.de (mrweb001 [213.165.67.108]) with ESMTPSA (Nemesis) id 0LdVdS-1iKae11nvO-00ilAT; Sat, 15 Jun 2019 02:25:07 +0200 In-Reply-To: <87tvcsab8e.fsf@petton.fr> (Nicolas Petton's message of "Fri, 14 Jun 2019 09:15:45 +0200") X-Provags-ID: V03:K1:lx58NGQHRYrOCcOL+ptIU6M485WaBLgKv9/cseF3JhE0xum6Svg ynlhWrhna+LXQqgVKc/ySR7bCAzLNQo5G44Txr2adTN8QA6ZJ1tuxYnGzpC3Hkpdi9L40AC WYujnD0FiGbrBaKOBiyHDcf5z8FoTMIyxQx+EoC1qVOhO751EjfAvf0KofW27ZcTsg54PXk l8IJA8a+d6QP5lYPJQp5w== X-UI-Out-Filterresults: notjunk:1;V03:K0:ZMEKZccAhqg=:0M9+Jl6EKJ9jqo9/wvBlVy IygDeEON+FA3RgOAVs2ScYT0Q7+u4OWlD7/FBki8oDiljRFeHYy9vqAUXuXoRzw8tKVwBEb3Q Dtv5Fy876FzEjErnkPUb8MoEnAL1HogzD6aqFhRPLRBTLzJWSt6ayYieCPX5H0EeYKUYSJKhC QdLoK2WAhOvuji84eNUzGLqUV5gse4kzI1ScsCjmkp7UpGtBmEiMpPuH+on1te6PtFjdoGqrm 7C5R3ny0aJdkY7WJMSo20v+A4X+KPA1kFmQQgyxHD1b0ariigR0jLfhq6PNshR48UggqSAfbw ebPn9f1iZeMEqo50ovbv2/fFPmbZidGBf2TKhthFkMxP6zrwMUTP/mVczXsZh5Lz3nndTsKvB ma2d/VYchVxMH4WcjP1SFZwHTK3rZG/IIjpMUNvHrZhSrnLHSIn+ZoHM5t1witbx0BdOyWrlj XiNsxofygV4nlNGgyjquWDRDS1/b0z1WmKgv0ElWuu8tJMlmD5IiSHlMvL6aHlef0meMqTTj5 UjtHNDC974rXr0gK+BI8H8+8Wwgq3vkJrjP7z+nqQ5QmSHwtafi/uZ4qMqXdULyenDI4cdk6i fyMA1NKgo98q+1I5aNAM7oSXw7emLuAcsR9b0Zgjr7zEkB71IX1GdAz1QSkkN/NocoM6Fk7fE BTzXcxJIugIs7XFTY6wybrhNyECRhLydqeGy+sbY4Iqoy6ntrWYA/6KEpAfMUrNRsEoLnQTIy X6qx8Cu+AAMQ3vqWmEsO3hLjEmEQFfu57J1Qq4KJTUZ1AUkehS0cx0w438Ftcu8vh5NZvSmc X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 212.227.15.4 X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.23 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:237648 Archived-At: --=-=-= Content-Type: text/plain Nicolas Petton writes: > It's ok to fix them, but I'd like to stay involved at least. Ok, great. I would like to extend seq-min and seq-max to allow (a) arbitrary comparison functions and (b) key functions like in cl, so that you can find e.g. the longest string in a sequence of strings. I miss this functionality, AFAIK cl-lib also can't do it. Like this, maybe? --=-=-= Content-Type: text/x-diff Content-Disposition: inline; filename=0001-WIP-Enhance-seq-min-and-seq-max.patch Content-Transfer-Encoding: quoted-printable =46rom 3cbaf1316de41687a6a3a6d67e4cc773769999c3 Mon Sep 17 00:00:00 2001 From: Michael Heerdegen Date: Fri, 10 May 2019 15:08:57 +0200 Subject: [PATCH] WIP: Enhance seq-min and seq-max =2D-- lisp/emacs-lisp/seq.el | 34 +++++++++++++++++++++++++++------- 1 file changed, 27 insertions(+), 7 deletions(-) diff --git a/lisp/emacs-lisp/seq.el b/lisp/emacs-lisp/seq.el index 3413cd1513..ddd8f3161c 100644 =2D-- a/lisp/emacs-lisp/seq.el +++ b/lisp/emacs-lisp/seq.el @@ -451,15 +451,35 @@ seq-group-by (seq-reverse sequence) nil)) -(cl-defgeneric seq-min (sequence) +(cl-defgeneric seq-min (sequence &optional predicate key) "Return the smallest element of SEQUENCE. -SEQUENCE must be a sequence of numbers or markers." - (apply #'min (seq-into sequence 'list))) - -(cl-defgeneric seq-max (sequence) +With PREDICATE given, use it to compare elements. The default is +`<'. +With function KEY given, call it on the individual elements and +compare the results instead of the elements." + (if predicate + (seq-reduce + (if key + (lambda (x y) + (if (funcall predicate (funcall key y) (funcall key x)) y x)= ) + (lambda (x y) (if (funcall predicate y x) y x))) + sequence (car sequence)) + (apply #'min (seq-into sequence 'list)))) + +(cl-defgeneric seq-max (sequence &optional predicate key) "Return the largest element of SEQUENCE. -SEQUENCE must be a sequence of numbers or markers." - (apply #'max (seq-into sequence 'list))) +With PREDICATE given, use it to compare elements. The default is +`<'. +With function KEY given, call it on the individual elements and +compare the results instead of the elements." + (if predicate + (seq-reduce + (if key + (lambda (x y) + (if (funcall predicate (funcall key x) (funcall key y)) y x)= ) + (lambda (x y) (if (funcall predicate x y) y x))) + sequence (car sequence)) + (apply #'max (seq-into sequence 'list)))) (defun seq--count-successive (pred sequence) "Return the number of successive elements for which (PRED element) is n= on-nil in SEQUENCE." =2D- 2.20.1 --=-=-= Content-Type: text/plain I made it so that if the result is ambiguous (e.g. multiple strings with equal length), the first one is returned, but that is not explicitly documented. Regards, Michael. --=-=-=--