help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: return first element in list with certain property


From: Drew Adams
Subject: RE: return first element in list with certain property
Date: Mon, 20 Nov 2017 14:59:19 -0800 (PST)

> However the below tests seem to indicate no
> problems for any of the solutions suggested
> so far?

Certainly there will be no problem with any of them
when the list is small.

> (let ((test-list '(0 1 2 3 4 5)))
> 
>   (cl-dolist (e test-list)
>     (message "cl-dolist processing: %s" e)
>     (when (> e 1) (cl-return e) ))
> 
>   (cl-find-if (lambda (e) (and (message "cl-find-if processing: %s" e)
>                                (> e 1) ))
>               test-list)
> 
>   (seq-find   (lambda (e) (and (message "find-seq processing: %s" e)
>                                (> e 1) ))
>               test-list)
>   )
> ;; cl-dolist processing: 0
> ;; cl-dolist processing: 1
> ;; cl-dolist processing: 2
> ;; cl-find-if processing: 0
> ;; cl-find-if processing: 1
> ;; cl-find-if processing: 2
> ;; find-seq processing: 0
> ;; find-seq processing: 1
> ;; find-seq processing: 2

As I said, `cl-find-if' apparently traverses the list twice.

You see only one message for each element tested because your
predicate is called only once per element.  `cl-find-if' is
implemented by first calling `cl-position', which uses your
predicate.  And then, once a satisfactory element is found,
it calls `elt' to traverse the list again, to obtain that
element.  The call to `elt' does not use your predicate, so
you see no messages for it.

As I said, I have not bothered to profile the different
suggestions, but a guess is that with a very large list
`cl-find-if' will be slower.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]