From 1f6fdf123bf393ea36ace9dfe1bd97a894996ed7 Mon Sep 17 00:00:00 2001 From: Dmitry Gutov Date: Tue, 21 Nov 2023 03:18:45 +0200 Subject: [PATCH] (seq-contains-pred): Split off list-specialized impl into separate method * lisp/emacs-lisp/seq.el (seq-contains-pred): Split off list-specialized impl into separate method. The result is a bit slower (about 10%?), but better structured. --- lisp/emacs-lisp/seq.el | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/lisp/emacs-lisp/seq.el b/lisp/emacs-lisp/seq.el index 832a49d7845..d85c7297ee0 100644 --- a/lisp/emacs-lisp/seq.el +++ b/lisp/emacs-lisp/seq.el @@ -463,22 +463,25 @@ whether an element was found or not." ;; (t ;; (cl-call-next-method)))) -(cl-defgeneric seq-contains-pred (sequence &optional testfn) +(cl-defgeneric seq-contains-pred (_sequence &optional testfn) + (lambda (elt sequence) + (catch 'seq--break + (seq-doseq (e sequence) + (let ((r (funcall testfn e elt))) + (when r + (throw 'seq--break r)))) + nil))) + +(cl-defmethod seq-contains-pred ((_sequence list) &optional testfn) (cond - ((and (listp sequence) (or (null testfn) (eq testfn 'equal))) + ((or (null testfn) (eq testfn 'equal)) #'member) - ((and (listp sequence) (eq testfn 'eql)) + ((eq testfn 'eql) #'memql) - ((and (listp sequence) (eq testfn 'eq)) + ((eq testfn 'eq) #'memq) (t - (lambda (elt sequence) - (catch 'seq--break - (seq-doseq (e sequence) - (let ((r (funcall testfn e elt))) - (when r - (throw 'seq--break r)))) - nil))))) + (cl-call-next-method)))) (cl-defgeneric seq-set-equal-p (sequence1 sequence2 &optional testfn) "Return non-nil if SEQUENCE1 and SEQUENCE2 contain the same elements.