aboutsummaryrefslogtreecommitdiffstats
path: root/lisp
diff options
context:
space:
mode:
authorNicolas Petton2015-02-06 15:55:57 +0100
committerNicolas Petton2015-02-06 15:55:57 +0100
commitc4a0eff0112298de0a97b63e4e22826bf2b4126c (patch)
tree8cf3babd3fcb25769c6cd36a52bb7753b143bbe2 /lisp
parent05211a578ed2c52f6ed818fc173561afbaea54c2 (diff)
downloademacs-c4a0eff0112298de0a97b63e4e22826bf2b4126c.tar.gz
emacs-c4a0eff0112298de0a97b63e4e22826bf2b4126c.zip
Add seq-partition and seq-group-by
* lisp/emacs-lisp/seq.el: Better docstring for seq.el functions * test/automated/seq-tests.el: New tests for seq-partition and seq-group-by
Diffstat (limited to 'lisp')
-rw-r--r--lisp/ChangeLog2
-rw-r--r--lisp/emacs-lisp/seq.el27
2 files changed, 28 insertions, 1 deletions
diff --git a/lisp/ChangeLog b/lisp/ChangeLog
index aa58c5349aa..0cd2a4d04e8 100644
--- a/lisp/ChangeLog
+++ b/lisp/ChangeLog
@@ -1,6 +1,6 @@
12015-02-06 Nicolas Petton <nicolas@petton.fr> 12015-02-06 Nicolas Petton <nicolas@petton.fr>
2 2
3 * emacs-lisp/seq.el (seq-mapcat): New function. 3 * emacs-lisp/seq.el (seq-mapcat, seq-partition, seq-group-by): New functions.
4 4
52015-02-06 Artur Malabarba <bruce.connor.am@gmail.com> 52015-02-06 Artur Malabarba <bruce.connor.am@gmail.com>
6 6
diff --git a/lisp/emacs-lisp/seq.el b/lisp/emacs-lisp/seq.el
index bd234a3b55a..cd45989eca8 100644
--- a/lisp/emacs-lisp/seq.el
+++ b/lisp/emacs-lisp/seq.el
@@ -230,6 +230,33 @@ The result is a sequence of type TYPE, or a list if TYPE is nil."
230 (apply #'seq-concatenate (or type 'list) 230 (apply #'seq-concatenate (or type 'list)
231 (seq-map function seq))) 231 (seq-map function seq)))
232 232
233(defun seq-partition (seq n)
234 "Return a list of the elements of SEQ grouped into sub-sequences of length N.
235The last sequence may contain less than N elements. If N is a
236negative integer or 0, nil is returned."
237 (unless (< n 1)
238 (let ((result '()))
239 (while (not (seq-empty-p seq))
240 (push (seq-take seq n) result)
241 (setq seq (seq-drop seq n)))
242 (nreverse result))))
243
244(defun seq-group-by (function seq)
245 "Apply FUNCTION to each element of SEQ.
246Separate the elements of SEQ into an alist using the results as
247keys. Keys are compared using `equal'."
248 (nreverse
249 (seq-reduce
250 (lambda (acc elt)
251 (let* ((key (funcall function elt))
252 (cell (assoc key acc)))
253 (if cell
254 (setcdr cell (push elt (cdr cell)))
255 (push (list key elt) acc))
256 acc))
257 seq
258 nil)))
259
233(defun seq--drop-list (list n) 260(defun seq--drop-list (list n)
234 "Optimized version of `seq-drop' for lists." 261 "Optimized version of `seq-drop' for lists."
235 (while (and list (> n 0)) 262 (while (and list (> n 0))