Re: Handling deep list recursion

[email protected]
Newsgroups gmane.emacs.help
Message-ID <[email protected]>
Heime <[email protected]> writes:

>> 
>> >
>> > 1. If someone does not provide a recursive solution to your
>> > question, then you might consider writing an iterative
>> > solution using ‘dolist’ or ‘while’ or ‘do’.
>> 
>> I could use the `while' approach, because there is no binding
>> overhead.
>> 
>> >    (info "(elisp) Iteration")
>> >
>> > 2. Besides ‘cl-every’, you could write a solution using
>> > ‘cl-find-if’ or ‘cl-find-if-not’ or ‘cl-notany’.
>> 
>> cl-notany looks the most appropriate because it exits immediately
>> on first non-consp cell, matching the recursive early-return logic.
>
> Is this equivalent to (cl-every #'consp object)?  Does cl-notany
> return immediately as soon as an entry is not a consp?

I leave it to the Emacs maintainers to answer whether
cl-every and cl-notany short-circuit.  Both almost certainly
do.

>
>       (cl-notany (lambda (x) (not (consp x))) object)

More simply: (cl-notany #'atom object)

Examples:
   
;; A valid association list and two invalid alists:
(setq
 alist1     '((a . b) (c . d) ("e" "f") (gee . 8))
 not-alist1 '((a . b) (c . d) ("e" "f") nil (gee . 8))
 not-alist2 '(4.5 (a . b) (c . d) ("e" "f") (gee . 8)))

;; Using 'cl-notany':
(cl-notany #'atom alist1)
;;=> t

(cl-notany #'atom not-alist1)
;;=> nil

(cl-notany #'atom not-alist2)
;;=> nil

;; Using 'cl-every'
(cl-every #'consp alist1)
;;=> t

(cl-every #'consp not-alist1)
;;=> nil

(cl-every #'consp not-alist2)
;;=> nil

-- 
The lyf so short, the craft so long to lerne.
- Geoffrey Chaucer, The Parliament of Birds.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.