How to filter elements from a list?
Suppose you have a list, and you want to remove some element that a predicate function returns false. How?
emacs lisp doesn't have a filter/grep function for list.
you can use the
cl-remove-if-not in CL library, like this:
(require 'cl-lib) (cl-remove-if-not 'numberp '(1 "a" 2)) ;; (1 2)
(emacs 24.x changed the name
or, you can write your own:
(defun xah-filter-list (φpredicate φlist) "Return a new list such that φpredicate is true on all members of φlist. This function is similar to perl's “grep” or other lang's “filter” function." (let ((ξresult (mapcar (lambda (ξx) (if (funcall φpredicate ξx) ξx nil )) φlist))) (setq ξresult (delete nil ξresult)) ξresult )) ;; test (setq xx '(1 "a" 2)) (xah-filter-list 'numberp xx) ; (1 2) xx ; '(1 "a" 2)
for many functions that check if a element exists or filter a list/sequence, see: (info "(elisp) Sets And Lists")