Add some tests, and fix the bugs they uncovered.

master
Magnar Sveen 14 years ago
parent 3e0ef58cd4
commit d870282984
  1. 6
      README.md
  2. 9
      bang.el
  3. 8
      tests.el

@ -13,7 +13,7 @@ This is so much a work in progress that you should definitely not be using it ye
## Anaphoric functions
While `!filter` takes a function to filter the list by, you can also pass
it a forms - which will then be executed with `it` exposed as the list item.
it a form - which will then be executed with `it` exposed as the list item.
Here's an example:
(!filter (lambda (num) (= 0 (% num 2))) '(1 2 3 4)) ;; normal version
@ -22,7 +22,9 @@ Here's an example:
of course the original can also be written like
(!filter 'even? '(1 2 3 4))
(defun even? (num) (= 0 (% num 2)))
(!filter even? '(1 2 3 4))
which demonstrates the usefulness of both versions.

@ -46,12 +46,13 @@ It should only be set using dynamic scope with a let, like:
(!concat (!map fn list)))
(defmacro !filter (form-or-fn list)
`(let (!--result)
(while list
(let ((it (car 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))
(setq !--result (cons it !--result))))
(setq list (cdr list)))
(setq !--list (cdr !--list)))
(nreverse !--result)))
(defun !uniq (list)

@ -1,6 +1,14 @@
(require 'ert)
(require 'bang)
(defun even? (num) (= 0 (% num 2)))
(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 difference ()
"`!difference' returns a new list of only elements in list1 that are not in list2."
(should (equal (!difference '() '()) '()))

Loading…
Cancel
Save