# Process first or last case differently with clist_map

I wrote this macro to transform a comma-separated list of objects into a custom-separated list of objects:

\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\separators}{m}{%
\clist_map_inline:nn{#1}{{##1}\ **\ }%
}
\ExplSyntaxOff


But \separators{a, b, c} obviously yields

a ** b ** c **


a ** b ** c


How do I prevent the last item of my list to be processed the same way as the others?

• I'm sure somebody will come up with a proper solution, but for now note that 'last element in a list' is not a stopping rule/time, but 'first element in a list' is. You can also solve your problem by treating the first element differently, which may be easier, because you don't need to know the length of the list in advance (you could simply set a flag for the first item). – moewe May 17 '18 at 14:02
• @moewe Of course! I find the "last" view easier to grasp in this case with separators, but it ultimately is the same problem with "first", which is why I've written both in the OP title ;) – iago-lito May 17 '18 at 14:06

The map function is intended to deal with all entries equally. Here, you are actually using a clist (inserting it into the output)

\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand \separators { m }
{
\clist_set:Nn \l_tmpa_clist {#1}
\clist_use:Nn \l_tmpa_clist { ~**~ }
}
\ExplSyntaxOff
\begin{document}

\separators{a,b,c}

\end{document}


See also \clist_use:Nnnn if you need different behaviour at the end of a longer list, and when there are exactly two entries.

• Great! Cheers :) And these other clist variant seem quite versatile. – iago-lito May 17 '18 at 14:08