diff options
author | Lars Ingebrigtsen <larsi@gnus.org> | 2022-10-04 21:44:52 +0200 |
---|---|---|
committer | Lars Ingebrigtsen <larsi@gnus.org> | 2022-10-04 21:44:52 +0200 |
commit | 92df7cd923d0e870f08484cec06c2726be30882b (patch) | |
tree | f807122fce7f761ea97c9af634ed8a1f903895b3 | |
parent | 1d3d87cd678371b067887950acc640361d0ab87c (diff) | |
download | emacs-92df7cd923d0e870f08484cec06c2726be30882b.tar.gz |
Add 'seq-keep'
* doc/lispref/sequences.texi (Sequence Functions): Document it.
* lisp/emacs-lisp/seq.el (seq-keep): New function (bug#58278).
-rw-r--r-- | doc/lispref/sequences.texi | 13 | ||||
-rw-r--r-- | etc/NEWS | 5 | ||||
-rw-r--r-- | lisp/emacs-lisp/seq.el | 4 | ||||
-rw-r--r-- | test/lisp/emacs-lisp/seq-tests.el | 6 |
4 files changed, 28 insertions, 0 deletions
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 @@ -3151,6 +3151,11 @@ 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 map, including a special formatting spec to list available keys. 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 |