From 994cda98cab17677baad3358841afe8d22fbdae9 Mon Sep 17 00:00:00 2001 From: "Basil L. Contovounesios" Date: Sat, 9 Jan 2021 20:54:50 +0000 Subject: [PATCH] Simplify -cons-pair? * dash.el (-cons-pair?): Simplify for speed. * dev/examples.el (-cons-pair?): New test. * README.md: * dash.texi: Regenerate docs. --- README.md | 13 +++++++++++++ dash.el | 12 +++++------- dash.texi | 22 ++++++++++++++++++++++ dev/examples.el | 11 ++++++++++- 4 files changed, 50 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 0b29a8c..7562956 100644 --- a/README.md +++ b/README.md @@ -210,6 +210,7 @@ value rather than consuming a list to produce a single value. * [-is-prefix?](#-is-prefix-prefix-list) `(prefix list)` * [-is-suffix?](#-is-suffix-suffix-list) `(suffix list)` * [-is-infix?](#-is-infix-infix-list) `(infix list)` +* [-cons-pair?](#-cons-pair-obj) `(obj)` ### Partitioning @@ -1345,6 +1346,18 @@ Alias: `-is-infix-p` (-is-infix? '(3 4 5) '(1 2 3 4 5)) ;; => t ``` +#### -cons-pair? `(obj)` + +Return non-nil if `obj` is a true cons pair. +That is, a cons (`a` . `b`) where `b` is not a list. +Alias: `-cons-pair-p`. + +```el +(-cons-pair? '(1 . 2)) ;; => t +(-cons-pair? '(1 2)) ;; => nil +(-cons-pair? '(1)) ;; => nil +``` + ## Partitioning diff --git a/dash.el b/dash.el index f249f5b..0097b42 100644 --- a/dash.el +++ b/dash.el @@ -2724,14 +2724,12 @@ the new seed." (declare (debug (form form))) `(-unfold (lambda (it) ,form) ,seed)) -(defun -cons-pair? (con) - "Return non-nil if CON is true cons pair. -That is (A . B) where B is not a list. - -Alias: `-cons-pair-p'" +(defun -cons-pair? (obj) + "Return non-nil if OBJ is a true cons pair. +That is, a cons (A . B) where B is not a list. +Alias: `-cons-pair-p'." (declare (pure t) (side-effect-free t)) - (and (listp con) - (not (listp (cdr con))))) + (nlistp (cdr-safe obj))) (defalias '-cons-pair-p '-cons-pair?) diff --git a/dash.texi b/dash.texi index 175035e..1b0addf 100644 --- a/dash.texi +++ b/dash.texi @@ -1903,6 +1903,28 @@ Alias: @code{-is-infix-p} @end example @end defun +@anchor{-cons-pair?} +@defun -cons-pair? (obj) +Return non-nil if @var{obj} is a true cons pair. +That is, a cons (@var{a} . @var{b}) where @var{b} is not a list. +Alias: @code{-cons-pair-p}. + +@example +@group +(-cons-pair? '(1 . 2)) + @result{} t +@end group +@group +(-cons-pair? '(1 2)) + @result{} nil +@end group +@group +(-cons-pair? '(1)) + @result{} nil +@end group +@end example +@end defun + @node Partitioning @section Partitioning diff --git a/dev/examples.el b/dev/examples.el index fb2d896..575684d 100644 --- a/dev/examples.el +++ b/dev/examples.el @@ -618,7 +618,16 @@ value rather than consuming a list to produce a single value." (-is-infix? '(2 3 4) '(1 2 3 4 5)) => t (-is-infix? '(3 4 5) '(1 2 3 4 5)) => t (-is-infix? '(2 3 4) '(1 2 4 5)) => nil - (-is-infix? '(2 4) '(1 2 3 4 5)) => nil)) + (-is-infix? '(2 4) '(1 2 3 4 5)) => nil) + + (defexamples -cons-pair? + (-cons-pair? '(1 . 2)) => t + (-cons-pair? '(1 2)) => nil + (-cons-pair? '(1)) => nil + (-cons-pair? ()) => nil + (-cons-pair? "") => nil + (-cons-pair? '(1 2 . 3)) => nil + (-cons-pair? '(() . "")) => t)) (def-example-group "Partitioning" "Functions partitioning the input list into a list of lists."