From fb2fdb1435d2520c1cbf2a3d6a53128512a38458 Mon Sep 17 00:00:00 2001 From: Nicolas Petton Date: Fri, 16 Dec 2016 11:18:04 +0100 Subject: [PATCH] Make seq-into return the sequence when no conversion needed * lisp/emacs-lisp/seq.el (seq-into): Do not convert the sequence when no conversion is needed. * test/lisp/emacs-lisp/seq-tests.el (test-seq-into-and-identity): Add a regression test checking for identity. --- lisp/emacs-lisp/seq.el | 28 ++++++++++++++++++++++------ test/lisp/emacs-lisp/seq-tests.el | 8 ++++++++ 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/lisp/emacs-lisp/seq.el b/lisp/emacs-lisp/seq.el index 9890e60614e..74510244be7 100644 --- a/lisp/emacs-lisp/seq.el +++ b/lisp/emacs-lisp/seq.el @@ -179,9 +179,7 @@ Return a list of the results. \(fn FUNCTION SEQUENCES...)" (let ((result nil) (sequences (seq-map (lambda (s) - (if (listp s) - s - (seq-into s 'list))) + (seq-into s 'list)) (cons sequence sequences)))) (while (not (memq nil sequences)) (push (apply function (seq-map #'car sequences)) result) @@ -275,9 +273,9 @@ of sequence." TYPE can be one of the following symbols: vector, string or list." (pcase type - (`vector (vconcat sequence)) - (`string (concat sequence)) - (`list (append sequence nil)) + (`vector (seq--into-vector sequence)) + (`string (seq--into-string sequence)) + (`list (seq--into-list sequence)) (_ (error "Not a sequence type name: %S" type)))) (cl-defgeneric seq-filter (pred sequence) @@ -514,6 +512,24 @@ Signal an error if SEQUENCE is empty." (null list)) +(defun seq--into-list (sequence) + "Concatenate the elements of SEQUENCE into a list." + (if (listp sequence) + sequence + (append sequence nil))) + +(defun seq--into-vector (sequence) + "Concatenate the elements of SEQUENCE into a vector." + (if (vectorp sequence) + sequence + (vconcat sequence))) + +(defun seq--into-string (sequence) + "Concatenate the elements of SEQUENCE into a string." + (if (stringp sequence) + sequence + (concat sequence))) + (defun seq--activate-font-lock-keywords () "Activate font-lock keywords for some symbols defined in seq." (font-lock-add-keywords 'emacs-lisp-mode diff --git a/test/lisp/emacs-lisp/seq-tests.el b/test/lisp/emacs-lisp/seq-tests.el index fc65c9834d6..a7a43471de3 100644 --- a/test/lisp/emacs-lisp/seq-tests.el +++ b/test/lisp/emacs-lisp/seq-tests.el @@ -391,5 +391,13 @@ Evaluate BODY for each created sequence. (should (equal (seq-mapn #'+ '(3 4 5 7) l1) '(4 5 6 8))))) +(ert-deftest test-seq-into-and-identity () + (let ((lst '(1 2 3)) + (vec [1 2 3]) + (str "foo bar")) + (should (eq (seq-into lst 'list) lst)) + (should (eq (seq-into vec 'vector) vec)) + (should (eq (seq-into str 'string) str)))) + (provide 'seq-tests) ;;; seq-tests.el ends here -- 2.39.2