unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* bug#44205: [PATCH] Add new function seq-remove-item
@ 2020-10-25  0:52 Stefan Kangas
  2020-10-25 12:49 ` Lars Ingebrigtsen
                   ` (3 more replies)
  0 siblings, 4 replies; 8+ messages in thread
From: Stefan Kangas @ 2020-10-25  0:52 UTC (permalink / raw)
  To: 44205

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

I found myself reaching for a version of `seq-remove' where I don't have
to supply a lambda but can just give an item.  Ergo, the attached.

    (seq-remove-item 2 '(1 2 3))  => (1 3)

I find it a whole lot nicer than:

    (seq-remove (lambda (a) (= a 2)) '(1 2 3))  => (1 3)

Turns out it could already be used to simplify some code in tab-line.el,
even if seq is arguably not yet very widely used in our sources.

I did not yet add it to NEWS or the manual; I will do that if people
agree that this is a good addition.

Comments?

[-- Attachment #2: 0001-Add-new-function-seq-remove-item.patch --]
[-- Type: text/x-diff, Size: 3729 bytes --]

From 628753c2fe03f86fec39bba97c11000bc1cce130 Mon Sep 17 00:00:00 2001
From: Stefan Kangas <stefan@marxist.se>
Date: Sun, 25 Oct 2020 02:30:26 +0200
Subject: [PATCH] Add new function seq-remove-item

* lisp/emacs-lisp/seq.el (seq-remove-item): New function.
* lisp/emacs-lisp/shortdoc.el (sequence): Document it.

* lisp/tab-line.el (tab-line-tabs-window-buffers)
(tab-line-select-tab-buffer): Use seq-remove-item instead of
seq-remove to simplify the code.
---
 lisp/emacs-lisp/seq.el      |  7 +++++++
 lisp/emacs-lisp/shortdoc.el |  2 ++
 lisp/tab-line.el            | 16 ++++++++--------
 3 files changed, 17 insertions(+), 8 deletions(-)

diff --git a/lisp/emacs-lisp/seq.el b/lisp/emacs-lisp/seq.el
index 4656277ea1..82daae6f48 100644
--- a/lisp/emacs-lisp/seq.el
+++ b/lisp/emacs-lisp/seq.el
@@ -331,6 +331,13 @@ seq-remove
   (seq-filter (lambda (elt) (not (funcall pred elt)))
               sequence))
 
+;;;###autoload
+(cl-defgeneric seq-remove-item (item sequence)
+  "Return a list of all the elements in SEQUENCE that are not ITEM.
+The comparison is done using `equal'. "
+  (seq-filter (lambda (elt) (not (equal item elt)))
+           sequence))
+
 ;;;###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 acc7d13195..20cfd6e295 100644
--- a/lisp/emacs-lisp/shortdoc.el
+++ b/lisp/emacs-lisp/shortdoc.el
@@ -730,6 +730,8 @@ sequence
    :eval (seq-filter #'numberp '(a b 3 4 f 6)))
   (seq-remove
    :eval (seq-remove #'numberp '(1 2 c d 5)))
+  (seq-remove-item
+   :eval (seq-remove-item 3 '(1 2 3 4 5)))
   (seq-group-by
    :eval (seq-group-by #'cl-plusp '(-1 2 3 -4 -5 6)))
   (seq-difference
diff --git a/lisp/tab-line.el b/lisp/tab-line.el
index 46bf89f14e..26f5f750b4 100644
--- a/lisp/tab-line.el
+++ b/lisp/tab-line.el
@@ -385,11 +385,11 @@ tab-line-tabs-window-buffers
 variable `tab-line-tabs-function'."
   (let* ((window (selected-window))
          (buffer (window-buffer window))
-         (next-buffers (seq-remove (lambda (b) (eq b buffer))
-                                   (window-next-buffers window)))
+         (next-buffers (seq-remove-item buffer
+                                        (window-next-buffers window)))
          (next-buffers (seq-filter #'buffer-live-p next-buffers))
-         (prev-buffers (seq-remove (lambda (b) (eq b buffer))
-                                   (mapcar #'car (window-prev-buffers window))))
+         (prev-buffers (seq-remove-item buffer
+                                        (mapcar #'car (window-prev-buffers window))))
          (prev-buffers (seq-filter #'buffer-live-p prev-buffers))
          ;; Remove next-buffers from prev-buffers
          (prev-buffers (seq-difference prev-buffers next-buffers)))
@@ -622,10 +622,10 @@ tab-line-select-tab
 
 (defun tab-line-select-tab-buffer (buffer &optional window)
   (let* ((window-buffer (window-buffer window))
-         (next-buffers (seq-remove (lambda (b) (eq b window-buffer))
-                                   (window-next-buffers window)))
-         (prev-buffers (seq-remove (lambda (b) (eq b window-buffer))
-                                   (mapcar #'car (window-prev-buffers window))))
+         (next-buffers (seq-remove-item window-buffer
+                                        (window-next-buffers window)))
+         (prev-buffers (seq-remove-item window-buffer
+                                        (mapcar #'car (window-prev-buffers window))))
          ;; Remove next-buffers from prev-buffers
          (prev-buffers (seq-difference prev-buffers next-buffers)))
     (cond
-- 
2.28.0


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

end of thread, other threads:[~2020-11-18 17:17 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2020-10-25  0:52 bug#44205: [PATCH] Add new function seq-remove-item Stefan Kangas
2020-10-25 12:49 ` Lars Ingebrigtsen
2020-10-27 13:36   ` Basil L. Contovounesios
2020-11-13  0:07     ` Stefan Kangas
2020-11-18 17:17       ` Stefan Kangas
2020-10-25 16:25 ` Unknown
2020-10-25 19:12 ` Juri Linkov
2020-10-27 13:45 ` Basil L. Contovounesios

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).