Commit 5049f6b2 authored by Juergen Nickelsen's avatar Juergen Nickelsen
Browse files

have member function with &key :test

parent 8006e57d
......@@ -77,15 +77,15 @@ to the designated subsequence with START <= index < END."
(unless (predicate el)
(throw :found el)))))))
(defun member (item list &optional testpred)
(defun member (item list &key (test #'eq))
"Find first ITEM in LIST and return the tail of the list beginning with item.
Optional TESTPRED specifies a testpred function of two arguments to use instead of eq."
(flet ((testpred (or testpred #'eq)))
(if (null list)
nil
(if (testpred item (car list))
list
(member item (cdr list) testpred)))))
Keyword :TEST specifies a test predicate function of two arguments to use
instead of eq."
(if (null list)
nil
(if (funcall test item (car list)) ;be sure to use lexical var scope here!
list
(member item (cdr list) :test test))))
(defun member-if (predicate list)
"Find first item in LIST for which PREDICATE is true
......
......@@ -256,15 +256,15 @@
(test-is "find-if 2" (find-if #'evenp '(3 5 7 8 9 11)) 8)
(test-is "find-if-not 2" (find-if-not #'oddp '(3 5 7 8 9 11)) 8)
;; don't member
(test-is "member 0" (member 'a '(3 4 5 6)) nil)
;; member
(test-is "member 1" (member 4 '(3 4 5 6)) '(4 5 6))
(test-is "member 0" (member 'c nil) nil)
(test-is "member 1" (member 'c '(c)) '(c))
(test-is "member 2" (member 'c '(a b c d)) '(c d))
(test-is "member 3" (member 'c '(3 4 5 6)) nil)
(test-is "member 4" (member 4 '(3 4 5 6)) '(4 5 6))
;; member with custom test
(test-is "member 2" (member '(3 . 4) '((4 . a) (3 . d) (7 . c) (3 . 5) (5 . 9))
(lambda (a b) (eq (car a) (car b))))
(test-is "member 5" (member '(3 . 4) '((4 . a) (3 . d) (7 . c) (3 . 5) (5 . 9))
:test (lambda (a b) (eq (car a) (car b))))
'((3 . d) (7 . c) (3 . 5) (5 . 9)))
;; don't member
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment