From ed148b6824df9045670cbc837edc5f6dec05813f Mon Sep 17 00:00:00 2001 From: Daniel Mendler Date: Sat, 21 Mar 2026 09:37:07 +0100 Subject: compat-31: Rename any to member-if, keep alias --- NEWS.org | 2 +- compat-31.el | 4 +++- compat-tests.el | 25 +++++++++++++------------ 3 files changed, 17 insertions(+), 14 deletions(-) diff --git a/NEWS.org b/NEWS.org index c5c8b50..bc0fc46 100644 --- a/NEWS.org +++ b/NEWS.org @@ -16,7 +16,7 @@ - compat-31: New extended function =seconds-to-string=. - compat-31: New function =hash-table-contains-p=. - compat-31: New function =remove-display-text-property=. -- compat-31: New functions =drop-while=, =take-while=, =any=, =all=. +- compat-31: New functions =drop-while=, =take-while=, =member-if=, =any=, =all=. - Drop support for Emacs 24.x. Emacs 25.1 is required now. In case Emacs 24.x support is still needed, Compat 30 can be used. diff --git a/compat-31.el b/compat-31.el index adf9672..1e4f24f 100644 --- a/compat-31.el +++ b/compat-31.el @@ -47,12 +47,14 @@ "Non-nil if PRED is true for all elements in LIST." (not (drop-while pred list))) -(compat-defun any (pred list) ;; +(compat-defun member-if (pred list) ;; "Non-nil if PRED is true for at least one element in LIST. Returns the LIST suffix starting at the first element that satisfies PRED, or nil if none does." (drop-while (lambda (x) (not (funcall pred x))) list)) +(compat-defalias any member-if) ;; + (compat-defun hash-table-contains-p (key table) ;; "Return non-nil if TABLE has an element with KEY." (declare (side-effect-free t)) diff --git a/compat-tests.el b/compat-tests.el index 2df3f96..241149d 100644 --- a/compat-tests.el +++ b/compat-tests.el @@ -3079,20 +3079,21 @@ (should (equal (funcall (identity #'all) #'plusp ls) nil)) (should (equal (funcall (identity #'all) #'numberp ls) t)))) -(ert-deftest compat-any () - (should (equal (any #'hash-table-p nil) nil)) +(ert-deftest compat-member-if () + (should (equal (member-if #'hash-table-p nil) nil)) (let ((ls (append '(3 2 1) '(0) '(-1 -2 -3)))) - (should (equal (any #'numberp ls) ls)) - (should (equal (any (lambda (x) (numberp x)) ls) ls)) - (should (equal (any #'plusp ls) ls)) - (should (equal (any #'zerop ls) '(0 -1 -2 -3))) - (should (equal (any #'bufferp ls) nil)) + (should (equal (member-if #'numberp ls) ls)) + (should (equal (member-if (lambda (x) (numberp x)) ls) ls)) + (should (equal (member-if #'plusp ls) ls)) + (should (equal (member-if #'zerop ls) '(0 -1 -2 -3))) + (should (equal (member-if #'bufferp ls) nil)) (let ((z 9)) - (should (equal (any (lambda (x) (< x z)) ls) ls)) - (should (equal (any (lambda (x) (< x (- z 9))) ls) '(-1 -2 -3))) - (should (equal (any (lambda (x) (> x z)) ls) nil))) - (should (equal (funcall (identity #'any) #'minusp ls) '(-1 -2 -3))) - (should (equal (funcall (identity #'any) #'stringp ls) nil)))) + (should (equal (member-if (lambda (x) (< x z)) ls) ls)) + (should (equal (member-if (lambda (x) (< x (- z 9))) ls) '(-1 -2 -3))) + (should (equal (member-if (lambda (x) (> x z)) ls) nil))) + (dolist (fun '(member-if any)) + (should (equal (funcall (identity fun) #'minusp ls) '(-1 -2 -3))) + (should (equal (funcall (identity fun) #'stringp ls) nil))))) (ert-deftest compat-hash-table-contains-p () (let ((h (make-hash-table :test #'equal))) -- cgit v1.0