From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Damien Cassou Newsgroups: gmane.emacs.bugs Subject: bug#57564: Add new function `seq-remove-at-position' Date: Sat, 03 Sep 2022 19:09:01 +0200 Message-ID: <87bkrwfkr6.fsf@cassou.me> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="36227"; mail-complaints-to="usenet@ciao.gmane.io" To: 57564@debbugs.gnu.org Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Sat Sep 03 19:10:10 2022 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oUWeo-0009EW-3d for geb-bug-gnu-emacs@m.gmane-mx.org; Sat, 03 Sep 2022 19:10:10 +0200 Original-Received: from localhost ([::1]:44770 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oUWem-000663-R1 for geb-bug-gnu-emacs@m.gmane-mx.org; Sat, 03 Sep 2022 13:10:08 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:51976) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oUWeg-00065b-Mb for bug-gnu-emacs@gnu.org; Sat, 03 Sep 2022 13:10:02 -0400 Original-Received: from debbugs.gnu.org ([209.51.188.43]:54213) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1oUWeg-00009s-9v for bug-gnu-emacs@gnu.org; Sat, 03 Sep 2022 13:10:02 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1oUWef-00052A-VN for bug-gnu-emacs@gnu.org; Sat, 03 Sep 2022 13:10:01 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Damien Cassou Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Sat, 03 Sep 2022 17:10:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: report 57564 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch X-Debbugs-Original-To: bug-gnu-emacs@gnu.org Original-Received: via spool by submit@debbugs.gnu.org id=B.166222495319283 (code B ref -1); Sat, 03 Sep 2022 17:10:01 +0000 Original-Received: (at submit) by debbugs.gnu.org; 3 Sep 2022 17:09:13 +0000 Original-Received: from localhost ([127.0.0.1]:42912 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1oUWdt-00050x-AM for submit@debbugs.gnu.org; Sat, 03 Sep 2022 13:09:13 -0400 Original-Received: from lists.gnu.org ([209.51.188.17]:47858) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1oUWdr-00050n-ET for submit@debbugs.gnu.org; Sat, 03 Sep 2022 13:09:11 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:58200) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oUWdr-0005pM-A9 for bug-gnu-emacs@gnu.org; Sat, 03 Sep 2022 13:09:11 -0400 Original-Received: from mail.choca.pics ([2001:910:1410:500::1]:38084) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oUWdp-0008RC-1k for bug-gnu-emacs@gnu.org; Sat, 03 Sep 2022 13:09:10 -0400 Original-Received: from localhost (localhost.localdomain [IPv6:::1]) by mail.choca.pics (Postfix) with ESMTP id F0FF6181942A0 for ; Sat, 3 Sep 2022 19:09:03 +0200 (CEST) Original-Received: from mail.choca.pics ([IPv6:::1]) by localhost (mail.choca.pics [IPv6:::1]) (amavisd-new, port 10032) with ESMTP id ZNYkrVCdDdbr for ; Sat, 3 Sep 2022 19:09:03 +0200 (CEST) Original-Received: from localhost (localhost.localdomain [IPv6:::1]) by mail.choca.pics (Postfix) with ESMTP id 2CA9D181942A3 for ; Sat, 3 Sep 2022 19:09:03 +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 FcSJ57oL5QDA for ; Sat, 3 Sep 2022 19:09:02 +0200 (CEST) Original-Received: from localhost (153.226.95.79.rev.sfr.net [79.95.226.153]) by mail.choca.pics (Postfix) with ESMTPSA id CC030181942A0 for ; Sat, 3 Sep 2022 19:09:02 +0200 (CEST) Received-SPF: pass client-ip=2001:910:1410:500::1; envelope-from=damien@cassou.me; helo=mail.choca.pics X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: "bug-gnu-emacs" Xref: news.gmane.io gmane.emacs.bugs:241432 Archived-At: --=-=-= Content-Type: text/plain Tags: patch Hi, here is a patch adding seq-remove-at-position to seq.el. -- Damien Cassou "Success is the ability to go from one failure to another without losing enthusiasm." --Winston Churchill --=-=-= Content-Type: text/patch Content-Disposition: attachment; filename=0001-Add-new-function-seq-remove-at-position.patch >From 244bb4ac72afb682c52e48a1308b569ce782174d Mon Sep 17 00:00:00 2001 From: Damien Cassou Date: Sat, 3 Sep 2022 18:47:04 +0200 Subject: [PATCH] Add new function `seq-remove-at-position' * doc/lispref/sequences.texi (Sequence Functions): Document it. * lisp/emacs-lisp/seq.el (seq-remove-at-position): New function. * lisp/emacs-lisp/shortdoc.el (sequence): Mention it. * test/lisp/emacs-lisp/seq-tests.el (test-seq-remove-at-position): Test it. --- doc/lispref/sequences.texi | 18 ++++++++++++++++++ etc/NEWS | 5 +++++ lisp/emacs-lisp/seq.el | 14 ++++++++++++++ lisp/emacs-lisp/shortdoc.el | 3 +++ test/lisp/emacs-lisp/seq-tests.el | 8 ++++++++ 5 files changed, 48 insertions(+) diff --git a/doc/lispref/sequences.texi b/doc/lispref/sequences.texi index 1f6f80521c..e90502df3c 100644 --- a/doc/lispref/sequences.texi +++ b/doc/lispref/sequences.texi @@ -680,6 +680,24 @@ Sequence Functions @end example @end defun +@defun seq-remove-at-position sequence n +@cindex removing from sequences + This function returns a copy of @var{sequence} where the element at + (zero-based) index @var{n} got removed. The result is a sequence of + the same type as @var{sequence}. + +@example +@group +(seq-remove-at-position [1 -1 3 -3 5] 0) +@result{} [-1 3 -3 5] +@end group +@group +(seq-remove-at-position [1 -1 3 -3 5] 3) +@result{} [1 -1 3 5] +@end group +@end example +@end defun + @defun seq-reduce function sequence initial-value @cindex reducing sequences This function returns the result of calling @var{function} with diff --git a/etc/NEWS b/etc/NEWS index cc4714e71c..2fdc63551f 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -2734,6 +2734,11 @@ The default timeout value can be defined by the new variable ** New function 'seq-split'. This returns a list of sub-sequences of the specified sequence. ++++ +** New function 'seq-remove-at-position'. +This function returns a copy of the specified sequence where the +element at a given (zero-based) index got removed. + +++ ** 'plist-get', 'plist-put' and 'plist-member' are no longer limited to 'eq'. These function now take an optional comparison predicate argument. diff --git a/lisp/emacs-lisp/seq.el b/lisp/emacs-lisp/seq.el index b6f0f66e5b..4d05f2c589 100644 --- a/lisp/emacs-lisp/seq.el +++ b/lisp/emacs-lisp/seq.el @@ -346,6 +346,20 @@ seq-remove (seq-filter (lambda (elt) (not (funcall pred elt))) sequence)) +;;;###autoload +(cl-defgeneric seq-remove-at-position (sequence n) + "Return a copy of SEQUENCE where the element at N got removed. + +N is the (zero-based) index of the element that should not be in +the result. + +The result is a sequence of the same type as SEQUENCE." + (seq-concatenate + (let ((type (type-of sequence))) + (if (eq type 'cons) 'list type)) + (seq-subseq sequence 0 n) + (seq-subseq sequence (1+ n)))) + ;;;###autoload (cl-defgeneric seq-reduce (function sequence initial-value) "Reduce the function FUNCTION across SEQUENCE, starting with INITIAL-VALUE. diff --git a/lisp/emacs-lisp/shortdoc.el b/lisp/emacs-lisp/shortdoc.el index 990dabe351..6a366ec0fc 100644 --- a/lisp/emacs-lisp/shortdoc.el +++ b/lisp/emacs-lisp/shortdoc.el @@ -888,6 +888,9 @@ sequence :eval (seq-filter #'numberp '(a b 3 4 f 6))) (seq-remove :eval (seq-remove #'numberp '(1 2 c d 5))) + (seq-remove-at-position + :eval (seq-remove-at-position '(a b c d e) 3) + :eval (seq-remove-at-position [a b c d e] 0)) (seq-group-by :eval (seq-group-by #'cl-plusp '(-1 2 3 -4 -5 6))) (seq-union diff --git a/test/lisp/emacs-lisp/seq-tests.el b/test/lisp/emacs-lisp/seq-tests.el index 1a27467d29..6249e48617 100644 --- a/test/lisp/emacs-lisp/seq-tests.el +++ b/test/lisp/emacs-lisp/seq-tests.el @@ -137,6 +137,14 @@ test-seq-remove (with-test-sequences (seq '()) (should (equal (seq-remove #'test-sequences-evenp seq) '())))) +(ert-deftest test-seq-remove-at-position () + (with-test-sequences (seq '(1 2 3 4)) + (should (same-contents-p (seq-remove-at-position seq 2) '(1 2 4))) + (should (same-contents-p (seq-remove-at-position seq 0) '(2 3 4))) + (should (same-contents-p (seq-remove-at-position seq 3) '(1 2 3))) + (should (eq (type-of (seq-remove-at-position seq 2)) + (type-of seq))))) + (ert-deftest test-seq-count () (with-test-sequences (seq '(6 7 8 9 10)) (should (equal (seq-count #'test-sequences-evenp seq) 3)) -- 2.36.2 --=-=-=--