emacs-elpa-diffs
[Top][All Lists]
Advanced

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

[elpa] master c90ed9f: Update seq.el to version 1.1


From: Nicolas Petton
Subject: [elpa] master c90ed9f: Update seq.el to version 1.1
Date: Fri, 06 Feb 2015 16:57:16 +0000

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

    Update seq.el to version 1.1
    
    * packages/seq/seq.el: Update to version 1.1
    * packages/seq/tests/seq-tests.el: Update to version 1.1
---
 packages/seq/seq.el             |   67 +++++++++++++++++++++++++++++++-------
 packages/seq/tests/seq-tests.el |   33 ++++++++++++++++++-
 2 files changed, 85 insertions(+), 15 deletions(-)

diff --git a/packages/seq/seq.el b/packages/seq/seq.el
index 679c82b..ca36515 100644
--- a/packages/seq/seq.el
+++ b/packages/seq/seq.el
@@ -1,10 +1,10 @@
 ;;; seq.el --- Sequence manipulation functions  -*- lexical-binding: t -*-
 
-;; Copyright (C) 2014 Free Software Foundation, Inc.
+;; Copyright (C) 2014-2015 Free Software Foundation, Inc.
 
-;; Author: Nicolas Petton <address@hidden>
+;; Author: Nicolas Petton <address@hidden>
 ;; Keywords: sequences
-;; Version: 1.0
+;; Version: 1.1
 ;; Package: seq
 
 ;; Maintainer: address@hidden
@@ -93,14 +93,14 @@ returned."
     (seq-subseq seq 0 (min (max n 0) (seq-length seq)))))
 
 (defun seq-drop-while (pred seq)
-  "Return a sequence, from the first element for which (PRED element) is nil, 
of SEQ.
+  "Return a sequence from the first element for which (PRED element) is nil in 
SEQ.
 The result is a sequence of the same type as SEQ."
   (if (listp seq)
       (seq--drop-while-list pred seq)
     (seq-drop seq (seq--count-successive pred seq))))
 
 (defun seq-take-while (pred seq)
-  "Return a sequence of the successive elements for which (PRED element) is 
non-nil in SEQ.
+  "Return the successive elements for which (PRED element) is non-nil in SEQ.
 The result is a sequence of the same type as SEQ."
   (if (listp seq)
       (seq--take-while-list pred seq)
@@ -153,7 +153,7 @@ If SEQ is empty, return INITIAL-VALUE and FUNCTION is not 
called."
     t))
 
 (defun seq-count (pred seq)
-  "Return the number of elements for which (PRED element) returns non-nil in 
seq."
+  "Return the number of elements for which (PRED element) is non-nil in SEQ."
   (let ((count 0))
     (seq-doseq (elt seq)
       (when (funcall pred elt)
@@ -198,14 +198,18 @@ If END is omitted, it defaults to the length of the 
sequence.
 If START or END is negative, it counts from the end."
   (cond ((or (stringp seq) (vectorp seq)) (substring seq start end))
         ((listp seq)
-         (let (len)
+         (let (len (errtext (format "Bad bounding indices: %s, %s" start end)))
            (and end (< end 0) (setq end (+ end (setq len (seq-length seq)))))
            (if (< start 0) (setq start (+ start (or len (setq len (seq-length 
seq))))))
-           (if (> start 0) (setq seq (nthcdr start seq)))
+           (when (> start 0)
+             (setq seq (nthcdr (1- start) seq))
+             (or seq (error "%s" errtext))
+             (setq seq (cdr seq)))
            (if end
                (let ((res nil))
-                 (while (>= (setq end (1- end)) start)
+                 (while (and (>= (setq end (1- end)) start) seq)
                    (push (pop seq) res))
+                 (or (= (1+ end) start) (error "%s" errtext))
                  (nreverse res))
              (seq-copy seq))))
         (t (error "Unsupported sequence: %s" seq))))
@@ -221,15 +225,50 @@ TYPE must be one of following symbols: vector, string or 
list.
     (`list (apply #'append (append seqs '(nil))))
     (t (error "Not a sequence type name: %s" type))))
 
+(defun seq-mapcat (function seq &optional type)
+  "Concatenate the result of applying FUNCTION to each element of SEQ.
+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."
+  "Return a list from LIST without its first N elements.
+This is an optimization for lists in `seq-drop'."
   (while (and list (> n 0))
     (setq list (cdr list)
           n (1- n)))
   list)
 
 (defun seq--take-list (list n)
-  "Optimized version of `seq-take' for lists."
+  "Return a list from LIST made of its first N elements.
+This is an optimization for lists in `seq-take'."
   (let ((result '()))
     (while (and list (> n 0))
       (setq n (1- n))
@@ -237,13 +276,15 @@ TYPE must be one of following symbols: vector, string or 
list.
     (nreverse result)))
 
 (defun seq--drop-while-list (pred list)
-  "Optimized version of `seq-drop-while' for lists."
+  "Return a list from the first element for which (PRED element) is nil in 
LIST.
+This is an optimization for lists in `seq-drop-while'."
   (while (and list (funcall pred (car list)))
     (setq list (cdr list)))
   list)
 
 (defun seq--take-while-list (pred list)
-  "Optimized version of `seq-take-while' for lists."
+  "Return the successive elements for which (PRED element) is non-nil in LIST.
+This is an optimization for lists in `seq-take-while'."
   (let ((result '()))
     (while (and list (funcall pred (car list)))
       (push (pop list) result))
diff --git a/packages/seq/tests/seq-tests.el b/packages/seq/tests/seq-tests.el
index 9fcda7f..ecbc004 100644
--- a/packages/seq/tests/seq-tests.el
+++ b/packages/seq/tests/seq-tests.el
@@ -2,7 +2,7 @@
 
 ;; Copyright (C) 2014-2015 Free Software Foundation, Inc.
 
-;; Author: Nicolas Petton <address@hidden>
+;; Author: Nicolas Petton <address@hidden>
 ;; Maintainer: address@hidden
 
 ;; This file is part of GNU Emacs.
@@ -182,7 +182,12 @@ Evaluate BODY for each created sequence.
     (should (same-contents-p (seq-subseq seq 1 -1) '(3 4))))
   (should (vectorp (seq-subseq [2 3 4 5] 2)))
   (should (stringp (seq-subseq "foo" 2 3)))
-  (should (listp (seq-subseq '(2 3 4 4) 2 3))))
+  (should (listp (seq-subseq '(2 3 4 4) 2 3)))
+  (should-error (seq-subseq '(1 2 3) 4))
+  (should-not   (seq-subseq '(1 2 3) 3))
+  (should       (seq-subseq '(1 2 3) -3))
+  (should-error (seq-subseq '(1 2 3) 1 4))
+  (should       (seq-subseq '(1 2 3) 1 3)))
 
 (ert-deftest test-seq-concatenate ()
   (with-test-sequences (seq '(2 4 6))
@@ -192,5 +197,29 @@ Evaluate BODY for each created sequence.
     (should (equal (seq-concatenate 'vector nil '(8 10)) [8 10]))
     (should (equal (seq-concatenate 'vector seq nil) [2 4 6]))))
 
+(ert-deftest test-seq-mapcat ()
+  (should (equal (seq-mapcat #'seq-reverse '((3 2 1) (6 5 4)))
+                 '(1 2 3 4 5 6)))
+  (should (equal (seq-mapcat #'seq-reverse '[(3 2 1) (6 5 4)])
+                 '(1 2 3 4 5 6)))
+  (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]