unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* Is there already an alist function which gets all matching elements,  not just the first?
@ 2022-10-05 10:00 Alan Mackenzie
  2022-10-05 10:45 ` Robert Pluim
  2022-10-05 18:16 ` Philip Kaludercic
  0 siblings, 2 replies; 6+ messages in thread
From: Alan Mackenzie @ 2022-10-05 10:00 UTC (permalink / raw)
  To: emacs-devel

Hello, Emacs.

I want to be able to get all matching elements from an alist (thinking
about extending imenu).  Something like

    (assoc-all key list)

which would return a list of matches.  After all, we have functions
which _delete_ all matches from an alist.

Does such a function already exist, perhaps in cl-*.el?

Thanks!

-- 
Alan Mackenzie (Nuremberg, Germany).



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Is there already an alist function which gets all matching elements,  not just the first?
  2022-10-05 10:00 Is there already an alist function which gets all matching elements, not just the first? Alan Mackenzie
@ 2022-10-05 10:45 ` Robert Pluim
  2022-10-06 18:24   ` Alan Mackenzie
  2022-10-05 18:16 ` Philip Kaludercic
  1 sibling, 1 reply; 6+ messages in thread
From: Robert Pluim @ 2022-10-05 10:45 UTC (permalink / raw)
  To: Alan Mackenzie; +Cc: emacs-devel

>>>>> On Wed, 5 Oct 2022 10:00:10 +0000, Alan Mackenzie <acm@muc.de> said:

    Alan> Hello, Emacs.
    Alan> I want to be able to get all matching elements from an alist (thinking
    Alan> about extending imenu).  Something like

    Alan>     (assoc-all key list)

    Alan> which would return a list of matches.  After all, we have functions
    Alan> which _delete_ all matches from an alist.

    Alan> Does such a function already exist, perhaps in cl-*.el?

Just use assoc-delete-all and invert the test:

(assoc-delete-all mykey myalist (lambda (val key)
                                  (not (eq val key))))

Robert
-- 



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Is there already an alist function which gets all matching elements,  not just the first?
  2022-10-05 10:00 Is there already an alist function which gets all matching elements, not just the first? Alan Mackenzie
  2022-10-05 10:45 ` Robert Pluim
@ 2022-10-05 18:16 ` Philip Kaludercic
  2022-10-05 18:35   ` Lars Ingebrigtsen
  2022-10-06 18:34   ` Alan Mackenzie
  1 sibling, 2 replies; 6+ messages in thread
From: Philip Kaludercic @ 2022-10-05 18:16 UTC (permalink / raw)
  To: Alan Mackenzie; +Cc: emacs-devel

Alan Mackenzie <acm@muc.de> writes:

> Hello, Emacs.
>
> I want to be able to get all matching elements from an alist (thinking
> about extending imenu).  Something like
>
>     (assoc-all key list)
>
> which would return a list of matches.  After all, we have functions
> which _delete_ all matches from an alist.
>
> Does such a function already exist, perhaps in cl-*.el?

This appears to do the right thing

(map-filter (lambda (k _v) (eq k '3))
	    '((3 . 4)
	      (2 . 4)
	      (1 . 2)
	      (3 . 1)))
;; => ((3 . 4) (3 . 1))

Perhaps a `map-member' could be implemented that would do something like
what you are looking for?  Or does this already exist by some other name
(I don't really use map.el). I've certainly wanted something like this
more than a few times.

> Thanks!



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Is there already an alist function which gets all matching elements,  not just the first?
  2022-10-05 18:16 ` Philip Kaludercic
@ 2022-10-05 18:35   ` Lars Ingebrigtsen
  2022-10-06 18:34   ` Alan Mackenzie
  1 sibling, 0 replies; 6+ messages in thread
From: Lars Ingebrigtsen @ 2022-10-05 18:35 UTC (permalink / raw)
  To: Philip Kaludercic; +Cc: Alan Mackenzie, emacs-devel

Philip Kaludercic <philipk@posteo.net> writes:

> Perhaps a `map-member' could be implemented that would do something like
> what you are looking for?  Or does this already exist by some other name
> (I don't really use map.el). I've certainly wanted something like this
> more than a few times.

There's `map-filter'...  Hm...  but it takes a predicate, which makes it
not as convenient as (say) `assq'.

So, yes, perhaps there should be a... `map-members'?  `map-all-members'?



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Is there already an alist function which gets all matching elements,  not just the first?
  2022-10-05 10:45 ` Robert Pluim
@ 2022-10-06 18:24   ` Alan Mackenzie
  0 siblings, 0 replies; 6+ messages in thread
From: Alan Mackenzie @ 2022-10-06 18:24 UTC (permalink / raw)
  To: Robert Pluim; +Cc: emacs-devel

Hello, Robert.

On Wed, Oct 05, 2022 at 12:45:00 +0200, Robert Pluim wrote:
> >>>>> On Wed, 5 Oct 2022 10:00:10 +0000, Alan Mackenzie <acm@muc.de> said:

>     Alan> Hello, Emacs.
>     Alan> I want to be able to get all matching elements from an alist (thinking
>     Alan> about extending imenu).  Something like

>     Alan>     (assoc-all key list)

>     Alan> which would return a list of matches.  After all, we have functions
>     Alan> which _delete_ all matches from an alist.

>     Alan> Does such a function already exist, perhaps in cl-*.el?

> Just use assoc-delete-all and invert the test:

> (assoc-delete-all mykey myalist (lambda (val key)
>                                   (not (eq val key))))

Thanks for the idea!  But I'd have to copy the alist first, and then it
starts to look a bit contrived.

Maybe I should just write a straightforward loop to get this info.

> Robert
> -- 

-- 
Alan Mackenzie (Nuremberg, Germany).



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Is there already an alist function which gets all matching elements,  not just the first?
  2022-10-05 18:16 ` Philip Kaludercic
  2022-10-05 18:35   ` Lars Ingebrigtsen
@ 2022-10-06 18:34   ` Alan Mackenzie
  1 sibling, 0 replies; 6+ messages in thread
From: Alan Mackenzie @ 2022-10-06 18:34 UTC (permalink / raw)
  To: Philip Kaludercic; +Cc: emacs-devel

Hello, Philip.

On Wed, Oct 05, 2022 at 18:16:43 +0000, Philip Kaludercic wrote:
> Alan Mackenzie <acm@muc.de> writes:

> > Hello, Emacs.

> > I want to be able to get all matching elements from an alist (thinking
> > about extending imenu).  Something like

> >     (assoc-all key list)

> > which would return a list of matches.  After all, we have functions
> > which _delete_ all matches from an alist.

> > Does such a function already exist, perhaps in cl-*.el?

> This appears to do the right thing

> (map-filter (lambda (k _v) (eq k '3))
> 	    '((3 . 4)
> 	      (2 . 4)
> 	      (1 . 2)
> 	      (3 . 1)))
> ;; => ((3 . 4) (3 . 1))

The doc string for map-filter doesn't actually describe the function,
sadly.  It talks about key/value pairs whilst giving no clue where the
key and the value come from.  I suppose one might guess, but really
there's no alternative to studying the source code for map-filter.

> Perhaps a `map-member' could be implemented that would do something like
> what you are looking for?  Or does this already exist by some other name
> (I don't really use map.el). I've certainly wanted something like this
> more than a few times.

It seems like one of these things which quite a lot of people want, but
nobody wants it enough to be bothered to implement it.  ;-)

> > Thanks!

-- 
Alan Mackenzie (Nuremberg, Germany).



^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2022-10-06 18:34 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-10-05 10:00 Is there already an alist function which gets all matching elements, not just the first? Alan Mackenzie
2022-10-05 10:45 ` Robert Pluim
2022-10-06 18:24   ` Alan Mackenzie
2022-10-05 18:16 ` Philip Kaludercic
2022-10-05 18:35   ` Lars Ingebrigtsen
2022-10-06 18:34   ` Alan Mackenzie

Code repositories for project(s) associated with this public inbox

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

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).