From 0e5acda7a5a6034d3eb0bd42ca17206dff51964b Mon Sep 17 00:00:00 2001 From: "Basil L. Contovounesios" Date: Mon, 18 Jan 2021 09:03:57 +0000 Subject: [PATCH] Simplify -non-nil * dash.el (-non-nil): Use --filter for speed. * dev/examples.el (-non-nil): Extend tests. * README.md: * dash.texi: Regenerate docs. --- README.md | 6 ++++-- dash.el | 4 ++-- dash.texi | 12 ++++++++++-- dev/examples.el | 7 ++++++- 4 files changed, 22 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 135bc88..036cbcd 100644 --- a/README.md +++ b/README.md @@ -586,10 +586,12 @@ The comparison is done with `equal`. #### -non-nil `(list)` -Return all non-nil elements of `list`. +Return a copy of `list` with all nil items removed. ```el -(-non-nil '(1 nil 2 nil nil 3 4 nil 5 nil)) ;; => '(1 2 3 4 5) +(-non-nil '(nil 1 nil 2 nil nil 3 4 nil 5 nil)) ;; => '(1 2 3 4 5) +(-non-nil '((nil))) ;; => '((nil)) +(-non-nil '()) ;; => '() ``` #### -slice `(list from &optional to step)` diff --git a/dash.el b/dash.el index 449b53a..a800a1d 100644 --- a/dash.el +++ b/dash.el @@ -530,9 +530,9 @@ If you want to select the original items satisfying a predicate use `-filter'." (--keep (funcall fn it) list)) (defun -non-nil (list) - "Return all non-nil elements of LIST." + "Return a copy of LIST with all nil items removed." (declare (pure t) (side-effect-free t)) - (-remove 'null list)) + (--filter it list)) (defmacro --map-indexed (form list) "Anaphoric form of `-map-indexed'." diff --git a/dash.texi b/dash.texi index e55522b..1d2ac44 100644 --- a/dash.texi +++ b/dash.texi @@ -603,13 +603,21 @@ The comparison is done with @code{equal}. @anchor{-non-nil} @defun -non-nil (list) -Return all non-nil elements of @var{list}. +Return a copy of @var{list} with all nil items removed. @example @group -(-non-nil '(1 nil 2 nil nil 3 4 nil 5 nil)) +(-non-nil '(nil 1 nil 2 nil nil 3 4 nil 5 nil)) @result{} '(1 2 3 4 5) @end group +@group +(-non-nil '((nil))) + @result{} '((nil)) +@end group +@group +(-non-nil '()) + @result{} '() +@end group @end example @end defun diff --git a/dev/examples.el b/dev/examples.el index ebe616b..eb813f3 100644 --- a/dev/examples.el +++ b/dev/examples.el @@ -209,7 +209,12 @@ new list." (let ((l (list 1 2))) (setcar (-remove-item 2 l) 0) l) => '(1 2)) (defexamples -non-nil - (-non-nil '(1 nil 2 nil nil 3 4 nil 5 nil)) => '(1 2 3 4 5)) + (-non-nil '(nil 1 nil 2 nil nil 3 4 nil 5 nil)) => '(1 2 3 4 5) + (-non-nil '((()))) => '((())) + (-non-nil '()) => '() + (let ((l (list 1 2))) (setcar (-non-nil l) 0) l) => '(1 2) + (let ((l (list nil 1))) (setcar (-non-nil l) 0) l) => '(nil 1) + (let ((l (list 1 nil))) (setcar (-non-nil l) 0) l) => '(1 nil)) (defexamples -slice (-slice '(1 2 3 4 5) 1) => '(2 3 4 5)