From c4a0eff0112298de0a97b63e4e22826bf2b4126c Mon Sep 17 00:00:00 2001 From: Nicolas Petton Date: Fri, 6 Feb 2015 15:55:57 +0100 Subject: [PATCH] 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 --- lisp/ChangeLog | 2 +- lisp/emacs-lisp/seq.el | 27 +++++++++++++++++++++++++++ test/ChangeLog | 5 +++-- test/automated/seq-tests.el | 16 ++++++++++++++++ 4 files changed, 47 insertions(+), 3 deletions(-) diff --git a/lisp/ChangeLog b/lisp/ChangeLog index aa58c5349aa..0cd2a4d04e8 100644 --- a/lisp/ChangeLog +++ b/lisp/ChangeLog @@ -1,6 +1,6 @@ 2015-02-06 Nicolas Petton - * 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 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." (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)) diff --git a/test/ChangeLog b/test/ChangeLog index 9ae9db3f8fb..f9a54b53420 100644 --- a/test/ChangeLog +++ b/test/ChangeLog @@ -1,6 +1,7 @@ -2015-02-02 Nicolas Petton +2015-02-06 Nicolas Petton - * 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 diff --git a/test/automated/seq-tests.el b/test/automated/seq-tests.el index cc89c889675..ecbc0043210 100644 --- a/test/automated/seq-tests.el +++ b/test/automated/seq-tests.el @@ -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 -- 2.11.4.GIT