Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete only first appearance of element into list?

Tags:

elisp

How to delete only first appearance of element into list (elisp) ?

like image 964
parzival Avatar asked Dec 21 '22 20:12

parzival


1 Answers

The Common Lisp sequence-editing functions (remove and friends) take a :count keyword argument:

Count, if supplied, limits the number of elements removed or deleted; if more than count elements satisfy the test, then of these elements only the leftmost or rightmost, depending on from-end, are deleted or removed, as many as specified by count. If count is supplied and negative, the behavior is as if zero had been supplied instead. If count is nil, all matching items are affected.

For example:

ELISP> (require 'cl)
cl

ELISP> (remove* 1 '(1 2 1 3 1 4) :count 1)
(2 1 3 1 4)

ELISP> (remove* 1 '(1 2 1 3 1 4) :count 2)
(2 3 1 4)

ELISP> (remove* 1 '(1 2 1 3 1 4) :count 2 :from-end t)
(1 2 3 4)

(Note that Emacs already had its own function called remove, so the cl package has to use the name remove*.)

like image 178
Gareth Rees Avatar answered Mar 12 '23 06:03

Gareth Rees