1
0
mirror of https://git.savannah.gnu.org/git/emacs.git synced 2024-11-29 07:58:28 +00:00

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
This commit is contained in:
Nicolas Petton 2015-02-06 15:55:57 +01:00
parent 05211a578e
commit c4a0eff011
4 changed files with 47 additions and 3 deletions

View File

@ -1,6 +1,6 @@
2015-02-06 Nicolas Petton <nicolas@petton.fr>
* emacs-lisp/seq.el (seq-mapcat): New function.
* emacs-lisp/seq.el (seq-mapcat, seq-partition, seq-group-by): New functions.
2015-02-06 Artur Malabarba <bruce.connor.am@gmail.com>

View File

@ -230,6 +230,33 @@ The result is a sequence of type TYPE, or a list if TYPE is nil."
(apply #'seq-concatenate (or type 'list)
(seq-map function seq)))
(defun seq-partition (seq n)
"Return a list of the elements of SEQ grouped into sub-sequences of length N.
The last sequence may contain less than N elements. If N is a
negative integer or 0, nil is returned."
(unless (< n 1)
(let ((result '()))
(while (not (seq-empty-p seq))
(push (seq-take seq n) result)
(setq seq (seq-drop seq n)))
(nreverse result))))
(defun seq-group-by (function seq)
"Apply FUNCTION to each element of SEQ.
Separate the elements of SEQ into an alist using the results as
keys. Keys are compared using `equal'."
(nreverse
(seq-reduce
(lambda (acc elt)
(let* ((key (funcall function elt))
(cell (assoc key acc)))
(if cell
(setcdr cell (push elt (cdr cell)))
(push (list key elt) acc))
acc))
seq
nil)))
(defun seq--drop-list (list n)
"Optimized version of `seq-drop' for lists."
(while (and list (> n 0))

View File

@ -1,6 +1,7 @@
2015-02-02 Nicolas Petton <nicolas@petton.fr>
2015-02-06 Nicolas Petton <nicolas@petton.fr>
* automated/seq-tests.el: New test for seq-mapcat.
* automated/seq-tests.el: New tests for seq-mapcat, seq-partition
and seq-group-by.
2015-02-05 Artur Malabarba <bruce.connor.am@gmail.com>

View File

@ -205,5 +205,21 @@ Evaluate BODY for each created sequence.
(should (equal (seq-mapcat #'seq-reverse '((3 2 1) (6 5 4)) 'vector)
'[1 2 3 4 5 6])))
(ert-deftest test-seq-partition ()
(should (same-contents-p (seq-partition '(0 1 2 3 4 5 6 7) 3)
'((0 1 2) (3 4 5) (6 7))))
(should (same-contents-p (seq-partition '[0 1 2 3 4 5 6 7] 3)
'([0 1 2] [3 4 5] [6 7])))
(should (same-contents-p (seq-partition "Hello world" 2)
'("He" "ll" "o " "wo" "rl" "d")))
(should (equal (seq-partition '() 2) '()))
(should (equal (seq-partition '(1 2 3) -1) '())))
(ert-deftest test-seq-group-by ()
(should (equal (seq-group-by #'test-sequences-oddp [1 2 3 4])
'((t 3 1) (nil 4 2))))
(should (equal (seq-group-by #'car '((a 1) (b 3) (c 4) (a 2)))
'((a (a 2) (a 1)) (b (b 3)) (c (c 4))))))
(provide 'seq-tests)
;;; seq-tests.el ends here