From: Lars Ingebrigtsen Date: Tue, 4 Oct 2022 19:44:52 +0000 (+0200) Subject: Add 'seq-keep' X-Git-Tag: emacs-29.0.90~1856^2~14 X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=92df7cd923d0e870f08484cec06c2726be30882b;p=emacs.git Add 'seq-keep' * doc/lispref/sequences.texi (Sequence Functions): Document it. * lisp/emacs-lisp/seq.el (seq-keep): New function (bug#58278). --- diff --git a/doc/lispref/sequences.texi b/doc/lispref/sequences.texi index 12c15e6f9a2..bc5a4cf24a6 100644 --- a/doc/lispref/sequences.texi +++ b/doc/lispref/sequences.texi @@ -698,6 +698,19 @@ the same type as @var{sequence}. @end example @end defun +@defun seq-keep function sequence + This function returns a list of all non-@code{nil} results from +calling @var{function} on the elements in @var{sequence}. + +@example +@group +(seq-keep #'cl-digit-char-p '(?6 ?a ?7)) +@result{} (6 7) +@end group +@end example + +@end defun + @defun seq-reduce function sequence initial-value @cindex reducing sequences This function returns the result of calling @var{function} with diff --git a/etc/NEWS b/etc/NEWS index eb5d3afbd80..06e91f5d716 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -3150,6 +3150,11 @@ These can be used for buttons in buffers and the like. See the This returns a list of the (zero-based) indices of elements matching a given predicate in the specified sequence. ++++ +** New function 'seq-keep'. +This is like 'seq-map', but removes all non-nil results from the +returned list. + +++ ** New arguments MESSAGE and TIMEOUT of 'set-transient-map'. MESSAGE specifies a message to display after activating the transient diff --git a/lisp/emacs-lisp/seq.el b/lisp/emacs-lisp/seq.el index 31dcfa98b40..82ade0ac0c3 100644 --- a/lisp/emacs-lisp/seq.el +++ b/lisp/emacs-lisp/seq.el @@ -695,5 +695,9 @@ which may be shorter." result)) (nreverse result))) +(defun seq-keep (function sequence) + "Apply FUNCTION to SEQUENCE and return all non-nil results." + (delq nil (seq-map function sequence))) + (provide 'seq) ;;; seq.el ends here diff --git a/test/lisp/emacs-lisp/seq-tests.el b/test/lisp/emacs-lisp/seq-tests.el index d95b35c45eb..e22f86f0447 100644 --- a/test/lisp/emacs-lisp/seq-tests.el +++ b/test/lisp/emacs-lisp/seq-tests.el @@ -592,5 +592,11 @@ Evaluate BODY for each created sequence. (should (= (length list) 10000)) (should (= (length (seq-uniq (append list list))) 10000)))) +(ert-deftest test-seq-keep () + (should (equal (seq-keep #'cl-digit-char-p '(?6 ?a ?7)) + '(6 7))) + (should (equal (seq-keep #'cl-digit-char-p [?6 ?a ?7]) + '(6 7)))) + (provide 'seq-tests) ;;; seq-tests.el ends here