Add KEY and TEST args to MEMBER
authorOwen Rodley <Strigoides@gmail.com>
Sat, 4 May 2013 06:30:06 +0000 (18:30 +1200)
committerOwen Rodley <Strigoides@gmail.com>
Sat, 4 May 2013 06:30:06 +0000 (18:30 +1200)
src/list.lisp
tests/list.lisp

index a901395..02d1984 100644 (file)
   (and (consp (cdr x))
        (cons (car x) (butlast (cdr x)))))
 
-(defun member (x list)
+(defun member (x list &key (key #'identity) (test #'eql))
   (while list
-    (when (eql x (car list))
+    (when (funcall test x (funcall key (car list)))
       (return list))
     (setq list (cdr list))))
 
index 0b76ba4..d8086e2 100644 (file)
   (test (not   (assoc  2 alist)))
   (test (not   (rassoc 1 alist))))
 
+; MEMBER
+(test (equal (member 2 '(1 2 3)) '(2 3)))
+(test (not   (member 4 '(1 2 3))))
+(test (equal (member 4 '((1 . 2) (3 . 4)) :key #'cdr) '((3 . 4))))
+(test (member '(2) '((1) (2) (3)) :test #'equal))
+
 ; SUBST
 ; Can't really test this until EQUAL works properly on lists