summaryrefslogtreecommitdiff
path: root/lisp/emacs-lisp
diff options
context:
space:
mode:
authorLars Ingebrigtsen <larsi@gnus.org>2022-07-03 12:55:00 +0200
committerLars Ingebrigtsen <larsi@gnus.org>2022-07-03 12:55:00 +0200
commitb31680ef040d4a232619e8d070794a43d2cdca2c (patch)
treee67a2ce2dfe60d83dbc47fadf142da5608f1734f /lisp/emacs-lisp
parenta2f956a1d6fad6a2bc7c5d79eb3aa76cbb63cc40 (diff)
downloademacs-b31680ef040d4a232619e8d070794a43d2cdca2c.tar.gz
emacs-b31680ef040d4a232619e8d070794a43d2cdca2c.tar.bz2
emacs-b31680ef040d4a232619e8d070794a43d2cdca2c.zip
Add new function `seq-split'
* doc/lispref/sequences.texi (Sequence Functions): Document it. * lisp/emacs-lisp/seq.el (seq-split): New function. * lisp/emacs-lisp/shortdoc.el (sequence): Mention it.
Diffstat (limited to 'lisp/emacs-lisp')
-rw-r--r--lisp/emacs-lisp/seq.el15
-rw-r--r--lisp/emacs-lisp/shortdoc.el2
2 files changed, 17 insertions, 0 deletions
diff --git a/lisp/emacs-lisp/seq.el b/lisp/emacs-lisp/seq.el
index 947b64e8687..36c17f4cd5e 100644
--- a/lisp/emacs-lisp/seq.el
+++ b/lisp/emacs-lisp/seq.el
@@ -632,5 +632,20 @@ Signal an error if SEQUENCE is empty."
;; we automatically highlight macros.
(add-hook 'emacs-lisp-mode-hook #'seq--activate-font-lock-keywords))
+(defun seq-split (sequence length)
+ "Split SEQUENCE into a list of sub-sequences of at most LENGTH.
+All the sub-sequences will be of LENGTH, except the last one,
+which may be shorter."
+ (when (< length 1)
+ (error "Sub-sequence length must be larger than zero"))
+ (let ((result nil)
+ (seq-length (length sequence))
+ (start 0))
+ (while (< start seq-length)
+ (push (seq-subseq sequence start
+ (setq start (min seq-length (+ start length))))
+ result))
+ (nreverse result)))
+
(provide 'seq)
;;; seq.el ends here
diff --git a/lisp/emacs-lisp/shortdoc.el b/lisp/emacs-lisp/shortdoc.el
index c82aa3365cd..f53e783111c 100644
--- a/lisp/emacs-lisp/shortdoc.el
+++ b/lisp/emacs-lisp/shortdoc.el
@@ -889,6 +889,8 @@ A FUNC form can have any number of `:no-eval' (or `:no-value'),
:eval (seq-subseq '(a b c d e) 2 4))
(seq-take
:eval (seq-take '(a b c d e) 3))
+ (seq-split
+ :eval (seq-split [0 1 2 3 5] 2))
(seq-take-while
:eval (seq-take-while #'cl-evenp [2 4 9 6 5]))
(seq-uniq