[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/seq 380986d 02/53: Update seq.el to version 1.1
From: |
Stefan Monnier |
Subject: |
[elpa] externals/seq 380986d 02/53: Update seq.el to version 1.1 |
Date: |
Tue, 1 Dec 2020 17:06:03 -0500 (EST) |
branch: externals/seq
commit 380986da37cdd1fbd68868f16c3a9ea25e84a240
Author: Nicolas Petton <nicolas@petton.fr>
Commit: Nicolas Petton <nicolas@petton.fr>
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
---
seq.el | 67 +++++++++++++++++++++++++++++++++++++++++++-----------
tests/seq-tests.el | 33 +++++++++++++++++++++++++--
2 files changed, 85 insertions(+), 15 deletions(-)
diff --git a/seq.el b/seq.el
index 679c82b..ca36515 100644
--- a/seq.el
+++ b/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 <petton.nicolas@gmail.com>
+;; Author: Nicolas Petton <nicolas@petton.fr>
;; Keywords: sequences
-;; Version: 1.0
+;; Version: 1.1
;; Package: seq
;; Maintainer: emacs-devel@gnu.org
@@ -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/tests/seq-tests.el b/tests/seq-tests.el
index 9fcda7f..ecbc004 100644
--- a/tests/seq-tests.el
+++ b/tests/seq-tests.el
@@ -2,7 +2,7 @@
;; Copyright (C) 2014-2015 Free Software Foundation, Inc.
-;; Author: Nicolas Petton <petton.nicolas@gmail.com>
+;; Author: Nicolas Petton <nicolas@petton.fr>
;; Maintainer: emacs-devel@gnu.org
;; 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
- [elpa] branch externals/seq created (now 10d3983), Stefan Monnier, 2020/12/01
- [elpa] externals/seq 380986d 02/53: Update seq.el to version 1.1,
Stefan Monnier <=
- [elpa] externals/seq 088b2d6 09/53: Update seq.el to version 1.7, Stefan Monnier, 2020/12/01
- [elpa] externals/seq c555087 06/53: Rephrases a comment in seq.el about the order of the arguments, Stefan Monnier, 2020/12/01
- [elpa] externals/seq 81682a1 11/53: Update seq.el to version 1.9, Stefan Monnier, 2020/12/01
- [elpa] externals/seq 444ca62 01/53: packages/seq: New package, Stefan Monnier, 2020/12/01
- [elpa] externals/seq 503dc82 03/53: Update seq.el to version 1.1.1, Stefan Monnier, 2020/12/01
- [elpa] externals/seq 021f678 12/53: Update seq.el to version 1.11, Stefan Monnier, 2020/12/01
- [elpa] externals/seq 0ffc6fd 05/53: Update seq.el to version 1.3, Stefan Monnier, 2020/12/01
- [elpa] externals/seq 5d295d9 10/53: Update seq.el to version 1.8, Stefan Monnier, 2020/12/01
- [elpa] externals/seq 0455744 08/53: * packages/seq/seq.el: Update seq.el to version 1.5., Stefan Monnier, 2020/12/01
- [elpa] externals/seq 1580064 04/53: Update seq.el to version 1.2, Stefan Monnier, 2020/12/01