Out with remove-if, in with !reject

master
Magnar Sveen 14 years ago
parent f9471e411b
commit 4201ecd29b
  1. 13
      bang.el
  2. 19
      tests.el

@ -27,12 +27,17 @@
(eval-when-compile (require 'cl))
(defun !--call-with-it (form-or-fn)
(if (functionp form-or-fn)
(list form-or-fn 'it)
form-or-fn))
(defmacro !filter (form-or-fn list)
`(let ((!--list ,list)
(!--result '()))
(while !--list
(let ((it (car !--list)))
(when ,(if (functionp form-or-fn) (list form-or-fn 'it) (list 'progn form-or-fn))
(when ,(!--call-with-it form-or-fn)
(setq !--result (cons it !--result))))
(setq !--list (cdr !--list)))
(nreverse !--result)))
@ -48,7 +53,7 @@
(while !--list
(let ((it (car !--list))
(acc !--acc))
(setq !--acc ,(if (functionp form-or-fn) (list form-or-fn 'acc 'it) (list 'progn form-or-fn)))
(setq !--acc ,(if (functionp form-or-fn) (list form-or-fn 'acc 'it) form-or-fn))
(setq !--list (cdr !--list))))
!--acc))
@ -63,7 +68,9 @@
(apply 'append (append lists '(nil))))
(defalias '!select '!filter)
(defalias '!reject 'remove-if)
(defmacro !reject (form-or-fn list)
`(!filter (not ,(!--call-with-it form-or-fn)) ,list))
(defalias '!partial 'apply-partially)

@ -4,12 +4,6 @@
(defun even? (num) (= 0 (% num 2)))
(defun square (num) (* num num))
(ert-deftest filter ()
"`!filter' returns a new list of only those elements where the predicate was non-nil."
(should (equal (!filter (lambda (num) (= 0 (% num 2))) '(1 2 3 4)) '(2 4)))
(should (equal (!filter (= 0 (% it 2)) '(1 2 3 4)) '(2 4)))
(should (equal (!filter even? '(1 2 3 4)) '(2 4))))
(ert-deftest map ()
"`!map' returns a new list with the results of calling the function on each element."
(should (equal (!map (lambda (num) (* num num)) '(1 2 3 4)) '(1 4 9 16)))
@ -29,6 +23,19 @@
(should (equal (!reduce (format "%s-%s" acc it) '(1 2 3)) "1-2-3"))
(should (equal (!reduce (format "%s-%s" acc it) '()) "nil-nil")))
(ert-deftest filter ()
"`!filter' returns a new list of only those elements where the predicate was non-nil."
(should (equal (!filter (lambda (num) (= 0 (% num 2))) '(1 2 3 4)) '(2 4)))
(should (equal (!filter (= 0 (% it 2)) '(1 2 3 4)) '(2 4)))
(should (equal (!filter even? '(1 2 3 4)) '(2 4)))
(should (equal (!select even? '(1 2 3 4)) '(2 4))))
(ert-deftest reject ()
"`!reject' returns a new list of only those elements where the predicate was nil."
(should (equal (!reject (lambda (num) (= 0 (% num 2))) '(1 2 3 4)) '(1 3)))
(should (equal (!reject (= 0 (% it 2)) '(1 2 3 4)) '(1 3)))
(should (equal (!reject even? '(1 2 3 4)) '(1 3))))
(ert-deftest concat ()
"`!concat' returns the concatenation of the elements in the supplied lists"
(should (equal (!concat) nil))

Loading…
Cancel
Save