* Is there equivalent internal function for this list-has-elements?
@ 2020-10-18 19:37 Jean Louis
2020-10-18 20:43 ` Stefan Monnier
2020-10-18 20:58 ` tomas
0 siblings, 2 replies; 9+ messages in thread
From: Jean Louis @ 2020-10-18 19:37 UTC (permalink / raw)
To: help-gnu-emacs
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)))))
(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))
Thanks,
Jean
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: Is there equivalent internal function for this list-has-elements?
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>
2020-10-18 20:58 ` tomas
1 sibling, 2 replies; 9+ messages in thread
From: Stefan Monnier @ 2020-10-18 20:43 UTC (permalink / raw)
To: help-gnu-emacs
> Other question is, if there is any function other than pushnew, if I
> do not wish to use the pushnew?
Of course there is. And if you don't like the name `pushnew`, for
example because it starts with the distateful "p", you can use
`cl-pushnew`, which also has the advantage of not being deprecated.
> I can maybe just make a check if element is in the list and then
> simply push?
You mean, do the same as `pushnew`? Yes, of course.
You could also define a function or even a macro that does that
for you. A good name for it could `my-pushnew` ;-)
Stefan
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: Is there equivalent internal function for this list-has-elements?
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-18 20:58 ` tomas
2020-10-18 21:21 ` Joost Kremers
2020-10-19 18:23 ` Jean Louis
1 sibling, 2 replies; 9+ messages in thread
From: tomas @ 2020-10-18 20:58 UTC (permalink / raw)
To: help-gnu-emacs
[-- 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 --]
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: Is there equivalent internal function for this list-has-elements?
2020-10-18 20:58 ` tomas
@ 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
1 sibling, 2 replies; 9+ messages in thread
From: Joost Kremers @ 2020-10-18 21:21 UTC (permalink / raw)
To: help-gnu-emacs
On Sun, Oct 18 2020, tomas@tuxteam.de wrote:
> If I understand this one correctly, it can be expressed as:
>
> (seq-filter
> (lambda (elt) (string-match needle elt))
> haystack)
Small nit-pick, but it's generally better to use `string-match-p` if you're just
testing whether a string matches a regexp. `string-match` modifies the match
data, `string-match-p` doesn't.
--
Joost Kremers
Life has its moments
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: Is there equivalent internal function for this list-has-elements?
2020-10-18 21:21 ` Joost Kremers
@ 2020-10-19 6:57 ` tomas
2020-10-19 18:08 ` Jean Louis
1 sibling, 0 replies; 9+ messages in thread
From: tomas @ 2020-10-19 6:57 UTC (permalink / raw)
To: help-gnu-emacs
[-- Attachment #1: Type: text/plain, Size: 309 bytes --]
On Sun, Oct 18, 2020 at 11:21:05PM +0200, Joost Kremers wrote:
[...]
> Small nit-pick, but it's generally better to use `string-match-p` if you're just
> testing whether a string matches a regexp. `string-match` modifies the match
> data, `string-match-p` doesn't.
Thanks for the reminder :-)
Cheers
- t
[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 198 bytes --]
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: Is there equivalent internal function for this list-has-elements?
2020-10-18 20:43 ` Stefan Monnier
@ 2020-10-19 7:15 ` tomas
[not found] ` <20201019180655.GL19325@protected.rcdrun.com>
1 sibling, 0 replies; 9+ messages in thread
From: tomas @ 2020-10-19 7:15 UTC (permalink / raw)
To: help-gnu-emacs
[-- Attachment #1: Type: text/plain, Size: 427 bytes --]
On Sun, Oct 18, 2020 at 04:43:11PM -0400, Stefan Monnier wrote:
> > Other question is, if there is any function other than pushnew, if I
> > do not wish to use the pushnew?
>
> Of course there is. And if you don't like the name `pushnew`, for
> example [...]
Huh. You just made me aware (again) of how differently two people can
read one and the same thing.
Anyway, now the OP has the choice :-)
Cheers
- t
[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 198 bytes --]
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: Is there equivalent internal function for this list-has-elements?
2020-10-18 21:21 ` Joost Kremers
2020-10-19 6:57 ` tomas
@ 2020-10-19 18:08 ` Jean Louis
1 sibling, 0 replies; 9+ messages in thread
From: Jean Louis @ 2020-10-19 18:08 UTC (permalink / raw)
To: Joost Kremers; +Cc: help-gnu-emacs
* Joost Kremers <joostkremers@fastmail.fm> [2020-10-19 00:22]:
>
> On Sun, Oct 18 2020, tomas@tuxteam.de wrote:
> > If I understand this one correctly, it can be expressed as:
> >
> > (seq-filter
> > (lambda (elt) (string-match needle elt))
> > haystack)
>
> Small nit-pick, but it's generally better to use `string-match-p` if you're just
> testing whether a string matches a regexp. `string-match` modifies the match
> data, `string-match-p` doesn't.
Thanks.
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: Is there equivalent internal function for this list-has-elements?
2020-10-18 20:58 ` tomas
2020-10-18 21:21 ` Joost Kremers
@ 2020-10-19 18:23 ` Jean Louis
1 sibling, 0 replies; 9+ messages in thread
From: Jean Louis @ 2020-10-19 18:23 UTC (permalink / raw)
To: tomas; +Cc: help-gnu-emacs
* 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
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: Is there equivalent internal function for this list-has-elements?
[not found] ` <jwvpn5ef5dx.fsf-monnier+emacs@gnu.org>
@ 2020-10-19 20:15 ` Jean Louis
0 siblings, 0 replies; 9+ messages in thread
From: Jean Louis @ 2020-10-19 20:15 UTC (permalink / raw)
To: Stefan Monnier; +Cc: help-gnu-emacs
* Stefan Monnier <monnier@iro.umontreal.ca> [2020-10-19 21:20]:
> > I want to use function that is always unchangable or default within
> > Emacs for future years, and which is fast.
>
> `push` and `cl-pushnew` are your guys.
>
> > Apparently add-to-list is mentioned as not to be abused,
>
> The main problem with it is that it can only be used on dynamically
> scoped variables, whereas `push` and `cl-pushnew` can be used on any
> variable you like and even on any *place* you like.
>
> If it weren't for the fact that it's used in very many places, I'd have
> marked `add-to-list` as deprecated back in Emacs-24.
What you explained about dynamically scoped variables now makes sense.
Then those words "dynamically scoped variables" should be entered into
documentation for that function
>This is handy to add some elements to configuration variables,
but please do not abuse it in Elisp code, where you are usually
better off using ‘push’ or ‘cl-pushnew’.:
"Please do not abuse it in Elisp code" -- I did understand what it
means, I was here thinking more of using it let us say on very large
lists, that would be "abuse" for me, it is also in the sense of
excessively.
But then it is probably meant not to use it excessively in the sense
of too often.
That should be better clarified.
^ permalink raw reply [flat|nested] 9+ messages in thread
end of thread, other threads:[~2020-10-19 20:15 UTC | newest]
Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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
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
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).