LATEX-L Archives

Mailing list for the LaTeX3 project

LATEX-L@LISTSERV.UNI-HEIDELBERG.DE

Options: Use Classic View

Use Monospaced Font
Show Text Part by Default
Show All Mail Headers

Topic: [<< First] [< Prev] [Next >] [Last >>]

Print Reply
Will Robertson <[log in to unmask]>
Sun, 23 Jan 2011 19:42:51 +1030
text/plain (106 lines)
Begin forwarded message:

> Subject: [l3svn] r2135 - add \clist_length:N and \clist_nth:N to l3candidates
> Author: will
> Date: 2011-01-23 10:06:37 +0100 (Sun, 23 Jan 2011)
> New Revision: 2135
> 
> Modified:
>   trunk/l3in2e/l3candidates.dtx
> Log:
> add \clist_length:N and \clist_nth:N to l3candidates


From a question on tex.se (http://tex.stackexchange.com/q/8626/179), I finally got around to writing \clist_length:N and \clist_nth:Nn functions for extracting the length of a comma list, and extracting the n-th element of the same. Both are expandable. (Code attached below.)

Questions: names? I think "length" is a good one although it doesn't match \tl_elt_count:N which is the equivalent for arbitrary token lists.

I didn't write "clist_nth" with a view of it being the permanent name, but now that I've written it I can't think of a (good) alternative. Any thoughts?

I assume that other packages dealing with comma-lists (such as etoolbox, etc.) contain similar functions. Is there functionality in l3clist that we're currently missing?

Best wishes,
Will


> 
> Modified: trunk/l3in2e/l3candidates.dtx
> ===================================================================
> --- trunk/l3in2e/l3candidates.dtx	2011-01-23 09:06:32 UTC (rev 2134)
> +++ trunk/l3in2e/l3candidates.dtx	2011-01-23 09:06:37 UTC (rev 2135)
> @@ -192,6 +192,22 @@
> %
> %
> %
> +% \begin{function}{\clist_length:N / (EXP)}
> +% \begin{syntax}
> +% "\clist_length:N" <clist>
> +% \end{syntax}
> +% Expands to the number of elements in the <clist>.
> +% \end{function}
> +%
> +% \begin{function}{\clist_nth:Nn / (EXP)}
> +% \begin{syntax}
> +% \clist_nth:Nn <clist> \Arg{n}
> +% \end{syntax}
> +% Expands to the <n>-th element of the <clist>.
> +% \end{function}
> +%
> +%
> +%
> % \end{documentation}
> %
> % \begin{implementation}
> @@ -373,7 +389,48 @@
> % \end{macro}
> %
> %
> +%
> +% \begin{macro}{\clist_length:N}
> %    \begin{macrocode}
> +\cs_new:Npn \clist_length:N #1 {
> +  \int_eval:n {
> +    0 \clist_map_function:NN #1 \tl_elt_count_aux:n
> +  }
> +}
> +%    \end{macrocode}
> +% \end{macro}
> +%
> +%
> +% \begin{macro}{\clist_nth:Nn}
> +%    \begin{macrocode}
> +\cs_new:Npn \clist_nth:Nn #1#2 {
> +  \int_compare:nTF { \clist_length:N #1 < #2 }
> +  { \ERROR }
> +  {
> +    \exp_after:wN \clist_nth_aux:nn \exp_after:wN {#1} #2
> +  }
> +}
> +\cs_new:Npn \clist_nth_aux:nn #1#2 {
> +  \clist_nth_aux_i:nnnw {1}{#2} #1 , \q_recursion_tail \q_recursion_stop
> +}
> +\cs_new:Npn \clist_nth_aux_i:nnnw #1#2#3, {
> +  \quark_if_recursion_tail_stop:n {#3}
> +  \int_compare:nTF {#1==#2}
> +  {
> +    \use_i_delimit_by_q_recursion_stop:nw {#3}
> +  }
> +  {
> +    \clist_nth_aux_i:fnnw { \int_eval:n {#1+1} } {#2}
> +  }
> +}
> +\cs_generate_variant:Nn \clist_nth_aux_i:nnnw {f}
> +%    \end{macrocode}
> +% \end{macro}
> +%
> +%
> +%
> +%
> +%    \begin{macrocode}
> %</initex|package>
> %    \end{macrocode}
> %
> 

-- Will

ATOM RSS1 RSS2