Add 'seq-keep'

* doc/lispref/sequences.texi (Sequence Functions): Document it.
* lisp/emacs-lisp/seq.el (seq-keep): New function (bug#58278).
This commit is contained in:
Lars Ingebrigtsen 2022-10-04 21:44:52 +02:00
parent 1d3d87cd67
commit 92df7cd923
4 changed files with 28 additions and 0 deletions

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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