GNU bug report logs -
#57548
Add new function `seq-positions'
Previous Next
Reported by: Damien Cassou <damien <at> cassou.me>
Date: Fri, 2 Sep 2022 18:50:02 UTC
Severity: normal
Tags: patch
Fixed in version 29.1
Done: Lars Ingebrigtsen <larsi <at> gnus.org>
Bug is archived. No further changes may be made.
Full log
View this message in rfc822 format
Damien Cassou <damien <at> 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.
This bug report was last modified 2 years and 317 days ago.
Previous Next
GNU bug tracking system
Copyright (C) 1999 Darren O. Benham,
1997,2003 nCipher Corporation Ltd,
1994-97 Ian Jackson.