Given '(a b c d)
, how would I remove b
and c
to end up with '(a d)
? Is there a "right" way to remove multiple, distinct elements from a list?
Obviously, one could do (delete 'c (delete 'b '(a b c d)))
. But this doesn't generalize or scale well1.
Other possibilities include:
Create a predicate function and map it over the list with something like
delete-if-not
.Perform a set difference (if that's even different from the list mapping)
I am getting lost in all the different implementations which accomplish, so it seems, the same thing (i.e. delete
, dash
, seq
, cl-delete
, etc.).
I would prefer to only use whatever ships with vanilla Emacs (26.3), unless there is a compelling reason to introduce an external dependency.
1 I understand the distinction between remove
and delete
in that one preserves the original list while the other does not. It's my impression that the two are more or less interchangeable otherwise.
delete
doesn't accept a list and by scale I meant that to delete more items, it would require a process of(delete (delete (delete ...)
. I wasn't concerning myself with performance yet. However, your reference is noted and much appreciated!'(a b c d)
, directly to a destructive function such asdelete
, as the constant will then be modified globally and the same code may behave differently each time it's run. Instead, either make a copy of the constant list first, e.g.(copy-sequence '(a b c d))
, create a new list each time, e.g.(list 'a 'b 'c 'd)
, or pass the constant to non-destructive functions such asremove
.