diff options
author | Nicolas Petton <nicolas@petton.fr> | 2015-02-11 09:21:03 +0100 |
---|---|---|
committer | Nicolas Petton <nicolas@petton.fr> | 2015-02-11 14:48:18 +0100 |
commit | 4fb5565d0a0cd9640a242028c92b8b4e2bd4683e (patch) | |
tree | 67f5e8b5595d3b92d3a1f3c2b6b8a26d107d7501 /lisp/emacs-lisp/seq.el | |
parent | c49e769d8f141b0307db19ed2a5fa80e0696b1dc (diff) | |
download | emacs-4fb5565d0a0cd9640a242028c92b8b4e2bd4683e.tar.gz emacs-4fb5565d0a0cd9640a242028c92b8b4e2bd4683e.tar.bz2 emacs-4fb5565d0a0cd9640a242028c92b8b4e2bd4683e.zip |
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.
Diffstat (limited to 'lisp/emacs-lisp/seq.el')
-rw-r--r-- | lisp/emacs-lisp/seq.el | 28 |
1 files changed, 23 insertions, 5 deletions
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 <nicolas@petton.fr> ;; 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) |