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: tomas
Subject: Re: Is there equivalent internal function for this list-has-elements?
Date: Sun, 18 Oct 2020 22:58:50 +0200
User-agent: Mutt/1.5.21 (2010-09-15)

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)

> (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

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)

See elisp manual "6.1 Sequences"

All untested.

Cheers
 - t

Attachment: signature.asc
Description: Digital signature


reply via email to

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