Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filter function in Elisp

Is there equivalent of higher-order function filter in Emacs Lisp? Like function from python or Javascript.

(filter-equivalent (lambda (n) (= (% n 2) 0)) '(1 2 3 4 5 6 7 8))

==> (2 4 6 8)
like image 330
jcubic Avatar asked Sep 25 '13 09:09

jcubic


2 Answers

It's cl-remove-if-not. A bit of a mouthful, but it works.

To elaborate a bit, you need

(require 'cl-lib)

to get this function. There's an alias for it, called remove-if-not, but I prefer not to use it, since it may look like I'm using remove-if-not from cl.

It's a good practice to include the prefix, not doing using namespace std in C++, but saying std::cout each time.

like image 159
abo-abo Avatar answered Nov 11 '22 00:11

abo-abo


The third-party dash.el library provides a -filter function as an alternative to cl-remove-if-not.

(-filter 'evenp '(1 2 3 4 5 6 7 8))

;; => (2 4 6 8)
like image 27
Bozhidar Batsov Avatar answered Nov 10 '22 23:11

Bozhidar Batsov