1

Is there a standard emacs-lisp function replacement for

(defun collect-if (PREDICATE LIST)
  (loop for el in LIST if (funcall PREDICATE el) collect el))

Example toy application: Extract the odd numbers from a list of numbers

(collect-if (lambda (x) (/= 0 (% x 2))) '(1 2 3 4 5))
(1 3 5)
2

Okay remove-if-not makes a copy of the list it operates on. So this is the equivalent to collect-if above.

From my perspective remove-if-not is a really tricky name for something like collect-if or sub-list (not with the meaning of cl-sublis but with the meaning of sub-list). There should be an alias saying more directly what that function does.

  • I believe the current function name is the one used in Common Lisp. – Dan Jun 1 '16 at 11:29
  • @Dan This is almost clear since remove-if-not is from the cl-seq package. I am not saying the alias should live in cl. I think I put a remainder somewhere that I actually should use remove-if-not when I want collect-if. – Tobias Jun 1 '16 at 11:45
  • 1
    BTW, the standard name for this is filter (or for the Smalltalkers/Rubyists among us, select). dash.el provides both. Rumor has it dash.el was invented in a fit of rage after finding out that elisp got blessed with the unwieldy cl-remove-if-not identifier... – wasamasa Jun 1 '16 at 19:01
2

Emacs 25 will include the seq.el library, which will provide default functions to manipulate sequences. Right now you can download it from GNU ELPA for Emacs 24. The functions you are looking for are:

(seq-filter (lambda (x) (/= 0 (% x 2))) '(1 2 3 4 5)) ; => (1 3 5)
(seq-remove (lambda (x) (/= 0 (% x 2))) '(1 2 3 4 5)) ; => (2 4)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.