From: Owen Rodley Date: Sat, 4 May 2013 06:30:06 +0000 (+1200) Subject: Add KEY and TEST args to MEMBER X-Git-Url: http://repo.macrolet.net/gitweb/?a=commitdiff_plain;h=0c673afe782ea04c81cebee2a4060581ff99184d;p=jscl.git Add KEY and TEST args to MEMBER --- diff --git a/src/list.lisp b/src/list.lisp index a901395..02d1984 100644 --- a/src/list.lisp +++ b/src/list.lisp @@ -170,9 +170,9 @@ (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)))) diff --git a/tests/list.lisp b/tests/list.lisp index 0b76ba4..d8086e2 100644 --- a/tests/list.lisp +++ b/tests/list.lisp @@ -63,6 +63,12 @@ (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