Elisp: Join or Convert List, Vector
Here are functions to join sequences (list, vector, string) or convert them to any of list, vector, string.
Join Sequences to Vector
vconcat
-
(vconcat sequence1 sequence2 etc)
Join any Sequence Type and return a vector.
;; join vectors (vconcat [3 4] ["a" "b"]) ;; [3 4 "a" "b"] ;; join vector and list (vconcat [3 4] '("a" "b")) ;; [3 4 "a" "b"] ;; join vector and string (vconcat [3 4] "ab") ;; [3 4 97 98] ;; string elements are converted to char. ;; 97 is the codepoint for the char a
Join Sequences to List
append
-
(append sequence1 sequence2 etc)
Join any Sequence Type and return a list.
🛑 WARNING: if you want result to be Proper List , the last element must be a list, or nil.
join lists:
;; join lists (append (list 1 2) (list 3 4)) ;; (1 2 3 4)
convert vector to list:
;; convert vector to list (append [1 2 3] nil) ;; (1 2 3) ;; this creates improper list (append [1 2 3] [4 5]) ;; (1 2 3 . [4 5]) ;; this creates proper list (append [1 2 3] [4 5] nil) ;; (1 2 3 4 5) ;; join vectors and lists to list (append [1 2 3] [4 5] '(6)) ;; (1 2 3 4 5 6) ;; proper list
Sequence to String
To convert a sequence to string, use
mapconcat
or
format
.
[see Elisp: Format String]
mapconcat
-
(mapconcat function sequence separator)
Apply function to each element, and concat the results as string, with separator between elements.
;; list of numbers to string (mapconcat 'number-to-string '(1 2 3) ",") ;; "1,2,3"
;; list to string (mapconcat 'identity '("a" "b" "c") ",") ;; "a,b,c"
;; vector to string (mapconcat 'number-to-string [1 2 3] ",") ;; "1,2,3"
Join Sequences, to List or Vector
seq-concatenate
-
(seq-concatenate TYPE SEQUENCE...)
join sequences, into a list or vector.
;; to list (seq-concatenate 'list (list 1 2 3) (vector 4 5 6)) ;; (1 2 3 4 5 6) ;; to vector (seq-concatenate 'vector (list 1 2 3) (vector 4 5 6)) ;; [1 2 3 4 5 6] ;; to string (seq-concatenate 'string (list 97 98) (vector 99 100)) ;; "abcd" ;; integer is consider a char id ;; 97 is char a
Convert single sequence to list or vector
seq-into
-
(seq-into SEQUENCE TYPE)
convert sequence to a specific TYPE.
TYPE is one of:
'vector
'list
'string
(seq-into (list 3 4) 'vector) ;; [3 4] (seq-into (vector 3 4) 'list) ;; (3 4) (seq-into "abc" 'list) ;; (97 98 99) ;; string elements are converted to char. ;; 97 is the codepoint for the char a (seq-into "abc" 'vector) ;; [97 98 99] (seq-into (vector 97 98 99) 'string) ;; "abc"
;; nested item is not converted (seq-into (list 3 4 (list 3 4)) 'vector) ;; [3 4 (3 4)] (seq-into (vector 3 4 (vector 5 6)) 'list) ;; (3 4 [5 6])