|
From: | Jean Louis |
Subject: | Re: [External] : Re: Testing whether a list contains at least one non-nil element |
Date: | Thu, 27 Oct 2022 23:38:57 +0300 |
User-agent: | Mutt/2.2.7+37 (a90f69b) (2022-09-02) |
* Drew Adams <drew.adams@oracle.com> [2022-10-27 18:54]: > In the worst case for your code, the first element IS non-nil and > you spend forever doing useless stuff. In no case is your code as > efficient as just testing each list element, starting at the > beginning, and STOPPING as soon as you find a non-nil element. Of course I agree with the thought in general. Is it this below? (defun check-if-any-elt-is-non-nil (list) (let (there-is) (while (and list (not there-is)) (when (car list) (setq there-is t)) (setq list (cdr list))) there-is)) (check-if-any-elt-is-non-nil '(nil nil nil nil nil nil nil nil 1 nil)) ⇒ t (check-if-any-elt-is-non-nil '(nil nil nil nil nil nil nil nil nil)) ⇒ nil (benchmark 10000000 (check-if-any-elt-is-non-nil (append (list 1) (make-list 1000000 nil)))) ⇒ "Elapsed time: 0.771948s" (benchmark 10000000 (check-if-any-elt-is-non-nil (append (make-list 1000000 nil) (list 1) ))) ⇒ "Elapsed time: 0.744323s" -- 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] |