all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: <tomas@tuxteam.de>
To: help-gnu-emacs@gnu.org
Subject: Re: Is there equivalent internal function for this list-has-elements?
Date: Sun, 18 Oct 2020 22:58:50 +0200	[thread overview]
Message-ID: <20201018205850.GA13219@tuxteam.de> (raw)
In-Reply-To: <courier.000000005F8C9961.0000177E@static.rcdrun.com>

[-- Attachment #1: Type: text/plain, Size: 1484 bytes --]

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 #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 198 bytes --]

  parent reply	other threads:[~2020-10-18 20:58 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-10-18 19:37 Is there equivalent internal function for this list-has-elements? Jean Louis
2020-10-18 20:43 ` Stefan Monnier
2020-10-19  7:15   ` tomas
     [not found]   ` <20201019180655.GL19325@protected.rcdrun.com>
     [not found]     ` <jwvpn5ef5dx.fsf-monnier+emacs@gnu.org>
2020-10-19 20:15       ` Jean Louis
2020-10-18 20:58 ` tomas [this message]
2020-10-18 21:21   ` Joost Kremers
2020-10-19  6:57     ` tomas
2020-10-19 18:08     ` Jean Louis
2020-10-19 18:23   ` Jean Louis

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20201018205850.GA13219@tuxteam.de \
    --to=tomas@tuxteam.de \
    --cc=help-gnu-emacs@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.