From 0c673afe782ea04c81cebee2a4060581ff99184d Mon Sep 17 00:00:00 2001 From: Owen Rodley Date: Sat, 4 May 2013 18:30:06 +1200 Subject: [PATCH] Add KEY and TEST args to MEMBER --- src/list.lisp | 4 ++-- tests/list.lisp | 6 ++++++ 2 files changed, 8 insertions(+), 2 deletions(-) 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 -- 1.7.10.4