From 667d011410d1ab53fb90a497eb07f0b65c933821 Mon Sep 17 00:00:00 2001 From: Stefan Kangas Date: Mon, 17 Feb 2025 00:52:45 +0100 Subject: New functions oddp and evenp * lisp/emacs-lisp/cl-lib.el (cl-oddp, cl-evenp): Move from here... * lisp/subr.el (oddp, evenp): ...to here. Make old names into aliases, documented as deprecated. Add type declarations. * lisp/obsolete/cl.el: Don't alias oddp and evenp. * test/lisp/emacs-lisp/cl-lib-tests.el (cl-lib-test-oddp) (cl-lib-test-evenp): Move tests from here... * test/lisp/subr-tests.el (subr-test-oddp, subr-test-evenp): ...to here. * lisp/emacs-lisp/shortdoc.el (number): Add oddp and evenp. (map): Prefer oddp and evenp to cl-oddp and cl-evenp. * doc/lispref/numbers.texi (Predicates on Numbers): Document above new functions oddp and evenp. * doc/misc/cl.texi (Predicates on Numbers): Delete cl-oddp and cl-evenp. (Other Clauses): Prefer oddp to cl-oddp. --- lisp/emacs-lisp/cl-lib.el | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) (limited to 'lisp/emacs-lisp/cl-lib.el') diff --git a/lisp/emacs-lisp/cl-lib.el b/lisp/emacs-lisp/cl-lib.el index dba01b28325..1fc36517796 100644 --- a/lisp/emacs-lisp/cl-lib.el +++ b/lisp/emacs-lisp/cl-lib.el @@ -280,17 +280,17 @@ so that they are registered at compile-time as well as run-time." (declare (side-effect-free t)) (< number 0)) -(defun cl-oddp (integer) - "Return t if INTEGER is odd." - (declare (side-effect-free t) - (compiler-macro (lambda (_) `(eq (logand ,integer 1) 1)))) - (eq (logand integer 1) 1)) +(defalias 'cl-oddp #'oddp + "Return t if INTEGER is odd. -(defun cl-evenp (integer) - "Return t if INTEGER is even." - (declare (side-effect-free t) - (compiler-macro (lambda (_) `(eq (logand ,integer 1) 0)))) - (eq (logand integer 1) 0)) +This function is considered deprecated in favor of the built-in function +`evenp' that was added in Emacs 31.1.") + +(defalias 'cl-evenp #'evenp + "Return t if INTEGER is even. + +This function is considered deprecated in favor of the built-in function +`evenp' that was added in Emacs 31.1.") (defconst cl-digit-char-table (let* ((digits (make-vector 256 nil)) -- cgit v1.2.3 From afbf932106fdd1043b0debe9fc9134e031e4c9a6 Mon Sep 17 00:00:00 2001 From: Stefan Kangas Date: Mon, 17 Feb 2025 21:58:53 +0100 Subject: New functions plusp and minusp * lisp/emacs-lisp/cl-lib.el (cl-plusp, cl-minusp): Move from here... * lisp/subr.el (plusp, minusp): ...to here. Make old names into aliases, documented as deprecated. Add type declarations. Change from defsubst to regular functions with compiler macros. * lisp/obsolete/cl.el: Don't alias plusp and minusp. * test/lisp/emacs-lisp/cl-lib-tests.el (cl-lib-test-plusp) (cl-lib-test-minusp): Move tests from here... * test/lisp/subr-tests.el (subr-test-plusp, subr-test-minusp): ...to here. * doc/lispref/numbers.texi (Predicates on Numbers): Document plusp and minusp. * doc/misc/cl.texi (Predicates on Numbers): Delete cl-plusp and cl-minusp. * lisp/emacs-lisp/shortdoc.el (number): Document plusp and minusp instead of cl-plusp and cl-minusp. --- doc/lispref/numbers.texi | 10 ++++++++++ doc/misc/cl.texi | 12 +----------- etc/NEWS | 7 +++++++ lisp/emacs-lisp/cl-lib.el | 18 ++++++++++-------- lisp/emacs-lisp/shortdoc.el | 12 ++++++------ lisp/obsolete/cl.el | 2 -- lisp/subr.el | 14 ++++++++++++++ test/lisp/emacs-lisp/cl-lib-tests.el | 32 -------------------------------- test/lisp/subr-tests.el | 32 ++++++++++++++++++++++++++++++++ 9 files changed, 80 insertions(+), 59 deletions(-) (limited to 'lisp/emacs-lisp/cl-lib.el') diff --git a/doc/lispref/numbers.texi b/doc/lispref/numbers.texi index ee43389399a..17fa1e05fee 100644 --- a/doc/lispref/numbers.texi +++ b/doc/lispref/numbers.texi @@ -409,6 +409,16 @@ if so, @code{nil} otherwise. The argument must be a number. @code{(zerop x)} is equivalent to @code{(= x 0)}. @end defun +@defun plusp number +This predicate tests whether its argument is positive, and returns +@code{t} if so, @code{nil} otherwise. The argument must be a number. +@end defun + +@defun minusp number +This predicate tests whether its argument is negative, and returns +@code{t} if so, @code{nil} otherwise. The argument must be a number. +@end defun + @defun oddp integer This predicate tests whether its argument is an odd number, and returns @code{t} if so, @code{nil} otherwise. The argument must be an integer. diff --git a/doc/misc/cl.texi b/doc/misc/cl.texi index f481f6f2721..029f11f520d 100644 --- a/doc/misc/cl.texi +++ b/doc/misc/cl.texi @@ -3055,7 +3055,7 @@ This section defines a few simple Common Lisp operations on numbers that were left out of Emacs Lisp. @menu -* Predicates on Numbers:: @code{cl-plusp}, @code{cl-minusp}, etc. +* Predicates on Numbers:: @code{cl-digit-char-p}, etc. * Numerical Functions:: @code{cl-floor}, @code{cl-ceiling}, etc. * Random Numbers:: @code{cl-random}, @code{cl-make-random-state}. * Implementation Parameters:: @code{cl-most-positive-float}, etc. @@ -3068,16 +3068,6 @@ that were left out of Emacs Lisp. These functions return @code{t} if the specified condition is true of the numerical argument, or @code{nil} otherwise. -@defun cl-plusp number -This predicate tests whether @var{number} is positive. It is an -error if the argument is not a number. -@end defun - -@defun cl-minusp number -This predicate tests whether @var{number} is negative. It is an -error if the argument is not a number. -@end defun - @defun cl-digit-char-p char radix Test if @var{char} is a digit in the specified @var{radix} (default is 10). If it is, return the numerical value of digit @var{char} in diff --git a/etc/NEWS b/etc/NEWS index ef491edf624..caeda0c08db 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -1287,6 +1287,13 @@ Previously, its argument was always evaluated using dynamic binding. * Lisp Changes in Emacs 31.1 ++++ +** New functions 'plusp' and 'minusp'. +They return non-nil if a number is positive or negative, respectively, +and signal an error if they are given a non-number. The 'cl-lib' +functions 'cl-plusp' and 'cl-minusp' are now aliases for 'plusp' and +'minusp'. + +++ ** New functions 'oddp' and 'evenp'. They return non-nil if an integer is odd or even, respectively, and diff --git a/lisp/emacs-lisp/cl-lib.el b/lisp/emacs-lisp/cl-lib.el index 1fc36517796..4a83e9d6a7c 100644 --- a/lisp/emacs-lisp/cl-lib.el +++ b/lisp/emacs-lisp/cl-lib.el @@ -270,15 +270,17 @@ so that they are registered at compile-time as well as run-time." (define-obsolete-function-alias 'cl-floatp-safe 'floatp "24.4") -(defsubst cl-plusp (number) - "Return t if NUMBER is positive." - (declare (side-effect-free t)) - (> number 0)) +(defalias 'cl-plusp #'plusp + "Return t if NUMBER is positive. -(defsubst cl-minusp (number) - "Return t if NUMBER is negative." - (declare (side-effect-free t)) - (< number 0)) +This function is considered deprecated in favor of the built-in function +`plusp' that was added in Emacs 31.1.") + +(defalias 'cl-minusp #'minusp + "Return t if NUMBER is negative. + +This function is considered deprecated in favor of the built-in function +`minusp' that was added in Emacs 31.1.") (defalias 'cl-oddp #'oddp "Return t if INTEGER is odd. diff --git a/lisp/emacs-lisp/shortdoc.el b/lisp/emacs-lisp/shortdoc.el index af995123dc5..23b9b582a9a 100644 --- a/lisp/emacs-lisp/shortdoc.el +++ b/lisp/emacs-lisp/shortdoc.el @@ -1412,12 +1412,12 @@ A FUNC form can have any number of `:no-eval' (or `:no-value'), :eval (natnump -1) :eval (natnump 0) :eval (natnump 23)) - (cl-plusp - :eval (cl-plusp 0) - :eval (cl-plusp 1)) - (cl-minusp - :eval (cl-minusp 0) - :eval (cl-minusp -1)) + (plusp + :eval (plusp 0) + :eval (plusp 1)) + (minusp + :eval (minusp 0) + :eval (minusp -1)) (oddp :eval (oddp 3)) (evenp diff --git a/lisp/obsolete/cl.el b/lisp/obsolete/cl.el index 4fb49b596e2..5baa155c592 100644 --- a/lisp/obsolete/cl.el +++ b/lisp/obsolete/cl.el @@ -272,8 +272,6 @@ first svref copy-seq - minusp - plusp floatp-safe declaim proclaim diff --git a/lisp/subr.el b/lisp/subr.el index 173c69675aa..225483a609c 100644 --- a/lisp/subr.el +++ b/lisp/subr.el @@ -550,6 +550,20 @@ was called." (compiler-macro (lambda (_) `(= 0 ,number)))) (= 0 number)) +(defun plusp (number) + "Return t if NUMBER is positive." + (declare (ftype (function (number) boolean)) + (side-effect-free t) + (compiler-macro (lambda (_) `(> ,number 0)))) + (> number 0)) + +(defun minusp (number) + "Return t if NUMBER is negative." + (declare (ftype (function (number) boolean)) + (side-effect-free t) + (compiler-macro (lambda (_) `(< ,number 0)))) + (< number 0)) + (defun oddp (integer) "Return t if INTEGER is odd." (declare (ftype (function (integer) boolean)) diff --git a/test/lisp/emacs-lisp/cl-lib-tests.el b/test/lisp/emacs-lisp/cl-lib-tests.el index 607161c6a7c..ff19ec74a43 100644 --- a/test/lisp/emacs-lisp/cl-lib-tests.el +++ b/test/lisp/emacs-lisp/cl-lib-tests.el @@ -79,38 +79,6 @@ (should (= (cl-decf (alist-get 'a alist 0)) -1)) (should (= (alist-get 'a alist 0) -1)))) -(ert-deftest cl-lib-test-plusp () - (should-not (cl-plusp -1.0e+INF)) - (should-not (cl-plusp -1.5e2)) - (should-not (cl-plusp -3.14)) - (should-not (cl-plusp -1)) - (should-not (cl-plusp -0.0)) - (should-not (cl-plusp 0)) - (should-not (cl-plusp 0.0)) - (should-not (cl-plusp -0.0e+NaN)) - (should-not (cl-plusp 0.0e+NaN)) - (should (cl-plusp 1)) - (should (cl-plusp 3.14)) - (should (cl-plusp 1.5e2)) - (should (cl-plusp 1.0e+INF)) - (should-error (cl-plusp "42") :type 'wrong-type-argument)) - -(ert-deftest cl-lib-test-minusp () - (should (cl-minusp -1.0e+INF)) - (should (cl-minusp -1.5e2)) - (should (cl-minusp -3.14)) - (should (cl-minusp -1)) - (should-not (cl-minusp -0.0)) - (should-not (cl-minusp 0)) - (should-not (cl-minusp 0.0)) - (should-not (cl-minusp -0.0e+NaN)) - (should-not (cl-minusp 0.0e+NaN)) - (should-not (cl-minusp 1)) - (should-not (cl-minusp 3.14)) - (should-not (cl-minusp 1.5e2)) - (should-not (cl-minusp 1.0e+INF)) - (should-error (cl-minusp "-42") :type 'wrong-type-argument)) - (ert-deftest cl-digit-char-p () (should (eql 3 (cl-digit-char-p ?3))) (should (eql 10 (cl-digit-char-p ?a 11))) diff --git a/test/lisp/subr-tests.el b/test/lisp/subr-tests.el index 1b209ab6383..5684a08254d 100644 --- a/test/lisp/subr-tests.el +++ b/test/lisp/subr-tests.el @@ -45,6 +45,38 @@ (should-not (zerop (1- most-negative-fixnum))) (should-error (zerop "-5") :type 'wrong-type-argument)) +(ert-deftest subr-test-plusp () + (should-not (plusp -1.0e+INF)) + (should-not (plusp -1.5e2)) + (should-not (plusp -3.14)) + (should-not (plusp -1)) + (should-not (plusp -0.0)) + (should-not (plusp 0)) + (should-not (plusp 0.0)) + (should-not (plusp -0.0e+NaN)) + (should-not (plusp 0.0e+NaN)) + (should (plusp 1)) + (should (plusp 3.14)) + (should (plusp 1.5e2)) + (should (plusp 1.0e+INF)) + (should-error (plusp "42") :type 'wrong-type-argument)) + +(ert-deftest subr-test-minusp () + (should (minusp -1.0e+INF)) + (should (minusp -1.5e2)) + (should (minusp -3.14)) + (should (minusp -1)) + (should-not (minusp -0.0)) + (should-not (minusp 0)) + (should-not (minusp 0.0)) + (should-not (minusp -0.0e+NaN)) + (should-not (minusp 0.0e+NaN)) + (should-not (minusp 1)) + (should-not (minusp 3.14)) + (should-not (minusp 1.5e2)) + (should-not (minusp 1.0e+INF)) + (should-error (minusp "-42") :type 'wrong-type-argument)) + (ert-deftest subr-test-oddp () (should (oddp -3)) (should (oddp 3)) -- cgit v1.2.3