|
From: | Jean Louis |
Subject: | Re: Testing whether a list contains at least one non-nil element |
Date: | Wed, 26 Oct 2022 21:56:30 +0300 |
User-agent: | Mutt/2.2.7+37 (a90f69b) (2022-09-02) |
* Emanuel Berg <incal@dataswamp.org> [2022-10-26 15:55]: > Jean Louis wrote: > > >> I would like to test whether a list contains at least one > >> non-nil element? If I do that, then I would be able to > >> continue a loop where a non-nil element would mean that > >> a match was found. > > > > Here is the test for non nil element: > > > > (elt (delq nil (list nil nil)) 0) ⇒ nil > > > > (elt (delq nil (list nil 1)) 0) ⇒ 1 > > Interesting question ... > > (cl-position-if (lambda (e) e) '(nil nil)) ; nil > (cl-position-if (lambda (e) e) '(nil 1)) ; 1 While `remq' is more suitable as not destructive one: (elt (remq nil (list nil nil)) 0) ⇒ nil (elt (remq nil (list nil "any")) 0) ⇒ "any" cl- is Common Lisp prefix to conform those people who are used to Common Lisp. Those functions are bloated. (defun list-has-non-nil-p (list) "Test if list has non nil element." (elt (remq nil list) 0)) (list-has-non-nil-p '(1 2 3)) ⇒ 1 (list-has-non-nil-p '(nil nil nil)) ⇒ nil -- Jean Take action in Free Software Foundation campaigns: https://www.fsf.org/campaigns In support of Richard M. Stallman https://stallmansupport.org/
[Prev in Thread] | Current Thread | [Next in Thread] |