]> code.delx.au - gnu-emacs-elpa/blobdiff - packages/seq/seq.el
Update seq.el to version 1.2
[gnu-emacs-elpa] / packages / seq / seq.el
index bc0344d6b1bfa54a8f70c1b8f04eeef1acda9ec6..5366fd2e01138bf3cb535c24778733eece69ca79 100644 (file)
@@ -4,7 +4,7 @@
 
 ;; Author: Nicolas Petton <nicolas@petton.fr>
 ;; Keywords: sequences
-;; Version: 1.1.1
+;; Version: 1.2
 ;; Package: seq
 
 ;; Maintainer: emacs-devel@gnu.org
@@ -172,9 +172,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.
@@ -257,6 +255,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'."
@@ -300,7 +319,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)