emacs-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Emacs-diffs] master c4a0eff 2/4: Add seq-partition and seq-group-by


From: Nicolas Petton
Subject: [Emacs-diffs] master c4a0eff 2/4: Add seq-partition and seq-group-by
Date: Fri, 06 Feb 2015 15:02:20 +0000

branch: master
commit c4a0eff0112298de0a97b63e4e22826bf2b4126c
Author: Nicolas Petton <address@hidden>
Commit: Nicolas Petton <address@hidden>

    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 aa58c53..0cd2a4d 100644
--- a/lisp/ChangeLog
+++ b/lisp/ChangeLog
@@ -1,6 +1,6 @@
 2015-02-06  Nicolas Petton <address@hidden>
 
-       * 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  <address@hidden>
 
diff --git a/lisp/emacs-lisp/seq.el b/lisp/emacs-lisp/seq.el
index bd234a3..cd45989 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 9ae9db3..f9a54b5 100644
--- a/test/ChangeLog
+++ b/test/ChangeLog
@@ -1,6 +1,7 @@
-2015-02-02 Nicolas Petton <address@hidden>
+2015-02-06 Nicolas Petton <address@hidden>
 
-       * 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  <address@hidden>
 
diff --git a/test/automated/seq-tests.el b/test/automated/seq-tests.el
index cc89c88..ecbc004 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



reply via email to

[Prev in Thread] Current Thread [Next in Thread]