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

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

Re: Is there equivalent internal function for this list-has-elements?


From: Jean Louis
Subject: Re: Is there equivalent internal function for this list-has-elements?
Date: Mon, 19 Oct 2020 21:23:18 +0300
User-agent: Mutt/1.10.1 (2018-07-13)

* tomas@tuxteam.de <tomas@tuxteam.de> [2020-10-18 23:59]:
> On Sun, Oct 18, 2020 at 10:37:01PM +0300, Jean Louis wrote:
> > 
> > For the below function `list-has-elements' maybe there exist some
> > internal Emacs function that checks for list that elements that are
> > contained in the haystak? Is there any?
> > 
> > Other question is, if there is any function other than pushnew, if I
> > do not wish to use the pushnew? I can maybe just make a check if
> > element is in the list and then simply push?
> > 
> > (defun list-has (needle haystack)
> >   "Returns elements of haystack that contain needle, case insensitive"
> >   (let ((nlist))
> >     (dolist (element haystack (reverse nlist))
> >       (when (string-match needle element)
> >     (pushnew element nlist)))))
> 
> If I understand this one correctly, it can be expressed as:
> 
>   (seq-filter
>     (lambda (elt) (string-match needle elt))
>     haystack)

Thank you, I will use it with string-match-p tip.

> > (defun list-has-elements (needles haystack)
> >   "Returns elements of haystack that contain needle, case insensitive"
> >   (if needles
> >       (let* ((needle (pop needles))
> >          (haystack (list-has needle haystack)))
> >     (list-has-elements needles haystack))
> >     haystack))
> 
> The doc string sent me off to the weeds ;-D

Sorry for that, it was just duplicated badly.

> You want to filter out those elements in haystack which match *all*
> the needles? Then something like (Careful! untested!)
> 
>   (seq-reduce
>     (lambda (red-haystack needle) (list-has needle red-haystack))
>     needles
>     haystack)

That works for now on a small list, I will benchmark it as next to
compare what is faster, as I find those functions very useful.

Thanks,
Jean





reply via email to

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