Function: mapconcat
mapconcat is a function defined in fns.c.
Signature
(mapconcat FUNCTION SEQUENCE SEPARATOR)
Documentation
Apply FUNCTION to each element of SEQUENCE, and concat the results as strings.
In between each pair of results, stick in SEPARATOR. Thus, " " as
SEPARATOR results in spaces between the values returned by FUNCTION.
SEQUENCE may be a list, a vector, a bool-vector, or a string.
SEPARATOR must be a string, a vector, or a list of characters.
FUNCTION must be a function of one argument, and must return a value
that is a sequence of characters: either a string, or a vector or
list of numbers that are valid character codepoints.
Other relevant functions are documented in the list and string groups.
Probably introduced at or before Emacs version 21.1.
Shortdoc
;; string
(mapconcat (lambda (a) (concat "[" a "]")) '("foo" "bar" "zot") " ")
=> "[foo] [bar] [zot]"
;; list
(mapconcat #'identity '("foo" "bar") "|")
=> "foo|bar"
Source Code
// Defined in /usr/src/emacs/src/fns.c
{
USE_SAFE_ALLOCA;
EMACS_INT leni = XFIXNAT (Flength (sequence));
if (CHAR_TABLE_P (sequence))
wrong_type_argument (Qlistp, sequence);
EMACS_INT args_alloc = 2 * leni - 1;
if (args_alloc < 0)
return empty_unibyte_string;
Lisp_Object *args;
SAFE_ALLOCA_LISP (args, args_alloc);
ptrdiff_t nmapped = mapcar1 (leni, args, function, sequence);
ptrdiff_t nargs = 2 * nmapped - 1;
for (ptrdiff_t i = nmapped - 1; i > 0; i--)
args[i + i] = args[i];
for (ptrdiff_t i = 1; i < nargs; i += 2)
args[i] = separator;
Lisp_Object ret = Fconcat (nargs, args);
SAFE_FREE ();
return ret;
}