unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Michael Heerdegen <michael_heerdegen@web.de>
To: Damien Cassou <damien@cassou.me>
Cc: 57548@debbugs.gnu.org
Subject: bug#57548: Add new function `seq-positions'
Date: Sat, 03 Sep 2022 03:42:02 +0200	[thread overview]
Message-ID: <877d2l5j4l.fsf@web.de> (raw)
In-Reply-To: <87edwtty5v.fsf@cassou.me> (Damien Cassou's message of "Fri, 02 Sep 2022 20:43:24 +0200")

Damien Cassou <damien@cassou.me> writes:

> here is a patch adding seq-positions to seq.el.

+@defun seq-positions sequence elt &optional testfn
+  This function returns a list of the positions of the elements in
+@var{sequence} that are equal to @var{elt}.  If the optional argument
+@var{testfn} is non-@code{nil}, it is a function of two arguments to
+use instead of the default @code{equal}.

We do not need to limit this to equivalence relations.  A TESTFUN of, say,
(apply-partially #'<= 10) could be similarly useful.

+;;;###autoload
+(cl-defgeneric seq-positions (sequence elt &optional testfn)
+  "Return a list of the positions of ELT in SEQ.
+Equality is defined by TESTFN if non-nil or by `equal' if nil."
+  (let ((result '())
+        (index 0))
+    (seq-doseq (e sequence)
+      (when (funcall (or testfn #'equal) e elt)
+        (push index result))
+      (setq index (1+ index)))
+    (nreverse result)))

Could this maybe (simpler) call `seq-do-indexed'?


TIA,

Michael.





  parent reply	other threads:[~2022-09-03  1:42 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-09-02 18:43 bug#57548: Add new function `seq-positions' Damien Cassou
2022-09-02 19:00 ` Eli Zaretskii
2022-09-03  8:01   ` Damien Cassou
2022-09-03 10:16     ` Eli Zaretskii
2022-09-03 12:27       ` Lars Ingebrigtsen
2022-09-03 13:03       ` Damien Cassou
2022-09-04  2:27     ` Michael Heerdegen
2022-09-04  8:44       ` Damien Cassou
2022-09-04 11:22         ` Lars Ingebrigtsen
2022-09-06  1:44         ` Michael Heerdegen
2022-09-03  1:42 ` Michael Heerdegen [this message]
2022-09-03  8:02   ` Damien Cassou

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

  List information: https://www.gnu.org/software/emacs/

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

  git send-email \
    --in-reply-to=877d2l5j4l.fsf@web.de \
    --to=michael_heerdegen@web.de \
    --cc=57548@debbugs.gnu.org \
    --cc=damien@cassou.me \
    /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 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).