From 4fb5565d0a0cd9640a242028c92b8b4e2bd4683e Mon Sep 17 00:00:00 2001 From: Nicolas Petton Date: Wed, 11 Feb 2015 09:21:03 +0100 Subject: [PATCH] Add a backward-compatible version of seq-reverse * lisp/emacs-lisp/seq.el (seq-reverse): Add a backward-compatible version of seq-reverse that works on sequences in Emacs 24. Bump version to 1.2. * test/automated/seq-tests.el (test-seq-reverse, test-seq-group-by): Add a test for seq-reverse and update test for seq-group-by to test vectors and strings, not only lists. --- lisp/ChangeLog | 12 +++++++++--- lisp/emacs-lisp/seq.el | 28 +++++++++++++++++++++++----- test/ChangeLog | 6 ++++++ test/automated/seq-tests.el | 11 +++++++++-- 4 files changed, 47 insertions(+), 10 deletions(-) diff --git a/lisp/ChangeLog b/lisp/ChangeLog index ece253b1336..7e45b9db64c 100644 --- a/lisp/ChangeLog +++ b/lisp/ChangeLog @@ -1,7 +1,8 @@ -2015-02-09 Nicolas Petton +2015-02-11 Nicolas Petton - * emacs-lisp/seq.el (seq-group-by): Improves seq-group-by to - return sequence elements in correct order. + * emacs-lisp/seq.el (seq-reverse): Add a backward-compatible + version of seq-reverse that works on sequences in Emacs 24. + Bump seq.el version to 1.2. 2015-02-11 Martin Rudalics @@ -74,6 +75,11 @@ (python-shell-font-lock-turn-off): Fix typo. (python-util-text-properties-replace-name): Delete function. +2015-02-09 Nicolas Petton + + * emacs-lisp/seq.el (seq-group-by): Improves seq-group-by to + return sequence elements in correct order. + 2015-02-09 Simen Heggestøyl (tiny change) * textmodes/css-mode.el (css-smie-rules): Fix paren indent (bug#19815). diff --git a/lisp/emacs-lisp/seq.el b/lisp/emacs-lisp/seq.el index 5fbec185b76..ad4c3536b44 100644 --- a/lisp/emacs-lisp/seq.el +++ b/lisp/emacs-lisp/seq.el @@ -4,7 +4,7 @@ ;; Author: Nicolas Petton ;; Keywords: sequences -;; Version: 1.1.1 +;; Version: 1.2 ;; Maintainer: emacs-devel@gnu.org @@ -171,9 +171,7 @@ The result is a sequence of the same type as SEQ." (if (listp seq) (sort (seq-copy seq) pred) (let ((result (seq-sort pred (append seq nil)))) - (cond ((stringp seq) (concat result)) - ((vectorp seq) (vconcat result)) - (t (error "Unsupported sequence: %s" seq)))))) + (seq--into result (type-of seq))))) (defun seq-contains-p (seq elt &optional testfn) "Return the first element in SEQ that equals to ELT. @@ -256,6 +254,27 @@ keys. Keys are compared using `equal'." (seq-reverse seq) nil)) +(defalias 'seq-reverse + (if (ignore-errors (reverse [1 2])) + #'reverse + (lambda (seq) + "Return the reversed copy of list, vector, or string SEQ. +See also the function `nreverse', which is used more often." + (let ((result '())) + (seq-map (lambda (elt) (push elt result)) + seq) + (if (listp seq) + result + (seq--into result (type-of seq))))))) + +(defun seq--into (seq type) + "Convert the sequence SEQ into a sequence of type TYPE." + (pcase type + (`vector (vconcat seq)) + (`string (concat seq)) + (`list (append seq nil)) + (t (error "Not a sequence type name: %s" type)))) + (defun seq--drop-list (list n) "Return a list from LIST without its first N elements. This is an optimization for lists in `seq-drop'." @@ -299,7 +318,6 @@ This is an optimization for lists in `seq-take-while'." (defalias 'seq-copy #'copy-sequence) (defalias 'seq-elt #'elt) -(defalias 'seq-reverse #'reverse) (defalias 'seq-length #'length) (defalias 'seq-do #'mapc) (defalias 'seq-each #'seq-do) diff --git a/test/ChangeLog b/test/ChangeLog index b080961f681..979214c45da 100644 --- a/test/ChangeLog +++ b/test/ChangeLog @@ -1,3 +1,9 @@ +2015-02-11 Nicolas Petton + + * automated/seq-tests.el (test-seq-reverse, test-seq-group-by): + Add a test for seq-reverse and update test for seq-group-by to + test vectors and strings, not only lists. + 2015-02-10 Glenn Morris * automated/package-test.el (package-test-signed): diff --git a/test/automated/seq-tests.el b/test/automated/seq-tests.el index b92a15cacc5..badb3267f43 100644 --- a/test/automated/seq-tests.el +++ b/test/automated/seq-tests.el @@ -216,10 +216,17 @@ Evaluate BODY for each created sequence. (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 1 3) (nil 2 4)))) + (with-test-sequences (seq '(1 2 3 4)) + (should (equal (seq-group-by #'test-sequences-oddp seq) + '((t 1 3) (nil 2 4))))) (should (equal (seq-group-by #'car '((a 1) (b 3) (c 4) (a 2))) '((b (b 3)) (c (c 4)) (a (a 1) (a 2)))))) +(ert-deftest test-seq-reverse () + (with-test-sequences (seq '(1 2 3 4)) + (should (same-contents-p (seq-reverse seq) '(4 3 2 1))) + (should (equal (type-of (seq-reverse seq)) + (type-of seq))))) + (provide 'seq-tests) ;;; seq-tests.el ends here -- 2.39.2