]> git.eshelyaron.com Git - emacs.git/commitdiff
Add new function `seq-remove-at-position'
authorDamien Cassou <damien@cassou.me>
Sat, 3 Sep 2022 16:47:04 +0000 (18:47 +0200)
committerLars Ingebrigtsen <larsi@gnus.org>
Sun, 4 Sep 2022 11:07:18 +0000 (13:07 +0200)
* 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
etc/NEWS
lisp/emacs-lisp/seq.el
lisp/emacs-lisp/shortdoc.el
test/lisp/emacs-lisp/seq-tests.el

index cc956952d6f0ec83dd7e7c1a875e06865e0dfe49..2ee19efb1a99e5fe454af481f2d89054550a448d 100644 (file)
@@ -680,6 +680,24 @@ for which @var{predicate} returns @code{nil}.
 @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
index edd4b01eab507aa5aa9b4499ec0a2857c292a880..e9c322d74ae4be63eb2e03df28330680e89f7c67 100644 (file)
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -2743,6 +2743,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.
index b5f762ef3aca5d33740e7ce66336ca798ed35943..64197b55e5f977b21f139f610b2ff6c537e37aaf 100644 (file)
@@ -346,6 +346,20 @@ list."
   (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.
index 990dabe351a3796fbdbd7bb63a86571b82e0e9db..6a366ec0fc09f72876b28df2d152796fc67e09ed 100644 (file)
@@ -888,6 +888,9 @@ A FUNC form can have any number of `:no-eval' (or `:no-value'),
    :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
index 1a27467d292f761710dfa9273e299c6825d56490..6249e486173c7341c0be6351eb77029026cc92fb 100644 (file)
@@ -137,6 +137,14 @@ Evaluate BODY for each created sequence.
   (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))