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

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

RE: [External] : Re: Testing whether a list contains at least one non-ni


From: Drew Adams
Subject: RE: [External] : Re: Testing whether a list contains at least one non-nil element
Date: Thu, 27 Oct 2022 15:54:25 +0000

> >   ;; Why do the rest of this?
> >   ;; Search for a nil, after the non-nil car?
> >   ;; Then copy the entire list.  Then delete
> >   ;; all nils from it?  Why on earth do this?
> >   (if (memq elt list)
> >        ^^^^
> >       (delq elt (copy-sequence list))
> >                  ^^^^^^^^^^^^^
> >        ^^^^^^^^^
> >     list))
> 
> I get your reasoning. Maybe my function was not
> named correctly and then your logic jumps in.
> 
> > You certainly don't need to remove all nils
> > from the list.  If your list is 100,000,000
> > elements long and the first element is t, why
                     ^
                     IF
> > would you want to copy the entire list and
> > then remove all the nils from it?  Testing
> > the first element tells you the answer.
> 
> I get the reasoning, you are right, though maybe not in the context of
> testing if list has at least one non nil element. I am not sure, you
> know is not easy to grasp all.
> 
> What I know is that by testing the first element does not tell the
> answer if list has any non nil element:...
> So testing the first element did not give me the answer that
> my-list has one non-nil element.

No one said it did.  Please reread what I wrote.

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.

> Maybe you can show in Emacs Lisp practically how do you mean it.

I think I did.  Please reread.  Try your code.
Try it in the debugger.  Try the code that
others have sent.  Look at that code: it just
tests till it finds a non-nil element, then
stops - that's the point.  And no list copying.

It's not important perhaps, if your use case
doesn't care about such things.  But it's not
a bad idea to look at the code that defines
functions you use - in this case `remq', to
see how it behaves.  That's the real point
here.

And yes, the `cl-*' functions can be, and
typically are, implemented in a performant
way.

If you find an Emacs implementation of a CL
function that's not as good as it could be,
please report that.

Emacs's `cl-*' emulation is not a real,
performant CL implementation, nor does it
pretend to be.  But in some cases it could
perhaps be improved.

reply via email to

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