Debian Codegraph @

texmf-dist/source/latex/l3kernel/l3seq.dtx

raw
1 % \iffalse meta-comment
2 %
3 %% File: l3seq.dtx
4 %
5 % Copyright (C) 1990-2026 The LaTeX Project
6 %
7 % It may be distributed and/or modified under the conditions of the
8 % LaTeX Project Public License (LPPL), either version 1.3c of this
9 % license or (at your option) any later version. The latest version
10 % of this license is in the file
11 %
12 % https://www.latex-project.org/lppl.txt
13 %
14 % This file is part of the "l3kernel bundle" (The Work in LPPL)
15 % and all files in that bundle must be distributed together.
16 %
17 % -----------------------------------------------------------------------
18 %
19 % The development version of the bundle can be found at
20 %
21 % https://github.com/latex3/latex3
22 %
23 % for those people who are interested.
24 %
25 %<*driver>
26 \documentclass[full,kernel]{l3doc}
27 \begin{document}
28 \DocInput{\jobname.dtx}
29 \end{document}
30 %</driver>
31 % \fi
32 %
33 % \title{^^A
34 % The \pkg{l3seq} module\\ Sequences and stacks^^A
35 % }
36 %
37 % \author{^^A
38 % The \LaTeX{} Project\thanks
39 % {^^A
40 % E-mail:
41 % \href{mailto:[email protected]}
42 % {[email protected]}^^A
43 % }^^A
44 % }
45 %
46 % \date{Released 2026-05-15}
47 %
48 % \maketitle
49 %
50 % \begin{documentation}
51 %
52 % \LaTeX3 implements a \enquote{sequence} data type, which contain
53 % an ordered list of entries which may contain any \meta{balanced text}.
54 % It is possible to map functions to sequences such that the function
55 % is applied to every item in the sequence.
56 %
57 % Sequences are also used to implement stack functions in \LaTeX3. This
58 % is achieved using a number of dedicated stack functions.
59 %
60 % \section{Creating and initializing sequences}
61 %
62 % \begin{function}{\seq_new:N, \seq_new:c}
63 % \begin{syntax}
64 % \cs{seq_new:N} \meta{seq~var}
65 % \end{syntax}
66 % Creates a new \meta{seq~var} or raises an error if the name is
67 % already taken. The declaration is global. The \meta{seq~var}
68 % initially contains no items.
69 % \end{function}
70 %
71 % \begin{function}{\seq_clear:N, \seq_clear:c, \seq_gclear:N, \seq_gclear:c}
72 % \begin{syntax}
73 % \cs{seq_clear:N} \meta{seq~var}
74 % \end{syntax}
75 % Clears all items from the \meta{seq~var}.
76 % \end{function}
77 %
78 % \begin{function}
79 % {\seq_clear_new:N, \seq_clear_new:c, \seq_gclear_new:N, \seq_gclear_new:c}
80 % \begin{syntax}
81 % \cs{seq_clear_new:N} \meta{seq~var}
82 % \end{syntax}
83 % Ensures that the \meta{seq~var} exists globally by applying
84 % \cs{seq_new:N} if necessary, then applies
85 % \cs[index=seq_clear:N]{seq_(g)clear:N} to leave
86 % the \meta{seq~var} empty.
87 % \end{function}
88 %
89 % \begin{function}
90 % {
91 % \seq_set_eq:NN, \seq_set_eq:cN, \seq_set_eq:Nc, \seq_set_eq:cc,
92 % \seq_gset_eq:NN, \seq_gset_eq:cN, \seq_gset_eq:Nc, \seq_gset_eq:cc
93 % }
94 % \begin{syntax}
95 % \cs{seq_set_eq:NN} \meta{seq~var_1} \meta{seq~var_2}
96 % \end{syntax}
97 % Sets the content of \meta{seq~var_1} equal to that of
98 % \meta{seq~var_2}.
99 % \end{function}
100 %
101 % \begin{function}[added = 2014-07-17]
102 % {
103 % \seq_set_from_clist:NN, \seq_set_from_clist:cN,
104 % \seq_set_from_clist:Nc, \seq_set_from_clist:cc,
105 % \seq_gset_from_clist:NN, \seq_gset_from_clist:cN,
106 % \seq_gset_from_clist:Nc, \seq_gset_from_clist:cc,
107 % }
108 % \begin{syntax}
109 % \cs{seq_set_from_clist:NN} \meta{seq~var} \meta{clist~var}
110 % \end{syntax}
111 % Converts the data in the \meta{clist~var} into a \meta{seq~var}:
112 % the original \meta{clist~var} is unchanged.
113 % \end{function}
114 %
115 % \begin{function}[added = 2014-07-17]
116 % {
117 % \seq_set_from_clist:Nn, \seq_set_from_clist:cn
118 % \seq_gset_from_clist:Nn, \seq_gset_from_clist:cn
119 % }
120 % \begin{syntax}
121 % \cs{seq_from_clist:Nn} \meta{seq~var} \Arg{comma-list}
122 % \end{syntax}
123 % The \meta{seq~var} is set to contain the items in the \meta{comma list}.
124 % \end{function}
125 %
126 % \begin{function}[added = 2017-11-28]
127 % {\seq_const_from_clist:Nn, \seq_const_from_clist:cn}
128 % \begin{syntax}
129 % \cs{seq_const_from_clist:Nn} \meta{seq~var} \Arg{comma-list}
130 % \end{syntax}
131 % Creates a new constant \meta{seq~var} or raises an error if the name
132 % is already taken. The \meta{seq~var} is set globally to contain the
133 % items in the \meta{comma list}.
134 % \end{function}
135 %
136 % \begin{function}[added = 2011-08-15, updated = 2012-07-02]
137 % {
138 % \seq_set_split:Nnn ,
139 % \seq_set_split:NVn , \seq_set_split:NnV , \seq_set_split:NVV ,
140 % \seq_set_split:Nne , \seq_set_split:Nee ,
141 % \seq_gset_split:Nnn,
142 % \seq_gset_split:NVn , \seq_gset_split:NnV, \seq_gset_split:NVV,
143 % \seq_gset_split:Nne , \seq_gset_split:Nee
144 % }
145 % \begin{syntax}
146 % \cs{seq_set_split:Nnn} \meta{seq~var} \Arg{delimiter} \Arg{token list}
147 % \end{syntax}
148 % Splits the \meta{token list} into \meta{items} separated
149 % by \meta{delimiter}, and assigns the result to the \meta{seq~var}.
150 % Spaces on both sides of each \meta{item} are ignored,
151 % then one set of outer braces is removed (if any);
152 % this space trimming behavior is identical to that of
153 % \pkg{l3clist} functions. Empty \meta{items} are preserved by
154 % \cs{seq_set_split:Nnn}, and can be removed afterwards using
155 % \cs{seq_remove_all:Nn} \meta{seq~var} |{}|.
156 % The \meta{delimiter} may not contain |{|, |}| or |#|
157 % (assuming \TeX{}'s normal category code régime).
158 % If the \meta{delimiter} is empty, the \meta{token list} is split
159 % into \meta{items} as described for \cs{tl_map_function:nN}.
160 % See also \cs{seq_set_split_keep_spaces:Nnn}, which omits space stripping.
161 % \end{function}
162 %
163 % \begin{function}[added = 2021-03-24]
164 % {
165 % \seq_set_split_keep_spaces:Nnn , \seq_set_split_keep_spaces:NnV ,
166 % \seq_gset_split_keep_spaces:Nnn, \seq_gset_split_keep_spaces:NnV
167 % }
168 % \begin{syntax}
169 % \cs{seq_set_split_keep_spaces:Nnn} \meta{seq~var} \Arg{delimiter} \Arg{token list}
170 % \end{syntax}
171 % Splits the \meta{token list} into \meta{items} separated
172 % by \meta{delimiter}, and assigns the result to the \meta{seq~var}.
173 % One set of outer braces is removed (if any) but any surrounding spaces
174 % are retained: any braces \emph{inside} one or more spaces are
175 % therefore kept. Empty \meta{items} are preserved by
176 % \cs{seq_set_split_keep_spaces:Nnn}, and can be removed afterwards using
177 % \cs{seq_remove_all:Nn} \meta{seq~var} |{}|.
178 % The \meta{delimiter} may not contain |{|, |}| or |#|
179 % (assuming \TeX{}'s normal category code régime).
180 % If the \meta{delimiter} is empty, the \meta{token list} is split
181 % into \meta{items} as described for \cs{tl_map_function:nN}; note in this
182 % case spaces will \emph{not} be preserved.
183 % See also \cs{seq_set_split:Nnn}, which removes spaces around the delimiters.
184 % \end{function}
185 %
186 % \begin{function}[added = 2012-06-15]
187 % {\seq_set_filter:NNn, \seq_gset_filter:NNn}
188 % \begin{syntax}
189 % \cs{seq_set_filter:NNn} \meta{seq~var_1} \meta{seq~var_2} \Arg{inline boolexpr}
190 % \end{syntax}
191 % Evaluates the \meta{inline boolexpr} for every \meta{item} stored
192 % within the \meta{seq~var_2}. The \meta{inline boolexpr}
193 % receives the \meta{item} as |#1|. The sequence of all \meta{items}
194 % for which the \meta{inline boolexpr} evaluated to \texttt{true}
195 % is assigned to \meta{seq~var_1}.
196 % \begin{texnote}
197 % Contrarily to other mapping functions, \cs{seq_map_break:} cannot
198 % be used in this function, and would lead to low-level \TeX{} errors.
199 % \end{texnote}
200 % \end{function}
201 %
202 % \begin{function}[added = 2024-12-08]
203 % {
204 % \seq_set_regex_extract_once:Nnn, \seq_set_regex_extract_once:cnn,
205 % \seq_set_regex_extract_once:NNn, \seq_set_regex_extract_once:cNn,
206 % \seq_gset_regex_extract_once:Nnn, \seq_gset_regex_extract_once:cnn,
207 % \seq_gset_regex_extract_once:NNn, \seq_gset_regex_extract_once:cNn,
208 % }
209 % \begin{syntax}
210 % \cs{seq_set_regex_extract_once:Nnn} \meta{seq~var} \Arg{regex} \Arg{token list}
211 % \cs{seq_set_regex_extract_once:NNn} \meta{seq~var} \meta{regex~var} \Arg{token list}
212 % \end{syntax}
213 % Finds the first match of the \meta{regex} in the
214 % \meta{token list}. If it exists, the match is stored as the first
215 % item of the \meta{seq~var}, and further items are the contents of
216 % capturing groups, in the order of their opening parenthesis.
217 % If there is no match, the \meta{seq~var} is cleared.
218 % Theses are alternative names for \cs{regex_extract_once:nnN} and friends,
219 % with arguments re-ordered for \meta{seq~var} setting;
220 % see \pkg{l3regex} chapter for more details of the \meta{regex}
221 % format.
222 % \end{function}
223 %
224 % \begin{function}[added = 2024-12-08]
225 % {
226 % \seq_set_regex_extract_all:Nnn, \seq_set_regex_extract_all:cnn,
227 % \seq_set_regex_extract_all:NNn, \seq_set_regex_extract_all:cNn,
228 % \seq_gset_regex_extract_all:Nnn, \seq_gset_regex_extract_all:cnn,
229 % \seq_gset_regex_extract_all:NNn, \seq_gset_regex_extract_all:cNn,
230 % }
231 % \begin{syntax}
232 % \cs{seq_set_regex_extract_all:Nnn} \meta{seq~var} \Arg{regex} \Arg{token list}
233 % \cs{seq_set_regex_extract_all:NNn} \meta{seq~var} \meta{regex~var} \Arg{token list}
234 % \end{syntax}
235 % Finds all matches of the \meta{regex} in the
236 % \meta{token list}, and stores all the submatch information
237 % in a single sequence (concatenating the results of
238 % multiple \cs{seq_set_regex_extract_all:Nnn} calls).
239 % If there is no match, the \meta{seq~var} is cleared.
240 % Theses are alternative names for \cs{regex_extract_all:nnN} and friends,
241 % with arguments re-ordered for \meta{seq~var} setting;
242 % see \pkg{l3regex} chapter for more details of the \meta{regex}
243 % format.
244 % \end{function}
245 %
246 % \begin{function}[added = 2024-12-08]
247 % {
248 % \seq_set_regex_split:Nnn, \seq_set_regex_split:cnn,
249 % \seq_set_regex_split:NNn, \seq_set_regex_split:cNn,
250 % \seq_gset_regex_split:Nnn, \seq_gset_regex_split:cnn,
251 % \seq_gset_regex_split:NNn, \seq_gset_regex_split:cNn,
252 % }
253 % \begin{syntax}
254 % \cs{seq_set_regex_split:Nnn} \meta{seq~var} \Arg{regex} \Arg{token list}
255 % \cs{seq_set_regex_split:NNn} \meta{seq~var} \meta{regex~var} \Arg{token list}
256 % \end{syntax}
257 % Splits the \meta{token list} into a sequence of parts, delimited by
258 % matches of the \meta{regular expression}. If the \meta{regular expression}
259 % has capturing groups, then the token lists that they match are stored as
260 % items of the sequence as well.
261 % If no match is found the resulting \meta{seq~var} has the
262 % \meta{token list} as its sole item. If the \meta{regular expression}
263 % matches the empty token list, then the \meta{token list} is split
264 % into single tokens.
265 % For example, after
266 % \begin{verbatim}
267 % \seq_set_regex_split:Nnn \l_path_seq { / } { the/path/for/this/file.tex }
268 % \end{verbatim}
269 % the sequence |\l_path_seq| contains the items |{the}|, |{path}|,
270 % |{for}|, |{this}|, and |{file.tex}|.
271 % Theses are alternative names for \cs{regex_split:nnN} and friends,
272 % with arguments re-ordered for \meta{seq~var} setting;
273 % see \pkg{l3regex} chapter for more details of the \meta{regex}
274 % format.
275 % \end{function}
276 %
277 % \begin{function}
278 % {\seq_concat:NNN, \seq_concat:ccc, \seq_gconcat:NNN, \seq_gconcat:ccc}
279 % \begin{syntax}
280 % \cs{seq_concat:NNN} \meta{seq~var_1} \meta{seq~var_2} \meta{seq~var_3}
281 % \end{syntax}
282 % Concatenates the content of \meta{seq~var_2} and \meta{seq~var_3}
283 % together and saves the result in \meta{seq~var_1}. The items in
284 % \meta{seq~var_2} are placed at the left side of the new sequence.
285 % \end{function}
286 %
287 % \begin{function}[EXP, pTF, added=2012-03-03]
288 % {\seq_if_exist:N, \seq_if_exist:c}
289 % \begin{syntax}
290 % \cs{seq_if_exist_p:N} \meta{seq~var}
291 % \cs{seq_if_exist:NTF} \meta{seq~var} \Arg{true code} \Arg{false code}
292 % \end{syntax}
293 % Tests whether the \meta{seq~var} is currently defined. This does not
294 % check that the \meta{seq~var} really is a sequence variable.
295 % \end{function}
296 %
297 % \section{Appending data to sequences}
298 %
299 % \begin{function}{
300 % \seq_put_left:Nn, \seq_put_left:NV, \seq_put_left:Nv, \seq_put_left:Ne,
301 % \seq_put_left:No,
302 % \seq_put_left:cn, \seq_put_left:cV, \seq_put_left:cv, \seq_put_left:ce,
303 % \seq_put_left:co,
304 % \seq_gput_left:Nn, \seq_gput_left:NV, \seq_gput_left:Nv, \seq_gput_left:Ne,
305 % \seq_gput_left:No,
306 % \seq_gput_left:cn, \seq_gput_left:cV, \seq_gput_left:cv, \seq_gput_left:ce,
307 % \seq_gput_left:co
308 % }
309 % \begin{syntax}
310 % \cs{seq_put_left:Nn} \meta{seq~var} \Arg{item}
311 % \end{syntax}
312 % Appends the \meta{item} to the left of the \meta{seq~var}.
313 % \end{function}
314 %
315 % \begin{function}{
316 % \seq_put_right:Nn, \seq_put_right:NV, \seq_put_right:Nv, \seq_put_right:Ne,
317 % \seq_put_right:No,
318 % \seq_put_right:cn, \seq_put_right:cV, \seq_put_right:cv, \seq_put_right:ce,
319 % \seq_put_right:co,
320 % \seq_gput_right:Nn, \seq_gput_right:NV, \seq_gput_right:Nv, \seq_gput_right:Ne,
321 % \seq_gput_right:No,
322 % \seq_gput_right:cn, \seq_gput_right:cV, \seq_gput_right:cv, \seq_gput_right:ce,
323 % \seq_gput_right:co,
324 % }
325 % \begin{syntax}
326 % \cs{seq_put_right:Nn} \meta{seq~var} \Arg{item}
327 % \end{syntax}
328 % Appends the \meta{item} to the right of the \meta{seq~var}.
329 % \end{function}
330 %
331 % \section{Recovering items from sequences}
332 %
333 % Items can be recovered from either the left or the right of sequences.
334 % For implementation reasons, the actions at the left of the sequence are
335 % faster than those acting on the right. These functions all assign the
336 % recovered material locally, i.e., setting the
337 % \meta{tl~var} used with \cs{tl_set:Nn} and \emph{never}
338 % \cs{tl_gset:Nn}.
339 %
340 % \begin{function}[updated = 2012-05-14]{\seq_get_left:NN, \seq_get_left:cN}
341 % \begin{syntax}
342 % \cs{seq_get_left:NN} \meta{seq~var} \meta{tl~var}
343 % \end{syntax}
344 % Stores the left-most item from a \meta{seq~var} in the
345 % \meta{tl~var} without removing it from the
346 % \meta{seq~var}. The \meta{tl~var} is assigned locally.
347 % If \meta{seq~var} is empty the \meta{tl~var}
348 % is set to the special marker \cs{q_no_value}.
349 % \end{function}
350 %
351 % \begin{function}[updated = 2012-05-19]{\seq_get_right:NN, \seq_get_right:cN}
352 % \begin{syntax}
353 % \cs{seq_get_right:NN} \meta{seq~var} \meta{tl~var}
354 % \end{syntax}
355 % Stores the right-most item from a \meta{seq~var} in the
356 % \meta{tl~var} without removing it from the
357 % \meta{seq~var}. The \meta{tl~var} is assigned locally.
358 % If \meta{seq~var} is empty the \meta{tl~var}
359 % is set to the special marker \cs{q_no_value}.
360 % \end{function}
361 %
362 % \begin{function}[updated = 2012-05-14]{\seq_pop_left:NN, \seq_pop_left:cN}
363 % \begin{syntax}
364 % \cs{seq_pop_left:NN} \meta{seq~var} \meta{tl~var}
365 % \end{syntax}
366 % Pops the left-most item from a \meta{seq~var} into the
367 % \meta{tl~var}, i.e., removes the item from the
368 % sequence and stores it in the \meta{tl~var}.
369 % Both of the variables are assigned locally. If \meta{seq~var} is
370 % empty the \meta{tl~var} is set to
371 % the special marker \cs{q_no_value}.
372 % \end{function}
373 %
374 % \begin{function}[updated = 2012-05-14]{\seq_gpop_left:NN, \seq_gpop_left:cN}
375 % \begin{syntax}
376 % \cs{seq_gpop_left:NN} \meta{seq~var} \meta{tl~var}
377 % \end{syntax}
378 % Pops the left-most item from a \meta{seq~var} into the
379 % \meta{tl~var}, i.e., removes the item from the
380 % sequence and stores it in the \meta{tl~var}.
381 % The \meta{seq~var} is modified globally, while the assignment of
382 % the \meta{tl~var} is local.
383 % If \meta{seq~var} is empty the \meta{tl~var} is set to
384 % the special marker \cs{q_no_value}.
385 % \end{function}
386 %
387 % \begin{function}[updated = 2012-05-19]{\seq_pop_right:NN, \seq_pop_right:cN}
388 % \begin{syntax}
389 % \cs{seq_pop_right:NN} \meta{seq~var} \meta{tl~var}
390 % \end{syntax}
391 % Pops the right-most item from a \meta{seq~var} into the
392 % \meta{tl~var}, i.e., removes the item from the
393 % sequence and stores it in the \meta{tl~var}.
394 % Both of the variables are assigned locally. If \meta{seq~var} is
395 % empty the \meta{tl~var} is set to
396 % the special marker \cs{q_no_value}.
397 % \end{function}
398 %
399 % \begin{function}[updated = 2012-05-19]{\seq_gpop_right:NN, \seq_gpop_right:cN}
400 % \begin{syntax}
401 % \cs{seq_gpop_right:NN} \meta{seq~var} \meta{tl~var}
402 % \end{syntax}
403 % Pops the right-most item from a \meta{seq~var} into the
404 % \meta{tl~var}, i.e., removes the item from the
405 % sequence and stores it in the \meta{tl~var}.
406 % The \meta{seq~var} is modified globally, while the assignment of
407 % the \meta{tl~var} is local.
408 % If \meta{seq~var} is empty the \meta{tl~var} is set to
409 % the special marker \cs{q_no_value}.
410 % \end{function}
411 %
412 % \begin{function}[added = 2014-07-17, EXP]
413 % {
414 % \seq_item:Nn, \seq_item:NV, \seq_item:Ne,
415 % \seq_item:cn, \seq_item:cV, \seq_item:ce
416 % }
417 % \begin{syntax}
418 % \cs{seq_item:Nn} \meta{seq~var} \Arg{integer expression}
419 % \end{syntax}
420 % Indexing items in the \meta{seq~var} from~$1$ at the top (left), this
421 % function evaluates the \meta{integer expression} and leaves the
422 % appropriate item from the sequence in the input stream. If the
423 % \meta{integer expression} is negative, indexing occurs from the
424 % bottom (right) of the sequence. If the \meta{integer expression}
425 % is larger than the number of items in the \meta{seq~var} (as
426 % calculated by \cs{seq_count:N}) then the function expands to
427 % nothing.
428 % \begin{texnote}
429 % The result is returned within the \tn{unexpanded}
430 % primitive (\cs{exp_not:n}), which means that the \meta{item}
431 % does not expand further when appearing in an \texttt{e}-type
432 % or \texttt{x}-type argument expansion.
433 % \end{texnote}
434 % \end{function}
435 %
436 % \begin{function}[EXP, added = 2016-12-06]{\seq_rand_item:N, \seq_rand_item:c}
437 % \begin{syntax}
438 % \cs{seq_rand_item:N} \meta{seq~var}
439 % \end{syntax}
440 % Selects a pseudo-random item of the \meta{seq~var}. If the
441 % \meta{seq~var} is empty the result is empty.
442 % \begin{texnote}
443 % The result is returned within the \tn{unexpanded}
444 % primitive (\cs{exp_not:n}), which means that the \meta{item}
445 % does not expand further when appearing in an \texttt{e}-type
446 % or \texttt{x}-type argument expansion.
447 % \end{texnote}
448 % \end{function}
449 %
450 % \section{Recovering values from sequences with branching}
451 %
452 % The functions in this section combine tests for non-empty sequences
453 % with recovery of an item from the sequence. They offer increased readability
454 % and performance over separate testing and recovery phases.
455 %
456 % \begin{function}[TF, added = 2012-05-14, updated = 2012-05-19]
457 % {\seq_get_left:NN, \seq_get_left:cN}
458 % \begin{syntax}
459 % \cs{seq_get_left:NNTF} \meta{seq~var} \meta{tl~var} \Arg{true code} \Arg{false code}
460 % \end{syntax}
461 % If the \meta{seq~var} is empty, leaves the \meta{false code} in the
462 % input stream. The value of the \meta{tl~var} is
463 % not defined in this case and should not be relied upon. If the
464 % \meta{seq~var} is non-empty, stores the left-most item from the
465 % \meta{seq~var}
466 % in the \meta{tl~var} without removing it from the
467 % \meta{seq~var}, then leaves the \meta{true code} in the input stream.
468 % The \meta{tl~var} is assigned locally.
469 % \end{function}
470 %
471 % \begin{function}[TF, added = 2012-05-19]
472 % {\seq_get_right:NN, \seq_get_right:cN}
473 % \begin{syntax}
474 % \cs{seq_get_right:NNTF} \meta{seq~var} \meta{tl~var} \Arg{true code} \Arg{false code}
475 % \end{syntax}
476 % If the \meta{seq~var} is empty, leaves the \meta{false code} in the
477 % input stream. The value of the \meta{tl~var} is
478 % not defined in this case and should not be relied upon. If the
479 % \meta{seq~var} is non-empty, stores the right-most item from the
480 % \meta{seq~var}
481 % in the \meta{tl~var} without removing it from the
482 % \meta{seq~var}, then leaves the \meta{true code} in the input stream.
483 % The \meta{tl~var} is assigned locally.
484 % \end{function}
485 %
486 % \begin{function}[TF, added = 2012-05-14, updated = 2012-05-19]
487 % {\seq_pop_left:NN, \seq_pop_left:cN}
488 % \begin{syntax}
489 % \cs{seq_pop_left:NNTF} \meta{seq~var} \meta{tl~var} \Arg{true code} \Arg{false code}
490 % \end{syntax}
491 % If the \meta{seq~var} is empty, leaves the \meta{false code} in the
492 % input stream. The value of the \meta{tl~var} is
493 % not defined in this case and should not be relied upon. If the
494 % \meta{seq~var} is non-empty, pops the left-most item from the
495 % \meta{seq~var}
496 % in the \meta{tl~var}, i.e., removes the item from the
497 % \meta{seq~var}, then leaves the \meta{true code} in the input stream.
498 % Both the \meta{seq~var} and the \meta{tl~var} are assigned
499 % locally.
500 % \end{function}
501 %
502 % \begin{function}[TF, added = 2012-05-14, updated = 2012-05-19]
503 % {\seq_gpop_left:NN, \seq_gpop_left:cN}
504 % \begin{syntax}
505 % \cs{seq_gpop_left:NNTF} \meta{seq~var} \meta{tl~var} \Arg{true code} \Arg{false code}
506 % \end{syntax}
507 % If the \meta{seq~var} is empty, leaves the \meta{false code} in the
508 % input stream. The value of the \meta{tl~var} is
509 % not defined in this case and should not be relied upon. If the
510 % \meta{seq~var} is non-empty, pops the left-most item from the \meta{seq~var}
511 % in the \meta{tl~var}, i.e., removes the item from the
512 % \meta{seq~var}, then leaves the \meta{true code} in the input stream.
513 % The \meta{seq~var} is modified globally, while the \meta{tl~var}
514 % is assigned locally.
515 % \end{function}
516 %
517 % \begin{function}[TF, added = 2012-05-19]
518 % {\seq_pop_right:NN, \seq_pop_right:cN}
519 % \begin{syntax}
520 % \cs{seq_pop_right:NNTF} \meta{seq~var} \meta{tl~var} \Arg{true code} \Arg{false code}
521 % \end{syntax}
522 % If the \meta{seq~var} is empty, leaves the \meta{false code} in the
523 % input stream. The value of the \meta{tl~var} is
524 % not defined in this case and should not be relied upon. If the
525 % \meta{seq~var} is non-empty, pops the right-most item from the \meta{seq~var}
526 % in the \meta{tl~var}, i.e., removes the item from the
527 % \meta{seq~var}, then leaves the \meta{true code} in the input stream.
528 % Both the \meta{seq~var} and the \meta{tl~var} are assigned
529 % locally.
530 % \end{function}
531 %
532 % \begin{function}[TF, added = 2012-05-19]
533 % {\seq_gpop_right:NN, \seq_gpop_right:cN}
534 % \begin{syntax}
535 % \cs{seq_gpop_right:NNTF} \meta{seq~var} \meta{tl~var} \Arg{true code} \Arg{false code}
536 % \end{syntax}
537 % If the \meta{seq~var} is empty, leaves the \meta{false code} in the
538 % input stream. The value of the \meta{tl~var} is
539 % not defined in this case and should not be relied upon. If the
540 % \meta{seq~var} is non-empty, pops the right-most item from the \meta{seq~var}
541 % in the \meta{tl~var}, i.e., removes the item from the
542 % \meta{seq~var}, then leaves the \meta{true code} in the input stream.
543 % The \meta{seq~var} is modified globally, while the
544 % \meta{tl~var} is assigned locally.
545 % \end{function}
546 %
547 % \section{Modifying sequences}
548 %
549 % While sequences are normally used as ordered lists, it may be
550 % necessary to modify the content. The functions here may be used
551 % to update sequences, while retaining the order of the unaffected
552 % entries.
553 %
554 % \begin{function}
555 % {
556 % \seq_remove_duplicates:N, \seq_remove_duplicates:c,
557 % \seq_gremove_duplicates:N, \seq_gremove_duplicates:c
558 % }
559 % \begin{syntax}
560 % \cs{seq_remove_duplicates:N} \meta{seq~var}
561 % \end{syntax}
562 % Removes duplicate items from the \meta{seq~var}, leaving the
563 % left most copy of each item in the \meta{seq~var}. The \meta{item}
564 % comparison takes place on a token basis, as for \cs{tl_if_eq:nnTF}.
565 % \begin{texnote}
566 % This function iterates through every item in the \meta{seq~var} and
567 % does a comparison with the \meta{items} already checked. It is therefore
568 % relatively slow with large sequences.
569 % \end{texnote}
570 % \end{function}
571 %
572 % \begin{function}
573 % {
574 % \seq_remove_all:Nn, \seq_remove_all:NV, \seq_remove_all:Ne,
575 % \seq_remove_all:cn, \seq_remove_all:cV, \seq_remove_all:ce,
576 % \seq_gremove_all:Nn, \seq_gremove_all:NV, \seq_gremove_all:Ne,
577 % \seq_gremove_all:cn, \seq_gremove_all:cV, \seq_gremove_all:ce
578 % }
579 % \begin{syntax}
580 % \cs{seq_remove_all:Nn} \meta{seq~var} \Arg{item}
581 % \end{syntax}
582 % Removes every occurrence of \meta{item} from the \meta{seq~var}.
583 % The \meta{item} comparison takes place on a token basis, as for
584 % \cs{tl_if_eq:nnTF}.
585 % \end{function}
586 %
587 % \begin{function}[added = 2021-04-29, noTF]
588 % {\seq_set_item:Nnn, \seq_set_item:cnn, \seq_gset_item:Nnn, \seq_gset_item:cnn}
589 % \begin{syntax}
590 % \cs{seq_set_item:Nnn} \meta{seq~var} \Arg{int expr} \Arg{item}
591 % \cs{seq_set_item:NnnTF} \meta{seq~var} \Arg{int expr} \Arg{item} \Arg{true code} \Arg{false code}
592 % \end{syntax}
593 % Removes the item of \meta{seq~var} at the position given by
594 % evaluating the \meta{int expr} and replaces it by
595 % \meta{item}. Items are indexed from $1$ on the left/top of the
596 % \meta{seq~var}, or from $-1$ on the right/bottom. If the
597 % \meta{int expr} is zero or is larger (in absolute value)
598 % than the number of items in the sequence, the \meta{seq~var} is not
599 % modified. In these cases, \cs{seq_set_item:Nnn} raises an error
600 % while \cs{seq_set_item:NnnTF} runs the \meta{false code}. In cases
601 % where the assignment was successful, \meta{true code} is run
602 % afterwards.
603 % \end{function}
604 %
605 % \begin{function}[added = 2014-07-18]
606 % {
607 % \seq_reverse:N, \seq_reverse:c,
608 % \seq_greverse:N, \seq_greverse:c
609 % }
610 % \begin{syntax}
611 % \cs{seq_reverse:N} \meta{seq~var}
612 % \end{syntax}
613 % Reverses the order of the items stored in the \meta{seq~var}.
614 % \end{function}
615 %
616 % \begin{function}[added = 2017-02-06]
617 % {\seq_sort:Nn, \seq_sort:cn, \seq_gsort:Nn, \seq_gsort:cn}
618 % \begin{syntax}
619 % \cs{seq_sort:Nn} \meta{seq~var} \Arg{comparison code}
620 % \end{syntax}
621 % Sorts the items in the \meta{seq~var} according to the
622 % \meta{comparison code}, and assigns the result to
623 % \meta{seq~var}. The details of sorting comparison are
624 % described in Section~\ref{sec:l3sort:mech}.
625 % \end{function}
626 %
627 % \begin{function}[added = 2018-04-29]
628 % {\seq_shuffle:N, \seq_shuffle:c, \seq_gshuffle:N, \seq_gshuffle:c}
629 % \begin{syntax}
630 % \cs{seq_shuffle:N} \meta{seq~var}
631 % \end{syntax}
632 % Sets the \meta{seq~var} to the result of placing the items of the
633 % \meta{seq~var} in a random order. Each item is (roughly) as likely
634 % to end up in any given position.
635 % \begin{texnote}
636 % For sequences with more than $13$ items or so, only a small
637 % proportion of all possible permutations can be reached, because
638 % the random seed \cs{sys_rand_seed:} only has $28$-bits. The use
639 % of \tn{toks} internally means that sequences with more than
640 % $32767$ or $65535$ items (depending on the engine) cannot be
641 % shuffled.
642 % \end{texnote}
643 % \end{function}
644 %
645 % \section{Sequence conditionals}
646 %
647 % \begin{function}[EXP,pTF]{\seq_if_empty:N, \seq_if_empty:c}
648 % \begin{syntax}
649 % \cs{seq_if_empty_p:N} \meta{seq~var}
650 % \cs{seq_if_empty:NTF} \meta{seq~var} \Arg{true code} \Arg{false code}
651 % \end{syntax}
652 % Tests if the \meta{seq~var} is empty (containing no items).
653 % \end{function}
654 %
655 % \begin{function}[TF]
656 % {
657 % \seq_if_in:Nn, \seq_if_in:NV, \seq_if_in:Nv, \seq_if_in:Ne,
658 % \seq_if_in:No,
659 % \seq_if_in:cn, \seq_if_in:cV, \seq_if_in:cv, \seq_if_in:ce,
660 % \seq_if_in:co,
661 % }
662 % \begin{syntax}
663 % \cs{seq_if_in:NnTF} \meta{seq~var} \Arg{item} \Arg{true code} \Arg{false code}
664 % \end{syntax}
665 % Tests if the \meta{item} is present in the \meta{seq~var}.
666 % \end{function}
667 %
668 % \section{Mapping over sequences}
669 %
670 % All mappings are done at the current group level, i.e., any
671 % local assignments made by the \meta{function} or \meta{code} discussed
672 % below remain in effect after the loop.
673 %
674 % \begin{function}[rEXP, updated = 2012-06-29]
675 % {\seq_map_function:NN, \seq_map_function:cN}
676 % \begin{syntax}
677 % \cs{seq_map_function:NN} \meta{seq~var} \meta{function}
678 % \end{syntax}
679 % Applies \meta{function} to every \meta{item} stored in the
680 % \meta{seq~var}. The \meta{function} will receive one argument for
681 % each iteration. The \meta{items} are returned from left to right.
682 % To pass further arguments to the \meta{function}, see
683 % \cs{seq_map_tokens:Nn}.
684 % The function \cs{seq_map_inline:Nn} is faster than
685 % \cs{seq_map_function:NN} for sequences with more than about~$10$
686 % items.
687 % \end{function}
688 %
689 % \begin{function}[updated = 2012-06-29]
690 % {\seq_map_inline:Nn, \seq_map_inline:cn}
691 % \begin{syntax}
692 % \cs{seq_map_inline:Nn} \meta{seq~var} \Arg{inline function}
693 % \end{syntax}
694 % Applies \meta{inline function} to every \meta{item} stored
695 % within the \meta{seq~var}. The \meta{inline function} should
696 % consist of code which will receive the \meta{item} as |#1|.
697 % The \meta{items} are returned from left to right.
698 % \end{function}
699 %
700 % \begin{function}[rEXP, added = 2019-08-30]
701 % {\seq_map_tokens:Nn, \seq_map_tokens:cn}
702 % \begin{syntax}
703 % \cs{seq_map_tokens:Nn} \meta{seq~var} \Arg{code}
704 % \end{syntax}
705 % Analogue of \cs{seq_map_function:NN} which maps several tokens
706 % instead of a single function. The \meta{code} receives each item in
707 % the \meta{seq~var} as a trailing brace group. For instance,
708 % \begin{verbatim}
709 % \seq_map_tokens:Nn \l_my_seq { \prg_replicate:nn { 2 } }
710 % \end{verbatim}
711 % expands to twice each item in the \meta{seq~var}: for each item in
712 % |\l_my_seq| the function \cs{prg_replicate:nn} receives |2| and
713 % \meta{item} as its two arguments. The function
714 % \cs{seq_map_inline:Nn} is typically faster but it is not expandable.
715 % \end{function}
716 %
717 % \begin{function}[updated = 2012-06-29]
718 % {
719 % \seq_map_variable:NNn, \seq_map_variable:Ncn,
720 % \seq_map_variable:cNn, \seq_map_variable:ccn
721 % }
722 % \begin{syntax}
723 % \cs{seq_map_variable:NNn} \meta{seq~var} \meta{variable} \Arg{code}
724 % \end{syntax}
725 % Stores each \meta{item} of the \meta{seq~var} in turn in the (token
726 % list) \meta{variable} and applies the \meta{code}. The \meta{code}
727 % will usually make use of the \meta{variable}, but this is not
728 % enforced. The assignments to the \meta{variable} are local. Its
729 % value after the loop is the last \meta{item} in the \meta{seq~var},
730 % or its original value if the \meta{seq~var} is empty. The
731 % \meta{items} are returned from left to right.
732 % \end{function}
733 %
734 % \begin{function}[rEXP,added = 2018-05-03]{\seq_map_indexed_function:NN}
735 % \begin{syntax}
736 % \cs{seq_map_indexed_function:NN} \meta{seq~var} \meta{function}
737 % \end{syntax}
738 % Applies \meta{function} to every entry in the \meta{seq~var}.
739 % The \meta{function} should have signature |:nn|. It
740 % receives two arguments for each iteration: the \meta{index} (namely
741 % |1| for the first entry, then |2| and so on) and the \meta{item}.
742 % \end{function}
743 %
744 % \begin{function}[added = 2018-05-03]{\seq_map_indexed_inline:Nn}
745 % \begin{syntax}
746 % \cs{seq_map_indexed_inline:Nn} \meta{seq~var} \Arg{inline function}
747 % \end{syntax}
748 % Applies \meta{inline function} to every entry in the \meta{seq~var}.
749 % The \meta{inline function} should consist of code which
750 % receives the \meta{index} (namely |1| for the first entry, then |2|
751 % and so on) as~|#1| and the \meta{item} as~|#2|.
752 % \end{function}
753 %
754 % \begin{function}[rEXP, added = 2023-05-10]
755 % {
756 % \seq_map_pairwise_function:NNN, \seq_map_pairwise_function:NcN,
757 % \seq_map_pairwise_function:cNN, \seq_map_pairwise_function:ccN
758 % }
759 % \begin{syntax}
760 % \cs{seq_map_pairwise_function:NNN} \meta{seq var_1} \meta{seq var_2} \meta{function}
761 % \end{syntax}
762 % Applies \meta{function} to every pair of items
763 % \meta{seq var_1-item}--\meta{seq var_2-item} from the two sequences, returning
764 % items from both sequences from left to right. The \meta{function}
765 % receives two \texttt{n}-type arguments for each iteration. The mapping
766 % terminates when
767 % the end of either sequence is reached (i.e., whichever sequence has
768 % fewer items determines how many iterations
769 % occur).
770 % \end{function}
771 %
772 % \begin{function}[rEXP, updated = 2012-06-29]{\seq_map_break:}
773 % \begin{syntax}
774 % \cs{seq_map_break:}
775 % \end{syntax}
776 % Used to terminate a \cs[no-index]{seq_map_\ldots} function before all
777 % entries in the \meta{seq~var} have been processed. This
778 % normally takes place within a conditional statement, for example
779 % \begin{verbatim}
780 % \seq_map_inline:Nn \l_my_seq
781 % {
782 % \str_if_eq:nnTF { #1 } { bingo }
783 % { \seq_map_break: }
784 % {
785 % % Do something useful
786 % }
787 % }
788 % \end{verbatim}
789 % Use outside of a \cs[no-index]{seq_map_\ldots} scenario leads to low
790 % level \TeX{} errors.
791 % \begin{texnote}
792 % When the mapping is broken, additional tokens may be inserted
793 % before further items are taken
794 % from the input stream. This depends on the design of the mapping
795 % function.
796 % \end{texnote}
797 % \end{function}
798 %
799 % \begin{function}[rEXP, updated = 2012-06-29]{\seq_map_break:n}
800 % \begin{syntax}
801 % \cs{seq_map_break:n} \Arg{code}
802 % \end{syntax}
803 % Used to terminate a \cs[no-index]{seq_map_\ldots} function before all
804 % entries in the \meta{seq~var} have been processed, inserting
805 % the \meta{code} after the mapping has ended. This
806 % normally takes place within a conditional statement, for example
807 % \begin{verbatim}
808 % \seq_map_inline:Nn \l_my_seq
809 % {
810 % \str_if_eq:nnTF { #1 } { bingo }
811 % { \seq_map_break:n { <code> } }
812 % {
813 % % Do something useful
814 % }
815 % }
816 % \end{verbatim}
817 % Use outside of a \cs[no-index]{seq_map_\ldots} scenario leads to low
818 % level \TeX{} errors.
819 % \begin{texnote}
820 % When the mapping is broken, additional tokens may be inserted
821 % before the \meta{code} is
822 % inserted into the input stream.
823 % This depends on the design of the mapping function.
824 % \end{texnote}
825 % \end{function}
826 %
827 % \begin{function}[added = 2011-12-22, updated = 2020-07-16]
828 % {\seq_set_map:NNn, \seq_gset_map:NNn}
829 % \begin{syntax}
830 % \cs{seq_set_map:NNn} \meta{seq~var_1} \meta{seq~var_2} \Arg{inline function}
831 % \end{syntax}
832 % Applies \meta{inline function} to every \meta{item} stored
833 % within the \meta{seq~var_2}. The \meta{inline function} should
834 % consist of code which will receive the \meta{item} as |#1|.
835 % The sequence resulting from applying \meta{inline function} to each
836 % \meta{item} is assigned to \meta{seq~var_1}.
837 % \begin{texnote}
838 % Contrarily to other mapping functions, \cs{seq_map_break:} cannot
839 % be used in this function, and would lead to low-level \TeX{} errors.
840 % \end{texnote}
841 % \end{function}
842 %
843 % \begin{function}[added = 2020-07-16, updated = 2023-10-26]
844 % {\seq_set_map_e:NNn, \seq_gset_map_e:NNn}
845 % \begin{syntax}
846 % \cs{seq_set_map_e:NNn} \meta{seq~var_1} \meta{seq~var_2} \Arg{inline function}
847 % \end{syntax}
848 % Applies \meta{inline function} to every \meta{item} stored
849 % within the \meta{seq~var_2}. The \meta{inline function} should
850 % consist of code which will receive the \meta{item} as |#1|.
851 % The sequence resulting from \texttt{e}-expanding
852 % \meta{inline function} applied to each \meta{item}
853 % is assigned to \meta{seq~var_1}. As such, the code
854 % in \meta{inline function} should be expandable.
855 % \begin{texnote}
856 % Contrarily to other mapping functions, \cs{seq_map_break:} cannot
857 % be used in this function, and would lead to low-level \TeX{} errors.
858 % \end{texnote}
859 % \end{function}
860 %
861 % \begin{function}[EXP, added = 2012-07-13]{\seq_count:N, \seq_count:c}
862 % \begin{syntax}
863 % \cs{seq_count:N} \meta{seq~var}
864 % \end{syntax}
865 % Leaves the number of items in the \meta{seq~var} in the input
866 % stream as an \meta{integer denotation}. The total number of items
867 % in a \meta{seq~var} includes those which are empty and duplicates,
868 % i.e., every item in a \meta{seq~var} is unique.
869 % \end{function}
870 %
871 % \section{Using the content of sequences directly}
872 %
873 % \begin{function}[EXP, added = 2013-05-26]{\seq_use:Nnnn, \seq_use:cnnn}
874 % \begin{syntax}
875 % \cs{seq_use:Nnnn} \meta{seq~var} \Arg{separator~between~two} \Arg{separator~between~more~than~two} \Arg{separator~between~final~two}
876 % \end{syntax}
877 % Places the contents of the \meta{seq~var} in the input stream, with
878 % the appropriate \meta{separator} between the items. Namely, if the
879 % sequence has more than two items, the \meta{separator between more
880 % than two} is placed between each pair of items except the last,
881 % for which the \meta{separator between final two} is used. If the
882 % sequence has exactly two items, then they are placed in the input stream
883 % separated by the \meta{separator between two}. If the sequence has
884 % a single item, it is placed in the input stream, and an empty sequence
885 % produces no output. An error is raised if the variable does
886 % not exist or if it is invalid.
887 %
888 % For example,
889 % \begin{verbatim}
890 % \seq_set_split:Nnn \l_tmpa_seq { | } { a | b | c | {de} | f }
891 % \seq_use:Nnnn \l_tmpa_seq { ~and~ } { ,~ } { ,~and~ }
892 % \end{verbatim}
893 % inserts \enquote{\texttt{a, b, c, de, and f}} in the input
894 % stream. The first separator argument is not used in this case
895 % because the sequence has more than $2$ items.
896 % \begin{texnote}
897 % The result is returned within the \tn{unexpanded}
898 % primitive (\cs{exp_not:n}), which means that the \meta{items}
899 % do not expand further when appearing in an \texttt{e}-type
900 % or \texttt{x}-type argument expansion.
901 % \end{texnote}
902 % \end{function}
903 %
904 % \begin{function}[EXP, added = 2013-05-26]{\seq_use:Nn, \seq_use:cn}
905 % \begin{syntax}
906 % \cs{seq_use:Nn} \meta{seq~var} \Arg{separator}
907 % \end{syntax}
908 % Places the contents of the \meta{seq~var} in the input stream, with
909 % the \meta{separator} between the items. If the sequence has
910 % a single item, it is placed in the input stream with no \meta{separator},
911 % and an empty sequence produces no output. An error is raised if
912 % the variable does not exist or if it is invalid.
913 %
914 % For example,
915 % \begin{verbatim}
916 % \seq_set_split:Nnn \l_tmpa_seq { | } { a | b | c | {de} | f }
917 % \seq_use:Nn \l_tmpa_seq { ~and~ }
918 % \end{verbatim}
919 % inserts \enquote{\texttt{a and b and c and de and f}} in the input
920 % stream.
921 % \begin{texnote}
922 % The result is returned within the \tn{unexpanded}
923 % primitive (\cs{exp_not:n}), which means that the \meta{items}
924 % do not expand further when appearing in an \texttt{e}-type
925 % or \texttt{x}-type argument expansion.
926 % \end{texnote}
927 % \end{function}
928 %
929 % \begin{function}[EXP, added = 2025-06-09]{\seq_format:Nn, \seq_format:cn}
930 % \begin{syntax}
931 % \cs{seq_format:Nn} \meta{seq~var} \Arg{format specification}
932 % \end{syntax}
933 % Converts each item in the \meta{seq~var} as a token list to a string
934 % according to the \meta{format specification}, and concatenates the results.
935 % The details of the \meta{format specification} are described in
936 % Section~\ref{sec:l3str-format:spec}.
937 % \end{function}
938 %
939 % \section{Sequences as stacks}
940 %
941 % Sequences can be used as stacks, where data is pushed to and popped
942 % from the top of the sequence. (The left of a sequence is the top, for
943 % performance reasons.) The stack functions for sequences are not
944 % intended to be mixed with the general ordered data functions detailed
945 % in the previous section: a sequence should either be used as an
946 % ordered data type or as a stack, but not in both ways.
947 %
948 % \begin{function}[updated = 2012-05-14]{\seq_get:NN, \seq_get:cN}
949 % \begin{syntax}
950 % \cs{seq_get:NN} \meta{seq~var} \meta{tl~var}
951 % \end{syntax}
952 % Reads the top item from a \meta{seq~var} into the
953 % \meta{tl~var} without removing it from the
954 % \meta{seq~var}. The \meta{tl~var} is assigned locally.
955 % If \meta{seq~var} is empty the \meta{tl~var} is set to
956 % the special marker \cs{q_no_value}.
957 % \end{function}
958 %
959 % \begin{function}[updated = 2012-05-14]{\seq_pop:NN, \seq_pop:cN}
960 % \begin{syntax}
961 % \cs{seq_pop:NN} \meta{seq~var} \meta{tl~var}
962 % \end{syntax}
963 % Pops the top item from a \meta{seq~var} into the
964 % \meta{tl~var}. Both of the variables are assigned
965 % locally. If \meta{seq~var} is empty the \meta{tl~var}
966 % is set to the special marker \cs{q_no_value}.
967 % \end{function}
968 %
969 % \begin{function}[updated = 2012-05-14]{\seq_gpop:NN, \seq_gpop:cN}
970 % \begin{syntax}
971 % \cs{seq_gpop:NN} \meta{seq~var} \meta{tl~var}
972 % \end{syntax}
973 % Pops the top item from a \meta{seq~var} into the
974 % \meta{tl~var}. The \meta{seq~var} is modified globally,
975 % while the \meta{tl~var} is assigned locally. If
976 % \meta{seq~var} is empty the \meta{tl~var} is set to
977 % the special marker \cs{q_no_value}.
978 % \end{function}
979 %
980 % \begin{function}[TF, added = 2012-05-14, updated = 2012-05-19]{\seq_get:NN, \seq_get:cN}
981 % \begin{syntax}
982 % \cs{seq_get:NNTF} \meta{seq~var} \meta{tl~var} \Arg{true code} \Arg{false code}
983 % \end{syntax}
984 % If the \meta{seq~var} is empty, leaves the \meta{false code} in the
985 % input stream. The value of the \meta{tl~var} is
986 % not defined in this case and should not be relied upon. If the
987 % \meta{seq~var} is non-empty, stores the top item from a
988 % \meta{seq~var} in the \meta{tl~var} without removing it from
989 % the \meta{seq~var}. The \meta{tl~var} is assigned locally.
990 % \end{function}
991 %
992 % \begin{function}[TF, added = 2012-05-14, updated = 2012-05-19]{\seq_pop:NN, \seq_pop:cN}
993 % \begin{syntax}
994 % \cs{seq_pop:NNTF} \meta{seq~var} \meta{tl~var} \Arg{true code} \Arg{false code}
995 % \end{syntax}
996 % If the \meta{seq~var} is empty, leaves the \meta{false code} in the
997 % input stream. The value of the \meta{tl~var} is
998 % not defined in this case and should not be relied upon. If the
999 % \meta{seq~var} is non-empty, pops the top item from the
1000 % \meta{seq~var} in the \meta{tl~var}, i.e., removes the
1001 % item from the \meta{seq~var}. Both the \meta{seq~var} and the
1002 % \meta{tl~var} are assigned locally.
1003 % \end{function}
1004 %
1005 % \begin{function}[TF, added = 2012-05-14, updated = 2012-05-19]{\seq_gpop:NN, \seq_gpop:cN}
1006 % \begin{syntax}
1007 % \cs{seq_gpop:NNTF} \meta{seq~var} \meta{tl~var} \Arg{true code} \Arg{false code}
1008 % \end{syntax}
1009 % If the \meta{seq~var} is empty, leaves the \meta{false code} in the
1010 % input stream. The value of the \meta{tl~var} is
1011 % not defined in this case and should not be relied upon. If the
1012 % \meta{seq~var} is non-empty, pops the top item from the \meta{seq~var}
1013 % in the \meta{tl~var}, i.e., removes the item from the
1014 % \meta{seq~var}. The \meta{seq~var} is modified globally, while the
1015 % \meta{tl~var} is assigned locally.
1016 % \end{function}
1017 %
1018 % \begin{function}
1019 % {
1020 % \seq_push:Nn, \seq_push:NV, \seq_push:Nv, \seq_push:Ne,
1021 % \seq_push:No,
1022 % \seq_push:cn, \seq_push:cV, \seq_push:cv, \seq_push:ce,
1023 % \seq_push:co,
1024 % \seq_gpush:Nn, \seq_gpush:NV, \seq_gpush:Nv, \seq_gpush:Ne,
1025 % \seq_gpush:No,
1026 % \seq_gpush:cn, \seq_gpush:cV, \seq_gpush:cv, \seq_gpush:ce,
1027 % \seq_gpush:co
1028 % }
1029 % \begin{syntax}
1030 % \cs{seq_push:Nn} \meta{seq~var} \Arg{item}
1031 % \end{syntax}
1032 % Adds the \Arg{item} to the top of the \meta{seq~var}.
1033 % \end{function}
1034 %
1035 % \section{Sequences as sets}
1036 %
1037 % Sequences can also be used as sets, such that all of their items are
1038 % distinct. Usage of sequences as sets is not currently widespread,
1039 % hence no specific set function is provided. Instead, it is explained
1040 % here how common set operations can be performed by combining several
1041 % functions described in earlier sections. When using sequences to
1042 % implement sets, one should be careful not to rely on the order of
1043 % items in the sequence representing the set.
1044 %
1045 % Sets should not contain several occurrences of a given item. To make
1046 % sure that a \meta{seq~var} only has distinct items, use
1047 % \cs{seq_remove_duplicates:N} \meta{seq~var}. This function
1048 % is relatively slow, and to avoid performance issues one should only
1049 % use it when necessary.
1050 %
1051 % Some operations on a set \meta{seq~var} are straightforward. For
1052 % instance, \cs{seq_count:N} \meta{seq~var} expands to the number of
1053 % items, while \cs{seq_if_in:NnTF} \meta{seq~var} \Arg{item} tests if
1054 % the \meta{item} is in the set.
1055 %
1056 % Adding an \meta{item} to a set \meta{seq~var} can be done by appending
1057 % it to the \meta{seq~var} if it is not already in the \meta{seq~var}:
1058 % \begin{quote}\ttfamily\parskip=0pt\obeylines
1059 % \cs{seq_if_in:NnF} \meta{seq~var} \Arg{item}
1060 % | |\{ \cs{seq_put_right:Nn} \meta{seq~var} \Arg{item} \}
1061 % \end{quote}
1062 % Removing an \meta{item} from a set \meta{seq~var} can be done using
1063 % \cs{seq_remove_all:Nn},
1064 % \begin{quote}\ttfamily
1065 % \cs{seq_remove_all:Nn} \meta{seq~var} \Arg{item}
1066 % \end{quote}
1067 %
1068 % The intersection of two sets \meta{seq~var_1} and \meta{seq~var_2} can
1069 % be stored into \meta{seq~var_3} by collecting items of
1070 % \meta{seq~var_1} which are in \meta{seq~var_2}.
1071 % \begin{quote}\ttfamily\parskip=0pt\obeylines
1072 % \cs{seq_clear:N} \meta{seq~var_3}
1073 % \cs{seq_map_inline:Nn} \meta{seq~var_1}
1074 % | |\{
1075 % | |\cs{seq_if_in:NnT} \meta{seq~var_2} \{\#1\}
1076 % | |\{ \cs{seq_put_right:Nn} \meta{seq~var_3} \{\#1\} \}
1077 % | |\}
1078 % \end{quote}
1079 % The code as written here only works if \meta{seq~var_3} is different
1080 % from the other two sequence variables. To cover all cases, items
1081 % should first be collected in a sequence
1082 % |\l__|\meta{pkg}|_tmp_seq|, then \meta{seq~var_3} should be set
1083 % equal to this internal sequence. The same remark applies to other set
1084 % functions.
1085 %
1086 % The union of two sets \meta{seq~var_1} and \meta{seq~var_2} can be
1087 % stored into \meta{seq~var_3} through
1088 % \begin{quote}\ttfamily
1089 % \cs{seq_concat:NNN} \meta{seq~var_3} \meta{seq~var_1} \meta{seq~var_2} \\
1090 % \cs{seq_remove_duplicates:N} \meta{seq~var_3}
1091 % \end{quote}
1092 % or by adding items to (a copy of) \meta{seq~var_1} one by one
1093 % \begin{quote}\ttfamily\parskip=0pt\obeylines
1094 % \cs{seq_set_eq:NN} \meta{seq~var_3} \meta{seq~var_1}
1095 % \cs{seq_map_inline:Nn} \meta{seq~var_2}
1096 % | |\{
1097 % | |\cs{seq_if_in:NnF} \meta{seq~var_3} \{\#1\}
1098 % | |\{ \cs{seq_put_right:Nn} \meta{seq~var_3} \{\#1\} \}
1099 % | |\}
1100 % \end{quote}
1101 % The second approach is faster than the first when the \meta{seq~var_2}
1102 % is short compared to \meta{seq~var_1}.
1103 %
1104 % The difference of two sets \meta{seq~var_1} and \meta{seq~var_2} can
1105 % be stored into \meta{seq~var_3} by removing items of the
1106 % \meta{seq~var_2} from (a copy of) the \meta{seq~var_1} one by one.
1107 % \begin{quote}\ttfamily\parskip=0pt\obeylines
1108 % \cs{seq_set_eq:NN} \meta{seq~var_3} \meta{seq~var_1}
1109 % \cs{seq_map_inline:Nn} \meta{seq~var_2}
1110 % | |\{ \cs{seq_remove_all:Nn} \meta{seq~var_3} \{\#1\} \}
1111 % \end{quote}
1112 %
1113 % The symmetric difference of two sets \meta{seq~var_1} and
1114 % \meta{seq~var_2} can be stored into \meta{seq~var_3} by computing the
1115 % difference between \meta{seq~var_1} and \meta{seq~var_2} and storing
1116 % the result as |\l__|\meta{pkg}|_tmp_seq|, then the difference
1117 % between \meta{seq~var_2} and \meta{seq~var_1}, and finally
1118 % concatenating the two differences to get the symmetric differences.
1119 % \begin{quote}\ttfamily\parskip=0pt\obeylines
1120 % \cs{seq_set_eq:NN} |\l__|\meta{pkg}|_tmp_seq| \meta{seq~var_1}
1121 % \cs{seq_map_inline:Nn} \meta{seq~var_2}
1122 % | |\{ \cs{seq_remove_all:Nn} |\l__|\meta{pkg}|_tmp_seq| \{\#1\} \}
1123 % \cs{seq_set_eq:NN} \meta{seq~var_3} \meta{seq~var_2}
1124 % \cs{seq_map_inline:Nn} \meta{seq~var_1}
1125 % | |\{ \cs{seq_remove_all:Nn} \meta{seq~var_3} \{\#1\} \}
1126 % \cs{seq_concat:NNN} \meta{seq~var_3} \meta{seq~var_3} |\l__|\meta{pkg}|_tmp_seq|
1127 % \end{quote}
1128 %
1129 % \section{Constant and scratch sequences}
1130 %
1131 % \begin{variable}[added = 2012-07-02]{\c_empty_seq}
1132 % Constant that is always empty.
1133 % \end{variable}
1134 %
1135 % \begin{variable}[added = 2012-04-26]{\l_tmpa_seq, \l_tmpb_seq}
1136 % Scratch sequences for local assignment. These are never used by
1137 % the kernel code, and so are safe for use with any \LaTeX3-defined
1138 % function. However, they may be overwritten by other non-kernel
1139 % code and so should only be used for short-term storage.
1140 % \end{variable}
1141 %
1142 % \begin{variable}[added = 2012-04-26]{\g_tmpa_seq, \g_tmpb_seq}
1143 % Scratch sequences for global assignment. These are never used by
1144 % the kernel code, and so are safe for use with any \LaTeX3-defined
1145 % function. However, they may be overwritten by other non-kernel
1146 % code and so should only be used for short-term storage.
1147 % \end{variable}
1148 %
1149 % \section{Viewing sequences}
1150 %
1151 % \begin{function}[updated = 2021-04-29]{\seq_show:N, \seq_show:c}
1152 % \begin{syntax}
1153 % \cs{seq_show:N} \meta{seq~var}
1154 % \end{syntax}
1155 % Displays the entries in the \meta{seq~var} in the terminal.
1156 % \end{function}
1157 %
1158 % \begin{function}[added = 2014-08-12, updated = 2021-04-29]{\seq_log:N, \seq_log:c}
1159 % \begin{syntax}
1160 % \cs{seq_log:N} \meta{seq~var}
1161 % \end{syntax}
1162 % Writes the entries in the \meta{seq~var} in the log file.
1163 % \end{function}
1164 %
1165 % \end{documentation}
1166 %
1167 % \begin{implementation}
1168 %
1169 % \section{\pkg{l3seq} implementation}
1170 %
1171 % \TestFiles{m3seq002,m3seq003}
1172 %
1173 % \begin{macrocode}
1174 %<*code>
1175 % \end{macrocode}
1176 %
1177 % \begin{macrocode}
1178 %<@@=seq>
1179 % \end{macrocode}
1180 %
1181 % A sequence is a control sequence whose top-level expansion is of the
1182 % form \enquote{\cs{s_@@} \cs{@@_item:n} \marg{item_1} \ldots
1183 % \cs{@@_item:n} \marg{item_n}}, with a leading scan
1184 % mark followed by $n$~items of the same form. An
1185 % earlier implementation used the structure \enquote{\cs{seq_elt:w}
1186 % \meta{item_1} \cs{seq_elt_end:} \ldots \cs{seq_elt:w} \meta{item_n}
1187 % \cs{seq_elt_end:}}. This allowed rapid searching using a delimited
1188 % function, but was not suitable for items containing |{|, |}| and |#|
1189 % tokens, and also lead to the loss of surrounding braces around items
1190 %
1191 % \begin{function}[EXP]{\@@_item:n}
1192 % \begin{syntax}
1193 % \cs{@@_item:n} \Arg{item}
1194 % \end{syntax}
1195 % The internal token used to begin each sequence entry. If expanded
1196 % outside of a mapping or manipulation function, an error is
1197 % raised. The definition should always be set globally.
1198 % \end{function}
1199 %
1200 % \begin{function}{\@@_push_item_def:n, \@@_push_item_def:e}
1201 % \begin{syntax}
1202 % \cs{@@_push_item_def:n} \Arg{code}
1203 % \end{syntax}
1204 % Saves the definition of \cs{@@_item:n} and redefines it to
1205 % accept one parameter and expand to \meta{code}. This function
1206 % should always be balanced by use of \cs{@@_pop_item_def:}.
1207 % \end{function}
1208 %
1209 % \begin{function}{\@@_pop_item_def:}
1210 % \begin{syntax}
1211 % \cs{@@_pop_item_def:}
1212 % \end{syntax}
1213 % Restores the definition of \cs{@@_item:n} most recently saved by
1214 % \cs{@@_push_item_def:n}. This function should always be used in
1215 % a balanced pair with \cs{@@_push_item_def:n}.
1216 % \end{function}
1217 %
1218 % \begin{variable}{\s_@@}
1219 % This private scan mark.
1220 % \begin{macrocode}
1221 \scan_new:N \s_@@
1222 % \end{macrocode}
1223 % \end{variable}
1224 %
1225 % \begin{variable}{\s_@@_mark,\s_@@_stop}
1226 % Private scan marks.
1227 % \begin{macrocode}
1228 \scan_new:N \s_@@_mark
1229 \scan_new:N \s_@@_stop
1230 % \end{macrocode}
1231 % \end{variable}
1232 %
1233 % \begin{macro}[EXP]{\@@_item:n}
1234 % The delimiter is always defined, but when used incorrectly simply
1235 % removes its argument and hits an undefined control sequence to
1236 % raise an error.
1237 % \begin{macrocode}
1238 \cs_new:Npn \@@_item:n
1239 {
1240 \msg_expandable_error:nn { seq } { misused }
1241 \use_none:n
1242 }
1243 % \end{macrocode}
1244 % \end{macro}
1245 %
1246 % \begin{variable}{\l_@@_tmpa_tl, \l_@@_tmpb_tl}
1247 % Scratch space for various internal uses.
1248 % \begin{macrocode}
1249 \tl_new:N \l_@@_tmpa_tl
1250 \tl_new:N \l_@@_tmpb_tl
1251 % \end{macrocode}
1252 % \end{variable}
1253 %
1254 % \begin{macro}{\@@_tmp:w}
1255 % Scratch function for internal use.
1256 % \begin{macrocode}
1257 \cs_new_eq:NN \@@_tmp:w ?
1258 % \end{macrocode}
1259 % \end{macro}
1260 %
1261 % \begin{variable}{\c_empty_seq}
1262 % A sequence with no item, following the structure mentioned above.
1263 % \begin{macrocode}
1264 \tl_const:Nn \c_empty_seq { \s_@@ }
1265 % \end{macrocode}
1266 % \end{variable}
1267 %
1268 % \subsection{Allocation and initialization}
1269 %
1270 % \begin{macro}{\seq_new:N, \seq_new:c}
1271 % \UnitTested
1272 % Sequences are initialized to \cs{c_empty_seq}.
1273 % \begin{macrocode}
1274 \cs_new_protected:Npn \seq_new:N #1
1275 {
1276 \__kernel_chk_if_free_cs:N #1
1277 \cs_gset_eq:NN #1 \c_empty_seq
1278 }
1279 \cs_generate_variant:Nn \seq_new:N { c }
1280 % \end{macrocode}
1281 % \end{macro}
1282 %
1283 % \begin{macro}{\seq_clear:N, \seq_clear:c}
1284 % \UnitTested
1285 % \begin{macro}{\seq_gclear:N, \seq_gclear:c}
1286 % \UnitTested
1287 % Clearing a sequence is similar to setting it equal to the empty one.
1288 % \begin{macrocode}
1289 \cs_new_protected:Npn \seq_clear:N #1
1290 { \seq_set_eq:NN #1 \c_empty_seq }
1291 \cs_generate_variant:Nn \seq_clear:N { c }
1292 \cs_new_protected:Npn \seq_gclear:N #1
1293 { \seq_gset_eq:NN #1 \c_empty_seq }
1294 \cs_generate_variant:Nn \seq_gclear:N { c }
1295 % \end{macrocode}
1296 % \end{macro}
1297 % \end{macro}
1298 %
1299 % \begin{macro}{\seq_clear_new:N, \seq_clear_new:c}
1300 % \UnitTested
1301 % \begin{macro}{\seq_gclear_new:N, \seq_gclear_new:c}
1302 % \UnitTested
1303 % Once again we copy code from the token list functions.
1304 % \begin{macrocode}
1305 \cs_new_protected:Npn \seq_clear_new:N #1
1306 { \seq_if_exist:NTF #1 { \seq_clear:N #1 } { \seq_new:N #1 } }
1307 \cs_generate_variant:Nn \seq_clear_new:N { c }
1308 \cs_new_protected:Npn \seq_gclear_new:N #1
1309 { \seq_if_exist:NTF #1 { \seq_gclear:N #1 } { \seq_new:N #1 } }
1310 \cs_generate_variant:Nn \seq_gclear_new:N { c }
1311 % \end{macrocode}
1312 % \end{macro}
1313 % \end{macro}
1314 %
1315 % \begin{macro}{\seq_set_eq:NN, \seq_set_eq:cN, \seq_set_eq:Nc, \seq_set_eq:cc}
1316 % \UnitTested
1317 % \begin{macro}
1318 % {\seq_gset_eq:NN, \seq_gset_eq:cN, \seq_gset_eq:Nc, \seq_gset_eq:cc}
1319 % \UnitTested
1320 % Copying a sequence is the same as copying the underlying token list.
1321 % \begin{macrocode}
1322 \cs_new_eq:NN \seq_set_eq:NN \tl_set_eq:NN
1323 \cs_new_eq:NN \seq_set_eq:Nc \tl_set_eq:Nc
1324 \cs_new_eq:NN \seq_set_eq:cN \tl_set_eq:cN
1325 \cs_new_eq:NN \seq_set_eq:cc \tl_set_eq:cc
1326 \cs_new_eq:NN \seq_gset_eq:NN \tl_gset_eq:NN
1327 \cs_new_eq:NN \seq_gset_eq:Nc \tl_gset_eq:Nc
1328 \cs_new_eq:NN \seq_gset_eq:cN \tl_gset_eq:cN
1329 \cs_new_eq:NN \seq_gset_eq:cc \tl_gset_eq:cc
1330 % \end{macrocode}
1331 % \end{macro}
1332 % \end{macro}
1333 %
1334 % \begin{macro}
1335 % {
1336 % \seq_set_from_clist:NN, \seq_set_from_clist:cN,
1337 % \seq_set_from_clist:Nc, \seq_set_from_clist:cc,
1338 % \seq_set_from_clist:Nn, \seq_set_from_clist:cn
1339 % }
1340 % \begin{macro}
1341 % {
1342 % \seq_gset_from_clist:NN, \seq_gset_from_clist:cN,
1343 % \seq_gset_from_clist:Nc, \seq_gset_from_clist:cc,
1344 % \seq_gset_from_clist:Nn, \seq_gset_from_clist:cn
1345 % }
1346 % Setting a sequence from a comma-separated list is done using a simple
1347 % mapping.
1348 % \begin{macrocode}
1349 \cs_new_protected:Npn \seq_set_from_clist:NN #1#2
1350 {
1351 \__kernel_tl_set:Nx #1
1352 { \s_@@ \clist_map_function:NN #2 \@@_wrap_item:n }
1353 }
1354 \cs_new_protected:Npn \seq_set_from_clist:Nn #1#2
1355 {
1356 \__kernel_tl_set:Nx #1
1357 { \s_@@ \clist_map_function:nN {#2} \@@_wrap_item:n }
1358 }
1359 \cs_new_protected:Npn \seq_gset_from_clist:NN #1#2
1360 {
1361 \__kernel_tl_gset:Nx #1
1362 { \s_@@ \clist_map_function:NN #2 \@@_wrap_item:n }
1363 }
1364 \cs_new_protected:Npn \seq_gset_from_clist:Nn #1#2
1365 {
1366 \__kernel_tl_gset:Nx #1
1367 { \s_@@ \clist_map_function:nN {#2} \@@_wrap_item:n }
1368 }
1369 \cs_generate_variant:Nn \seq_set_from_clist:NN { Nc }
1370 \cs_generate_variant:Nn \seq_set_from_clist:NN { c , cc }
1371 \cs_generate_variant:Nn \seq_set_from_clist:Nn { c }
1372 \cs_generate_variant:Nn \seq_gset_from_clist:NN { Nc }
1373 \cs_generate_variant:Nn \seq_gset_from_clist:NN { c , cc }
1374 \cs_generate_variant:Nn \seq_gset_from_clist:Nn { c }
1375 % \end{macrocode}
1376 % \end{macro}
1377 % \end{macro}
1378 %
1379 % \begin{macro}{\seq_const_from_clist:Nn, \seq_const_from_clist:cn}
1380 % Almost identical to \cs{seq_set_from_clist:Nn}.
1381 % \begin{macrocode}
1382 \cs_new_protected:Npn \seq_const_from_clist:Nn #1#2
1383 {
1384 \tl_const:Ne #1
1385 { \s_@@ \clist_map_function:nN {#2} \@@_wrap_item:n }
1386 }
1387 \cs_generate_variant:Nn \seq_const_from_clist:Nn { c }
1388 % \end{macrocode}
1389 % \end{macro}
1390 %
1391 % \begin{macro}
1392 % {
1393 % \seq_set_split:Nnn ,
1394 % \seq_set_split:NVn , \seq_set_split:NnV , \seq_set_split:NVV ,
1395 % \seq_set_split:Nne , \seq_set_split:Nee ,
1396 % \seq_set_split:Nnx , \seq_set_split:Nxx ,
1397 % \seq_gset_split:Nnn,
1398 % \seq_gset_split:NVn, \seq_gset_split:NnV, \seq_gset_split:NVV,
1399 % \seq_gset_split:Nne, \seq_gset_split:Nee,
1400 % \seq_gset_split:Nnx, \seq_gset_split:Nxx
1401 % }
1402 % \begin{macro}
1403 % {
1404 % \seq_set_split_keep_spaces:Nnn , \seq_set_split_keep_spaces:NnV ,
1405 % \seq_gset_split_keep_spaces:Nnn, \seq_gset_split_keep_spaces:NnV
1406 % }
1407 % \begin{macro}{\@@_set_split:NNnn}
1408 % \begin{macro}
1409 % {
1410 % \@@_set_split:Nw, \@@_set_split:w,
1411 % \@@_set_split_end:
1412 % }
1413 % When the separator is empty, everything is very simple, just map
1414 % \cs{@@_wrap_item:n} through the items of the last argument.
1415 % For non-trivial separators, the goal is to split a given token list
1416 % at the marker, strip spaces from each item, and remove one set of
1417 % outer braces if after removing leading and trailing
1418 % spaces the item is enclosed within braces. After
1419 % \cs{tl_replace_all:Nnn}, the token list \cs{l_@@_tmpa_tl}
1420 % is a repetition of the pattern
1421 % \cs{@@_set_split:Nw} \cs{prg_do_nothing:}
1422 % \meta{item with spaces} \cs{@@_set_split_end:}.
1423 % Then, \texttt{x}-expansion causes \cs{@@_set_split:Nw}
1424 % to trim spaces, and leaves its result as
1425 % \cs{@@_set_split:w} \meta{trimmed item}
1426 % \cs{@@_set_split_end:}. This is then converted
1427 % to the \pkg{l3seq} internal structure by another
1428 % \texttt{x}-expansion. In the first step, we insert
1429 % \cs{prg_do_nothing:} to avoid losing braces too early:
1430 % that would cause space trimming to act within those
1431 % lost braces. The second step is solely there to strip
1432 % braces which are outermost after space trimming.
1433 % \begin{macrocode}
1434 \cs_new_protected:Npn \seq_set_split:Nnn
1435 { \@@_set_split:NNNnn \__kernel_tl_set:Nx \tl_trim_spaces:n }
1436 \cs_new_protected:Npn \seq_gset_split:Nnn
1437 { \@@_set_split:NNNnn \__kernel_tl_gset:Nx \tl_trim_spaces:n }
1438 \cs_new_protected:Npn \seq_set_split_keep_spaces:Nnn
1439 { \@@_set_split:NNNnn \__kernel_tl_set:Nx \exp_not:n }
1440 \cs_new_protected:Npn \seq_gset_split_keep_spaces:Nnn
1441 { \@@_set_split:NNNnn \__kernel_tl_gset:Nx \exp_not:n }
1442 \cs_new_protected:Npn \@@_set_split:NNNnn #1#2#3#4#5
1443 {
1444 \tl_if_empty:nTF {#4}
1445 {
1446 \tl_set:Nn \l_@@_tmpa_tl
1447 { \tl_map_function:nN {#5} \@@_wrap_item:n }
1448 }
1449 {
1450 \tl_set:Nn \l_@@_tmpa_tl
1451 {
1452 \@@_set_split:Nw #2 \prg_do_nothing:
1453 #5
1454 \@@_set_split_end:
1455 }
1456 \tl_replace_all:Nnn \l_@@_tmpa_tl {#4}
1457 {
1458 \@@_set_split_end:
1459 \@@_set_split:Nw #2 \prg_do_nothing:
1460 }
1461 \__kernel_tl_set:Nx \l_@@_tmpa_tl { \l_@@_tmpa_tl }
1462 }
1463 #1 #3 { \s_@@ \l_@@_tmpa_tl }
1464 }
1465 \cs_new:Npn \@@_set_split:Nw #1#2 \@@_set_split_end:
1466 {
1467 \exp_not:N \@@_set_split:w
1468 \exp_args:No #1 {#2}
1469 \exp_not:N \@@_set_split_end:
1470 }
1471 \cs_new:Npn \@@_set_split:w #1 \@@_set_split_end:
1472 { \@@_wrap_item:n {#1} }
1473 \cs_generate_variant:Nn \seq_set_split:Nnn { NV , NnV , NVV , Nne , Nee }
1474 \cs_generate_variant:Nn \seq_set_split:Nnn { Nnx , Nxx }
1475 \cs_generate_variant:Nn \seq_gset_split:Nnn { NV , NnV , NVV , Nne , Nee }
1476 \cs_generate_variant:Nn \seq_gset_split:Nnn { Nnx , Nxx }
1477 \cs_generate_variant:Nn \seq_set_split_keep_spaces:Nnn { NnV }
1478 \cs_generate_variant:Nn \seq_gset_split_keep_spaces:Nnn { NnV }
1479 % \end{macrocode}
1480 % \end{macro}
1481 % \end{macro}
1482 % \end{macro}
1483 % \end{macro}
1484 %
1485 % \begin{macro}{\seq_set_filter:NNn, \seq_gset_filter:NNn}
1486 % \begin{macro}{\@@_set_filter:NNNn}
1487 % Similar to \cs{seq_map_inline:Nn}, without a
1488 % \cs{prg_break_point:} because the user's code
1489 % is performed within the evaluation of a boolean expression,
1490 % and skipping out of that would break horribly.
1491 % The \cs{@@_wrap_item:n} function inserts the relevant
1492 % \cs{@@_item:n} without expansion in the input stream,
1493 % hence in the \texttt{x}-expanding assignment.
1494 % \begin{macrocode}
1495 \cs_new_protected:Npn \seq_set_filter:NNn
1496 { \@@_set_filter:NNNn \__kernel_tl_set:Nx }
1497 \cs_new_protected:Npn \seq_gset_filter:NNn
1498 { \@@_set_filter:NNNn \__kernel_tl_gset:Nx }
1499 \cs_new_protected:Npn \@@_set_filter:NNNn #1#2#3#4
1500 {
1501 \@@_push_item_def:n { \bool_if:nT {#4} { \@@_wrap_item:n {##1} } }
1502 #1 #2 { #3 }
1503 \@@_pop_item_def:
1504 }
1505 % \end{macrocode}
1506 % \end{macro}
1507 % \end{macro}
1508 %
1509 % \begin{macro}
1510 % {
1511 % \seq_set_regex_extract_once:Nnn, \seq_set_regex_extract_once:cnn,
1512 % \seq_gset_regex_extract_once:Nnn, \seq_gset_regex_extract_once:cnn
1513 % }
1514 % \begin{macro}
1515 % {
1516 % \seq_set_regex_extract_all:Nnn, \seq_set_regex_extract_all:cnn,
1517 % \seq_gset_regex_extract_all:Nnn, \seq_gset_regex_extract_all:cnn
1518 % }
1519 % \begin{macro}
1520 % {
1521 % \seq_set_regex_extract_once:NNn, \seq_set_regex_extract_once:cNn,
1522 % \seq_gset_regex_extract_once:NNn, \seq_gset_regex_extract_once:cNn
1523 % }
1524 % \begin{macro}
1525 % {
1526 % \seq_set_regex_extract_all:NNn, \seq_set_regex_extract_all:cNn,
1527 % \seq_gset_regex_extract_all:NNn, \seq_gset_regex_extract_all:cNn
1528 % }
1529 % \begin{macro}
1530 % {
1531 % \seq_set_regex_split:Nnn, \seq_set_regex_split:cnn,
1532 % \seq_gset_regex_split:Nnn, \seq_gset_regex_split:cnn
1533 % }
1534 % \begin{macro}
1535 % {
1536 % \seq_set_regex_split:NNn, \seq_set_regex_split:cNn,
1537 % \seq_gset_regex_split:NNn, \seq_gset_regex_split:cNn
1538 % }
1539 % \begin{macrocode}
1540 \cs_new_protected:Npn \seq_set_regex_extract_once:Nnn #1#2#3
1541 { \regex_extract_once:nnN {#2} {#3} #1 }
1542 \cs_generate_variant:Nn \seq_set_regex_extract_once:Nnn { c }
1543 \cs_new_protected:Npn \seq_set_regex_extract_once:NNn #1#2#3
1544 { \regex_extract_once:NnN #2 {#3} #1 }
1545 \cs_generate_variant:Nn \seq_set_regex_extract_once:NNn { c }
1546 \cs_new_protected:Npn \seq_set_regex_extract_all:Nnn #1#2#3
1547 { \regex_extract_all:nnN {#2} {#3} #1 }
1548 \cs_generate_variant:Nn \seq_set_regex_extract_all:Nnn { c }
1549 \cs_new_protected:Npn \seq_set_regex_extract_all:NNn #1#2#3
1550 { \regex_extract_all:NnN #2 {#3} #1 }
1551 \cs_generate_variant:Nn \seq_set_regex_extract_all:NNn { c }
1552 \cs_new_protected:Npn \seq_set_regex_split:Nnn #1#2#3
1553 { \regex_split:nnN {#2} {#3} #1 }
1554 \cs_generate_variant:Nn \seq_set_regex_split:Nnn { c }
1555 \cs_new_protected:Npn \seq_set_regex_split:NNn #1#2#3
1556 { \regex_split:NnN #2 {#3} #1 }
1557 \cs_generate_variant:Nn \seq_set_regex_split:NNn { c }
1558 \group_begin:
1559 \cs_set_protected:Npn \@@_tmp:w #1#2#3
1560 {
1561 \cs_new_protected:cpe { seq_gset_regex_ #1 :N #2 n } ##1##2##3
1562 {
1563 \group_begin:
1564 \seq_set_eq:NN \exp_not:N \l_@@_tmp_seq ##1
1565 \exp_not:c { regex_ #1 :Nn #2 }
1566 #3 {##2} {##3} \exp_not:N \l_@@_tmp_seq
1567 \seq_gset_eq:NN ##1 \exp_not:N \l_@@_tmp_seq
1568 \group_end:
1569 }
1570 \cs_generate_variant:cn { seq_gset_regex_ #1 :N #2 n } { c }
1571 }
1572 \@@_tmp:w { extract_once } n { }
1573 \@@_tmp:w { extract_once } N \use:n
1574 \@@_tmp:w { extract_all } n { }
1575 \@@_tmp:w { extract_all } N \use:n
1576 \@@_tmp:w { split } n { }
1577 \@@_tmp:w { split } N \use:n
1578 \group_end:
1579 % \end{macrocode}
1580 % \end{macro}
1581 % \end{macro}
1582 % \end{macro}
1583 % \end{macro}
1584 % \end{macro}
1585 % \end{macro}
1586 %
1587 % \begin{macro}{\seq_concat:NNN, \seq_concat:ccc}
1588 % \UnitTested
1589 % \begin{macro}{\seq_gconcat:NNN, \seq_gconcat:ccc}
1590 % \UnitTested
1591 % When concatenating sequences, one must remove the leading \cs{s_@@}
1592 % of the second sequence. The result starts with \cs{s_@@} (of the
1593 % first sequence), which stops \texttt{f}-expansion.
1594 % \begin{macrocode}
1595 \cs_new_protected:Npn \seq_concat:NNN #1#2#3
1596 { \tl_set:Nf #1 { \exp_after:wN \use_i:nn \exp_after:wN #2 #3 } }
1597 \cs_new_protected:Npn \seq_gconcat:NNN #1#2#3
1598 { \tl_gset:Nf #1 { \exp_after:wN \use_i:nn \exp_after:wN #2 #3 } }
1599 \cs_generate_variant:Nn \seq_concat:NNN { ccc }
1600 \cs_generate_variant:Nn \seq_gconcat:NNN { ccc }
1601 % \end{macrocode}
1602 % \end{macro}
1603 % \end{macro}
1604 %
1605 % \begin{macro}[pTF]{\seq_if_exist:N, \seq_if_exist:c}
1606 % Copies of the \texttt{cs} functions defined in \pkg{l3basics}.
1607 % \begin{macrocode}
1608 \prg_new_eq_conditional:NNn \seq_if_exist:N \cs_if_exist:N
1609 { TF , T , F , p }
1610 \prg_new_eq_conditional:NNn \seq_if_exist:c \cs_if_exist:c
1611 { TF , T , F , p }
1612 % \end{macrocode}
1613 % \end{macro}
1614 %
1615 % \subsection{Appending data to either end}
1616 %
1617 % \begin{macro}{
1618 % \seq_put_left:Nn, \seq_put_left:NV, \seq_put_left:Nv, \seq_put_left:Ne,
1619 % \seq_put_left:No, \seq_put_left:Nx,
1620 % \seq_put_left:cn, \seq_put_left:cV, \seq_put_left:cv,\seq_put_left:ce,
1621 % \seq_put_left:co, \seq_put_left:cx
1622 % }
1623 % \UnitTested
1624 % \begin{macro}{
1625 % \seq_gput_left:Nn, \seq_gput_left:NV, \seq_gput_left:Nv, \seq_gput_left:Ne,
1626 % \seq_gput_left:No, \seq_gput_left:Nx,
1627 % \seq_gput_left:cn, \seq_gput_left:cV, \seq_gput_left:cv, \seq_gput_left:ce,
1628 % \seq_gput_left:co, \seq_gput_left:cx
1629 % }
1630 % \begin{macro}[EXP]{\@@_put_left_aux:w}
1631 % When adding to the left of a sequence, remove \cs{s_@@}. This is
1632 % done by \cs{@@_put_left_aux:w}, which also stops
1633 % \texttt{f}-expansion.
1634 % \begin{macrocode}
1635 \cs_new_protected:Npn \seq_put_left:Nn #1#2
1636 {
1637 \__kernel_tl_set:Nx #1
1638 {
1639 \exp_not:n { \s_@@ \@@_item:n {#2} }
1640 \exp_not:f { \exp_after:wN \@@_put_left_aux:w #1 }
1641 }
1642 }
1643 \cs_new_protected:Npn \seq_gput_left:Nn #1#2
1644 {
1645 \__kernel_tl_gset:Nx #1
1646 {
1647 \exp_not:n { \s_@@ \@@_item:n {#2} }
1648 \exp_not:f { \exp_after:wN \@@_put_left_aux:w #1 }
1649 }
1650 }
1651 \cs_new:Npn \@@_put_left_aux:w \s_@@ { \exp_stop_f: }
1652 \cs_generate_variant:Nn \seq_put_left:Nn { NV , Nv , Ne , No , Nx }
1653 \cs_generate_variant:Nn \seq_put_left:Nn { c , cV , cv , ce , co ,cx }
1654 \cs_generate_variant:Nn \seq_gput_left:Nn { NV , Nv , Ne , No , Nx }
1655 \cs_generate_variant:Nn \seq_gput_left:Nn { c , cV , cv , ce , co , cx }
1656 % \end{macrocode}
1657 % \end{macro}
1658 % \end{macro}
1659 % \end{macro}
1660 %
1661 % \begin{macro}
1662 % {
1663 % \seq_put_right:Nn, \seq_put_right:NV, \seq_put_right:Nv, \seq_put_right:Ne,
1664 % \seq_put_right:No, \seq_put_right:Nx,
1665 % \seq_put_right:cn, \seq_put_right:cV, \seq_put_right:cv, \seq_put_right:cx,
1666 % \seq_put_right:co, \seq_put_right:cx
1667 % }
1668 % \UnitTested
1669 % \begin{macro}
1670 % {
1671 % \seq_gput_right:Nn, \seq_gput_right:NV, \seq_gput_right:Nv, \seq_gput_right:Ne,
1672 % \seq_gput_right:No, \seq_gput_right:Nx,
1673 % \seq_gput_right:cn, \seq_gput_right:cV, \seq_gput_right:cv, \seq_gput_right:ce,
1674 % \seq_gput_right:co, \seq_gput_right:cx
1675 % }
1676 % Since there is no trailing marker, adding an item to the right of a
1677 % sequence simply means wrapping it in \cs{@@_item:n}.
1678 % \begin{macrocode}
1679 \cs_new_protected:Npn \seq_put_right:Nn #1#2
1680 { \tl_put_right:Nn #1 { \@@_item:n {#2} } }
1681 \cs_new_protected:Npn \seq_gput_right:Nn #1#2
1682 { \tl_gput_right:Nn #1 { \@@_item:n {#2} } }
1683 \cs_generate_variant:Nn \seq_put_right:Nn { NV , Nv , Ne , No , Nx }
1684 \cs_generate_variant:Nn \seq_put_right:Nn { c , cV , cv , ce , co , cx }
1685 \cs_generate_variant:Nn \seq_gput_right:Nn { NV , Nv , Ne , No , Nx }
1686 \cs_generate_variant:Nn \seq_gput_right:Nn { c , cV , cv , ce , co , cx }
1687 % \end{macrocode}
1688 % \end{macro}
1689 % \end{macro}
1690 %
1691 % \subsection{Modifying sequences}
1692 %
1693 % \begin{macro}{\@@_wrap_item:n}
1694 % This function converts its argument to a proper sequence item
1695 % in an \texttt{e}- or \texttt{x}-expansion context.
1696 % \begin{macrocode}
1697 \cs_new:Npn \@@_wrap_item:n #1 { \exp_not:n { \@@_item:n {#1} } }
1698 % \end{macrocode}
1699 % \end{macro}
1700 %
1701 % \begin{variable}{\l_@@_tmp_seq}
1702 % An internal sequence for the removal routines.
1703 % \begin{macrocode}
1704 \seq_new:N \l_@@_tmp_seq
1705 % \end{macrocode}
1706 % \end{variable}
1707 %
1708 % \begin{macro}{\seq_remove_duplicates:N, \seq_remove_duplicates:c}
1709 % \UnitTested
1710 % \begin{macro}{\seq_gremove_duplicates:N, \seq_gremove_duplicates:c}
1711 % \UnitTested
1712 % \begin{macro}{\@@_remove_duplicates:NN}
1713 % Removing duplicates means making a new list then copying it.
1714 % \begin{macrocode}
1715 \cs_new_protected:Npn \seq_remove_duplicates:N
1716 { \@@_remove_duplicates:NN \seq_set_eq:NN }
1717 \cs_new_protected:Npn \seq_gremove_duplicates:N
1718 { \@@_remove_duplicates:NN \seq_gset_eq:NN }
1719 \cs_new_protected:Npn \@@_remove_duplicates:NN #1#2
1720 {
1721 \seq_clear:N \l_@@_tmp_seq
1722 \seq_map_inline:Nn #2
1723 {
1724 \seq_if_in:NnF \l_@@_tmp_seq {##1}
1725 { \seq_put_right:Nn \l_@@_tmp_seq {##1} }
1726 }
1727 #1 #2 \l_@@_tmp_seq
1728 }
1729 \cs_generate_variant:Nn \seq_remove_duplicates:N { c }
1730 \cs_generate_variant:Nn \seq_gremove_duplicates:N { c }
1731 % \end{macrocode}
1732 % \end{macro}
1733 % \end{macro}
1734 % \end{macro}
1735 %
1736 % \begin{macro}
1737 % {
1738 % \seq_remove_all:Nn, \seq_remove_all:NV, \seq_remove_all:Ne,
1739 % \seq_remove_all:Nx,
1740 % \seq_remove_all:cn, \seq_remove_all:cV, \seq_remove_all:ce,
1741 % \seq_remove_all:cx,
1742 % }
1743 % \UnitTested
1744 % \begin{macro}
1745 % {
1746 % \seq_gremove_all:Nn, \seq_gremove_all:NV, \seq_gremove_all:Ne,
1747 % \seq_gremove_all:Nx,
1748 % \seq_gremove_all:cn, \seq_gremove_all:cV, \seq_gremove_all:ce,
1749 % \seq_gremove_all:Nx
1750 % }
1751 % \UnitTested
1752 % \begin{macro}{\@@_remove_all_aux:NNn}
1753 % The idea of the code here is to avoid a relatively expensive addition of
1754 % items one at a time to an intermediate sequence.
1755 % The approach taken is therefore similar to
1756 % that in \cs{@@_pop_right:NNN}, using a \enquote{flexible}
1757 % \texttt{x}-type expansion to do most of the work. As \cs{tl_if_eq:nnT}
1758 % is not expandable, a two-part strategy is needed. First, the
1759 % \texttt{x}-type expansion uses \cs{str_if_eq:nnT} to find potential
1760 % matches. If one is found, the expansion is halted and the necessary
1761 % set up takes place to use the \cs{tl_if_eq:NNT} test. The \texttt{x}-type
1762 % is started again, including all of the items copied already. This
1763 % happens repeatedly until the entire sequence has been scanned. The code
1764 % is set up to avoid needing an intermediate scratch list: the lead-off
1765 % \texttt{x}-type expansion (|#1 #2 {#2}|) ensures that nothing is lost.
1766 % \begin{macrocode}
1767 \cs_new_protected:Npn \seq_remove_all:Nn
1768 { \@@_remove_all_aux:NNn \__kernel_tl_set:Nx }
1769 \cs_new_protected:Npn \seq_gremove_all:Nn
1770 { \@@_remove_all_aux:NNn \__kernel_tl_gset:Nx }
1771 \cs_new_protected:Npn \@@_remove_all_aux:NNn #1#2#3
1772 {
1773 \@@_push_item_def:n
1774 {
1775 \str_if_eq:nnT {##1} {#3}
1776 {
1777 \if_false: { \fi: }
1778 \tl_set:Nn \l_@@_tmpb_tl {##1}
1779 #1 #2
1780 { \if_false: } \fi:
1781 \exp_not:o {#2}
1782 \tl_if_eq:NNT \l_@@_tmpa_tl \l_@@_tmpb_tl
1783 { \use_none:nn }
1784 }
1785 \@@_wrap_item:n {##1}
1786 }
1787 \tl_set:Nn \l_@@_tmpa_tl {#3}
1788 #1 #2 {#2}
1789 \@@_pop_item_def:
1790 }
1791 \cs_generate_variant:Nn \seq_remove_all:Nn { NV , Ne , c , cV , ce }
1792 \cs_generate_variant:Nn \seq_remove_all:Nn { Nx , cx }
1793 \cs_generate_variant:Nn \seq_gremove_all:Nn { NV , Ne , c , cV , ce }
1794 \cs_generate_variant:Nn \seq_gremove_all:Nn { Nx , cx }
1795 % \end{macrocode}
1796 % \end{macro}
1797 % \end{macro}
1798 % \end{macro}
1799 %
1800 % \begin{macro}{\@@_int_eval:w}
1801 % Useful to more quickly go through items.
1802 % \begin{macrocode}
1803 \cs_new_eq:NN \@@_int_eval:w \tex_numexpr:D
1804 % \end{macrocode}
1805 % \end{macro}
1806 %
1807 % \begin{macro}[noTF]{\seq_set_item:Nnn, \seq_set_item:cnn, \seq_gset_item:Nnn, \seq_gset_item:cnn}
1808 % \begin{macro}{\@@_set_item:NnnNN, \@@_set_item:nnNNNN, \@@_set_item_false:nnNNNN, \@@_set_item:nNnnNNNN}
1809 % \begin{macro}[rEXP]{\@@_set_item:wn, \@@_set_item_end:w}
1810 % The conditionals are distinguished from the |Nnn| versions by the
1811 % last argument \cs{use_ii:nn} vs \cs{use_i:nn}.
1812 % \begin{macrocode}
1813 \cs_new_protected:Npn \seq_set_item:Nnn #1#2#3
1814 { \@@_set_item:NnnNN #1 {#2} {#3} \__kernel_tl_set:Nx \use_i:nn }
1815 \cs_new_protected:Npn \seq_gset_item:Nnn #1#2#3
1816 { \@@_set_item:NnnNN #1 {#2} {#3} \__kernel_tl_gset:Nx \use_i:nn }
1817 \cs_generate_variant:Nn \seq_set_item:Nnn { c }
1818 \cs_generate_variant:Nn \seq_gset_item:Nnn { c }
1819 \prg_new_protected_conditional:Npnn \seq_set_item:Nnn #1#2#3 { TF , T , F }
1820 { \@@_set_item:NnnNN #1 {#2} {#3} \__kernel_tl_set:Nx \use_ii:nn }
1821 \prg_new_protected_conditional:Npnn \seq_gset_item:Nnn #1#2#3 { TF , T , F }
1822 { \@@_set_item:NnnNN #1 {#2} {#3} \__kernel_tl_gset:Nx \use_ii:nn }
1823 \prg_generate_conditional_variant:Nnn \seq_set_item:Nnn { c } { TF , T , F }
1824 \prg_generate_conditional_variant:Nnn \seq_gset_item:Nnn { c } { TF , T , F }
1825 % \end{macrocode}
1826 % Save the item to be stored and evaluate the position and the sequence
1827 % length only once. Then depending on the sign of the position, check
1828 % that it is not bigger than the length (in absolute value) nor zero.
1829 % \begin{macrocode}
1830 \cs_new_protected:Npn \@@_set_item:NnnNN #1#2#3
1831 {
1832 \tl_set:Nn \l_@@_tmpa_tl { \@@_item:n {#3} }
1833 \exp_args:Nff \@@_set_item:nnNNNN
1834 { \int_eval:n {#2} } { \seq_count:N #1 } #1 \use_none:nn
1835 }
1836 \cs_new_protected:Npn \@@_set_item:nnNNNN #1#2
1837 {
1838 \int_compare:nNnTF {#1} > 0
1839 { \int_compare:nNnF {#1} > {#2} { \@@_set_item:nNnnNNNN { #1 - 1 } } }
1840 {
1841 \int_compare:nNnF {#1} < {-#2}
1842 {
1843 \int_compare:nNnF {#1} = 0
1844 { \@@_set_item:nNnnNNNN { #2 + #1 } }
1845 }
1846 }
1847 \@@_set_item_false:nnNNNN {#1} {#2}
1848 }
1849 % \end{macrocode}
1850 % If the position is not ok, \cs{@@_set_item_false:nnNNNN} calls an
1851 % error or returns \texttt{false} (depending on the \cs{use_i:nn} vs
1852 % \cs{use_ii:nn} argument mentioned above).
1853 % \begin{macrocode}
1854 \cs_new_protected:Npn \@@_set_item_false:nnNNNN #1#2#3#4#5#6
1855 {
1856 #6
1857 {
1858 \msg_error:nneee { seq } { item-too-large }
1859 { \token_to_str:N #3 } {#2} {#1}
1860 }
1861 { \prg_return_false: }
1862 }
1863 % \end{macrocode}
1864 % If the position is ok, \cs{@@_set_item:nNnnNNNN} makes the assignment
1865 % and returns \texttt{true} (in the case of conditionals). Here |#1|
1866 % is an integer expression (position minus one), it needs to be
1867 % evaluated. The sequence |#5| starts with \cs{s_@@} (even if empty),
1868 % which stops the integer expression and is absorbed by it. The
1869 % \cs{if_meaning:w} test is slightly faster than an integer test (but
1870 % only works when testing against zero, hence the offset we chose in
1871 % the position). When we are done skipping items, insert the saved
1872 % item \cs{l_@@_tmpa_tl}. For |put| functions the last argument
1873 % of \cs{@@_set_item_end:w} is \cs{use_none:nn} and it absorbs the
1874 % item |#2| that we are removing: this is only useful for the |pop|
1875 % functions.
1876 % \begin{macrocode}
1877 \cs_new_protected:Npn \@@_set_item:nNnnNNNN #1#2#3#4#5#6#7#8
1878 {
1879 #7 #5
1880 {
1881 \s_@@
1882 \exp_after:wN \@@_set_item:wn
1883 \int_value:w \@@_int_eval:w #1
1884 #5 \s_@@_stop #6
1885 }
1886 #8 { } { \prg_return_true: }
1887 }
1888 \cs_new:Npn \@@_set_item:wn #1 \@@_item:n #2
1889 {
1890 \if_meaning:w 0 #1 \@@_set_item_end:w \fi:
1891 \exp_not:n { \@@_item:n {#2} }
1892 \exp_after:wN \@@_set_item:wn
1893 \int_value:w \@@_int_eval:w #1 - 1 \s_@@
1894 }
1895 \cs_new:Npn \@@_set_item_end:w #1 \exp_not:n #2 #3 \s_@@ #4 \s_@@_stop #5
1896 {
1897 #1
1898 \exp_not:o \l_@@_tmpa_tl
1899 \exp_not:n {#4}
1900 #5 #2
1901 }
1902 % \end{macrocode}
1903 % \end{macro}
1904 % \end{macro}
1905 % \end{macro}
1906 %
1907 % \begin{macro}
1908 % {\seq_reverse:N, \seq_reverse:c, \seq_greverse:N, \seq_greverse:c}
1909 % \begin{macro}{\@@_reverse:NN}
1910 % \begin{macro}[EXP]{\@@_reverse_item:nwn}
1911 % Previously, \cs{seq_reverse:N} was coded by collecting the items
1912 % in reverse order after an \cs{exp_stop_f:} marker.
1913 % \begin{verbatim}
1914 % \cs_new_protected:Npn \seq_reverse:N #1
1915 % {
1916 % \cs_set_eq:NN \@@_item:n \@@_reverse_item:nw
1917 % \tl_set:Nf #2 { #2 \exp_stop_f: }
1918 % }
1919 % \cs_new:Npn \@@_reverse_item:nw #1 #2 \exp_stop_f:
1920 % {
1921 % #2 \exp_stop_f:
1922 % \@@_item:n {#1}
1923 % }
1924 % \end{verbatim}
1925 % At first, this seems optimal, since we can forget about each item
1926 % as soon as it is placed after \cs{exp_stop_f:}. Unfortunately,
1927 % \TeX{}'s usual tail recursion does not take place in this case:
1928 % since the following \cs{@@_reverse_item:nw} only reads
1929 % tokens until \cs{exp_stop_f:}, and never reads the
1930 % |\@@_item:n {#1}| left by the previous call, \TeX{} cannot
1931 % remove that previous call from the stack, and in particular
1932 % must retain the various macro parameters in memory, until the
1933 % end of the replacement text is reached. The stack is thus
1934 % only flushed after all the \cs{@@_reverse_item:nw} are
1935 % expanded. Keeping track of the arguments of all those calls
1936 % uses up a memory quadratic in the length of the sequence.
1937 % \TeX{} can then not cope with more than a few thousand items.
1938 %
1939 % Instead, we collect the items in the argument
1940 % of \cs{exp_not:n}. The previous calls are cleanly removed
1941 % from the stack, and the memory consumption becomes linear.
1942 % \begin{macrocode}
1943 \cs_new_protected:Npn \seq_reverse:N
1944 { \@@_reverse:NN \__kernel_tl_set:Nx }
1945 \cs_new_protected:Npn \seq_greverse:N
1946 { \@@_reverse:NN \__kernel_tl_gset:Nx }
1947 \cs_new_protected:Npn \@@_reverse:NN #1 #2
1948 {
1949 \cs_set_eq:NN \@@_tmp:w \@@_item:n
1950 \cs_set_eq:NN \@@_item:n \@@_reverse_item:nwn
1951 #1 #2 { #2 \exp_not:n { } }
1952 \cs_set_eq:NN \@@_item:n \@@_tmp:w
1953 }
1954 \cs_new:Npn \@@_reverse_item:nwn #1 #2 \exp_not:n #3
1955 {
1956 #2
1957 \exp_not:n { \@@_item:n {#1} #3 }
1958 }
1959 \cs_generate_variant:Nn \seq_reverse:N { c }
1960 \cs_generate_variant:Nn \seq_greverse:N { c }
1961 % \end{macrocode}
1962 % \end{macro}
1963 % \end{macro}
1964 % \end{macro}
1965 %
1966 % \begin{macro}{\seq_sort:Nn, \seq_sort:cn, \seq_gsort:Nn, \seq_gsort:cn}
1967 % Implemented in \pkg{l3sort}.
1968 % \end{macro}
1969 %
1970 % \subsection{Sequence conditionals}
1971 %
1972 % \begin{macro}[pTF]{\seq_if_empty:N, \seq_if_empty:c}
1973 % \UnitTested
1974 % Similar to token lists, we compare with the empty sequence.
1975 % \begin{macrocode}
1976 \prg_new_conditional:Npnn \seq_if_empty:N #1 { p , T , F , TF }
1977 {
1978 \if_meaning:w #1 \c_empty_seq
1979 \prg_return_true:
1980 \else:
1981 \prg_return_false:
1982 \fi:
1983 }
1984 \prg_generate_conditional_variant:Nnn \seq_if_empty:N
1985 { c } { p , T , F , TF }
1986 % \end{macrocode}
1987 % \end{macro}
1988 %
1989 % \begin{macro}{\seq_shuffle:N, \seq_shuffle:c, \seq_gshuffle:N, \seq_gshuffle:c}
1990 % \begin{macro}{\@@_shuffle:NN}
1991 % \begin{macro}{\@@_shuffle_item:n}
1992 % \begin{variable}{\g_@@_tmp_seq}
1993 % We apply the Fisher--Yates shuffle, storing items in \tn{toks}
1994 % registers. We use the primitive \cs{tex_uniformdeviate:D} for
1995 % speed reasons. Its non-uniformity is of order its argument divided
1996 % by $2^{28}$, not too bad for small lists. For sequences with more
1997 % than $13$ elements there are more possible permutations than
1998 % possible seeds ($13!>2^{28}$) so the question of uniformity is
1999 % somewhat moot. The integer variables are declared in \pkg{l3int}:
2000 % load-order issues.
2001 % \begin{macrocode}
2002 \seq_new:N \g_@@_tmp_seq
2003 \cs_new_protected:Npn \seq_shuffle:N { \@@_shuffle:NN \seq_set_eq:NN }
2004 \cs_new_protected:Npn \seq_gshuffle:N { \@@_shuffle:NN \seq_gset_eq:NN }
2005 \cs_new_protected:Npn \@@_shuffle:NN #1#2
2006 {
2007 \int_compare:nNnTF { \seq_count:N #2 } > \c_max_register_int
2008 {
2009 \msg_error:nne { seq } { shuffle-too-large }
2010 { \token_to_str:N #2 }
2011 }
2012 {
2013 \group_begin:
2014 \int_zero:N \l_@@_tmpa_int
2015 \@@_push_item_def:
2016 \cs_gset_eq:NN \@@_item:n \@@_shuffle_item:n
2017 #2
2018 \@@_pop_item_def:
2019 \seq_gclear:N \g_@@_tmp_seq
2020 \int_step_inline:nn \l_@@_tmpa_int
2021 {
2022 \seq_gput_right:Ne \g_@@_tmp_seq
2023 { \tex_the:D \tex_toks:D ##1 }
2024 }
2025 \group_end:
2026 #1 #2 \g_@@_tmp_seq
2027 \seq_gclear:N \g_@@_tmp_seq
2028 }
2029 }
2030 \cs_new_protected:Npn \@@_shuffle_item:n
2031 {
2032 \int_incr:N \l_@@_tmpa_int
2033 \int_set:Nn \l_@@_tmpb_int
2034 { 1 + \tex_uniformdeviate:D \l_@@_tmpa_int }
2035 \tex_toks:D \l_@@_tmpa_int
2036 = \tex_toks:D \l_@@_tmpb_int
2037 \tex_toks:D \l_@@_tmpb_int
2038 }
2039 \cs_generate_variant:Nn \seq_shuffle:N { c }
2040 \cs_generate_variant:Nn \seq_gshuffle:N { c }
2041 % \end{macrocode}
2042 % \end{variable}
2043 % \end{macro}
2044 % \end{macro}
2045 % \end{macro}
2046 %
2047 % \begin{macro}[TF]
2048 % {
2049 % \seq_if_in:Nn, \seq_if_in:NV, \seq_if_in:Nv, \seq_if_in:Ne,
2050 % \seq_if_in:No, \seq_if_in:Nx,
2051 % \seq_if_in:cn, \seq_if_in:cV, \seq_if_in:cv,\seq_if_in:ce,
2052 % \seq_if_in:co, \seq_if_in:cx
2053 % }
2054 % \UnitTested
2055 % \begin{macro}{\@@_if_in:}
2056 % The approach here is to define \cs{@@_item:n} to compare its
2057 % argument with the test sequence. If the two items are equal, the
2058 % mapping is terminated and \cs{group_end:} \cs{prg_return_true:}
2059 % is inserted after skipping over the rest of the recursion. On the
2060 % other hand, if there is no match then the loop breaks, returning
2061 % \cs{prg_return_false:}.
2062 % Everything is inside a group so that \cs{@@_item:n} is preserved
2063 % in nested situations.
2064 % \begin{macrocode}
2065 \prg_new_protected_conditional:Npnn \seq_if_in:Nn #1#2
2066 { T , F , TF }
2067 {
2068 \group_begin:
2069 \tl_set:Nn \l_@@_tmpa_tl {#2}
2070 \cs_set_protected:Npn \@@_item:n ##1
2071 {
2072 \tl_set:Nn \l_@@_tmpb_tl {##1}
2073 \if_meaning:w \l_@@_tmpa_tl \l_@@_tmpb_tl
2074 \exp_after:wN \@@_if_in:
2075 \fi:
2076 }
2077 #1
2078 \group_end:
2079 \prg_return_false:
2080 \prg_break_point:
2081 }
2082 \cs_new:Npn \@@_if_in:
2083 { \prg_break:n { \group_end: \prg_return_true: } }
2084 \prg_generate_conditional_variant:Nnn \seq_if_in:Nn
2085 { NV , Nv , Ne , No , Nx , c , cV , cv , ce , co , cx } { T , F , TF }
2086 % \end{macrocode}
2087 % \end{macro}
2088 % \end{macro}
2089 %
2090 % \subsection{Recovering data from sequences}
2091 %
2092 % \begin{macro}{\@@_pop:NNNN, \@@_pop_TF:NNNN}
2093 % The two \texttt{pop} functions share their emptiness tests. We also
2094 % use a common emptiness test for all branching \texttt{get} and
2095 % \texttt{pop} functions.
2096 % \begin{macrocode}
2097 \cs_new_protected:Npn \@@_pop:NNNN #1#2#3#4
2098 {
2099 \if_meaning:w #3 \c_empty_seq
2100 \tl_set:Nn #4 { \q_no_value }
2101 \else:
2102 #1#2#3#4
2103 \fi:
2104 }
2105 \cs_new_protected:Npn \@@_pop_TF:NNNN #1#2#3#4
2106 {
2107 \if_meaning:w #3 \c_empty_seq
2108 % \tl_set:Nn #4 { \q_no_value }
2109 \prg_return_false:
2110 \else:
2111 #1#2#3#4
2112 \prg_return_true:
2113 \fi:
2114 }
2115 % \end{macrocode}
2116 % \end{macro}
2117 %
2118 % \begin{macro}{\seq_get_left:NN, \seq_get_left:cN}
2119 % \UnitTested
2120 % \begin{macro}{\@@_get_left:wnw}
2121 % Getting an item from the left of a sequence is pretty easy: just
2122 % trim off the first item after \cs{@@_item:n} at the start. We
2123 % append a \cs{q_no_value} item to cover the case of an empty sequence
2124 % \begin{macrocode}
2125 \cs_new_protected:Npn \seq_get_left:NN #1#2
2126 {
2127 \__kernel_tl_set:Nx #2
2128 {
2129 \exp_after:wN \@@_get_left:wnw
2130 #1 \@@_item:n { \q_no_value } \s_@@_stop
2131 }
2132 }
2133 \cs_new:Npn \@@_get_left:wnw #1 \@@_item:n #2#3 \s_@@_stop
2134 { \exp_not:n {#2} }
2135 \cs_generate_variant:Nn \seq_get_left:NN { c }
2136 % \end{macrocode}
2137 % \end{macro}
2138 % \end{macro}
2139 %
2140 % \begin{macro}{\seq_pop_left:NN, \seq_pop_left:cN}
2141 % \UnitTested
2142 % \begin{macro}{\seq_gpop_left:NN, \seq_gpop_left:cN}
2143 % \UnitTested
2144 % \begin{macro}{\@@_pop_left:NNN, \@@_pop_left:wnwNNN}
2145 % The approach to popping an item is pretty similar to that to get
2146 % an item, with the only difference being that the sequence itself has
2147 % to be redefined. This makes it more sensible to use an auxiliary
2148 % function for the local and global cases.
2149 % \begin{macrocode}
2150 \cs_new_protected:Npn \seq_pop_left:NN
2151 { \@@_pop:NNNN \@@_pop_left:NNN \tl_set:Nn }
2152 \cs_new_protected:Npn \seq_gpop_left:NN
2153 { \@@_pop:NNNN \@@_pop_left:NNN \tl_gset:Nn }
2154 \cs_new_protected:Npn \@@_pop_left:NNN #1#2#3
2155 { \exp_after:wN \@@_pop_left:wnwNNN #2 \s_@@_stop #1#2#3 }
2156 \cs_new_protected:Npn \@@_pop_left:wnwNNN
2157 #1 \@@_item:n #2#3 \s_@@_stop #4#5#6
2158 {
2159 #4 #5 { #1 #3 }
2160 \tl_set:Nn #6 {#2}
2161 }
2162 \cs_generate_variant:Nn \seq_pop_left:NN { c }
2163 \cs_generate_variant:Nn \seq_gpop_left:NN { c }
2164 % \end{macrocode}
2165 % \end{macro}
2166 % \end{macro}
2167 % \end{macro}
2168 %
2169 % \begin{macro}{\seq_get_right:NN, \seq_get_right:cN}
2170 % \UnitTested
2171 % \begin{macro}[EXP]{\@@_get_right_loop:nw, \@@_get_right_end:NnN}
2172 % First remove \cs{s_@@} and prepend \cs{q_no_value}. The first
2173 % argument of \cs{@@_get_right_loop:nw} is the last item found, and
2174 % the second argument is empty until the end of the loop, where it is
2175 % code that applies \cs{exp_not:n} to the last item and ends the loop.
2176 % \begin{macrocode}
2177 \cs_new_protected:Npn \seq_get_right:NN #1#2
2178 {
2179 \__kernel_tl_set:Nx #2
2180 {
2181 \exp_after:wN \use_i_ii:nnn
2182 \exp_after:wN \@@_get_right_loop:nw
2183 \exp_after:wN \q_no_value
2184 #1
2185 \@@_get_right_end:NnN \@@_item:n
2186 }
2187 }
2188 \cs_new:Npn \@@_get_right_loop:nw #1#2 \@@_item:n
2189 {
2190 #2 \use_none:n {#1}
2191 \@@_get_right_loop:nw
2192 }
2193 \cs_new:Npn \@@_get_right_end:NnN #1#2#3 { \exp_not:n {#2} }
2194 \cs_generate_variant:Nn \seq_get_right:NN { c }
2195 % \end{macrocode}
2196 % \end{macro}
2197 % \end{macro}
2198 %
2199 % \begin{macro}{\seq_pop_right:NN, \seq_pop_right:cN}
2200 % \UnitTested
2201 % \begin{macro}{\seq_gpop_right:NN, \seq_gpop_right:cN}
2202 % \UnitTested
2203 % \begin{macro}{\@@_pop_right:NNN, \@@_pop_right_loop:nn}
2204 % The approach to popping from the right is a bit more involved, but does
2205 % use some of the same ideas as getting from the right. What is needed is a
2206 % \enquote{flexible length} way to set a token list variable. This is
2207 % supplied by the |{ \if_false: } \fi:| \ldots
2208 % |\if_false: { \fi: }| construct. Using an \texttt{x}-type
2209 % expansion and a \enquote{non-expanding} definition for \cs{@@_item:n},
2210 % the left-most $n - 1$ entries in a sequence of $n$ items are stored
2211 % back in the sequence. That needs a loop of unknown length, hence using the
2212 % strange \cs{if_false:} way of including braces. When the last item
2213 % of the sequence is reached, the closing brace for the assignment is
2214 % inserted, and |\tl_set:Nn #3| is inserted in front of the final
2215 % entry. This therefore does the pop assignment. One more iteration
2216 % is performed, with an empty argument and \cs{use_none:nn}, which
2217 % finally stops the loop.
2218 % \begin{macrocode}
2219 \cs_new_protected:Npn \seq_pop_right:NN
2220 { \@@_pop:NNNN \@@_pop_right:NNN \__kernel_tl_set:Nx }
2221 \cs_new_protected:Npn \seq_gpop_right:NN
2222 { \@@_pop:NNNN \@@_pop_right:NNN \__kernel_tl_gset:Nx }
2223 \cs_new_protected:Npn \@@_pop_right:NNN #1#2#3
2224 {
2225 \cs_set_eq:NN \@@_tmp:w \@@_item:n
2226 \cs_set_eq:NN \@@_item:n \scan_stop:
2227 #1 #2
2228 { \if_false: } \fi: \s_@@
2229 \exp_after:wN \use_i:nnn
2230 \exp_after:wN \@@_pop_right_loop:nn
2231 #2
2232 {
2233 \if_false: { \fi: }
2234 \__kernel_tl_set:Nx #3
2235 }
2236 { } \use_none:nn
2237 \cs_set_eq:NN \@@_item:n \@@_tmp:w
2238 }
2239 \cs_new:Npn \@@_pop_right_loop:nn #1#2
2240 {
2241 #2 { \exp_not:n {#1} }
2242 \@@_pop_right_loop:nn
2243 }
2244 \cs_generate_variant:Nn \seq_pop_right:NN { c }
2245 \cs_generate_variant:Nn \seq_gpop_right:NN { c }
2246 % \end{macrocode}
2247 % \end{macro}
2248 % \end{macro}
2249 % \end{macro}
2250 %
2251 % \begin{macro}[TF]{\seq_get_left:NN, \seq_get_left:cN}
2252 % \begin{macro}[TF]{\seq_get_right:NN, \seq_get_right:cN}
2253 % Getting from the left or right with a check on the results. The
2254 % first argument to \cs{@@_pop_TF:NNNN} is left unused.
2255 % \begin{macrocode}
2256 \prg_new_protected_conditional:Npnn \seq_get_left:NN #1#2 { T , F , TF }
2257 { \@@_pop_TF:NNNN \prg_do_nothing: \seq_get_left:NN #1#2 }
2258 \prg_new_protected_conditional:Npnn \seq_get_right:NN #1#2 { T , F , TF }
2259 { \@@_pop_TF:NNNN \prg_do_nothing: \seq_get_right:NN #1#2 }
2260 \prg_generate_conditional_variant:Nnn \seq_get_left:NN
2261 { c } { T , F , TF }
2262 \prg_generate_conditional_variant:Nnn \seq_get_right:NN
2263 { c } { T , F , TF }
2264 % \end{macrocode}
2265 % \end{macro}
2266 % \end{macro}
2267 %
2268 % \begin{macro}[TF]{\seq_pop_left:NN, \seq_pop_left:cN}
2269 % \begin{macro}[TF]{\seq_gpop_left:NN, \seq_gpop_left:cN}
2270 % \begin{macro}[TF]{\seq_pop_right:NN, \seq_pop_right:cN}
2271 % \begin{macro}[TF]{\seq_gpop_right:NN, \seq_gpop_right:cN}
2272 % More or less the same for popping.
2273 % \begin{macrocode}
2274 \prg_new_protected_conditional:Npnn \seq_pop_left:NN #1#2
2275 { T , F , TF }
2276 { \@@_pop_TF:NNNN \@@_pop_left:NNN \tl_set:Nn #1 #2 }
2277 \prg_new_protected_conditional:Npnn \seq_gpop_left:NN #1#2
2278 { T , F , TF }
2279 { \@@_pop_TF:NNNN \@@_pop_left:NNN \tl_gset:Nn #1 #2 }
2280 \prg_new_protected_conditional:Npnn \seq_pop_right:NN #1#2
2281 { T , F , TF }
2282 { \@@_pop_TF:NNNN \@@_pop_right:NNN \__kernel_tl_set:Nx #1 #2 }
2283 \prg_new_protected_conditional:Npnn \seq_gpop_right:NN #1#2
2284 { T , F , TF }
2285 { \@@_pop_TF:NNNN \@@_pop_right:NNN \__kernel_tl_gset:Nx #1 #2 }
2286 \prg_generate_conditional_variant:Nnn \seq_pop_left:NN { c }
2287 { T , F , TF }
2288 \prg_generate_conditional_variant:Nnn \seq_gpop_left:NN { c }
2289 { T , F , TF }
2290 \prg_generate_conditional_variant:Nnn \seq_pop_right:NN { c }
2291 { T , F , TF }
2292 \prg_generate_conditional_variant:Nnn \seq_gpop_right:NN { c }
2293 { T , F , TF }
2294 % \end{macrocode}
2295 % \end{macro}
2296 % \end{macro}
2297 % \end{macro}
2298 % \end{macro}
2299 %
2300 % \begin{macro}
2301 % {\seq_item:Nn, \seq_item:NV, \seq_item:Ne, \seq_item:cn, \seq_item:cV, \seq_item:ce}
2302 % \begin{macro}{\@@_item:wNn, \@@_item:nN, \@@_item:nwn}
2303 % The idea here is to find the offset of the item from the left, then use
2304 % a loop to grab the correct item. If the resulting offset is too large,
2305 % then the argument delimited by \cs{@@_item:n} is \cs{prg_break:} instead
2306 % of being empty, terminating the loop and returning nothing at all.
2307 % \begin{macrocode}
2308 \cs_new:Npn \seq_item:Nn #1
2309 { \exp_after:wN \@@_item:wNn #1 \s_@@_stop #1 }
2310 \cs_new:Npn \@@_item:wNn \s_@@ #1 \s_@@_stop #2#3
2311 {
2312 \exp_args:Nf \@@_item:nwn
2313 { \exp_args:Nf \@@_item:nN { \int_eval:n {#3} } #2 }
2314 #1
2315 \prg_break: \@@_item:n { }
2316 \prg_break_point:
2317 }
2318 \cs_new:Npn \@@_item:nN #1#2
2319 {
2320 \int_compare:nNnTF {#1} < 0
2321 { \int_eval:n { \seq_count:N #2 + 1 + #1 } }
2322 {#1}
2323 }
2324 \cs_new:Npn \@@_item:nwn #1#2 \@@_item:n #3
2325 {
2326 #2
2327 \int_compare:nNnTF {#1} = 1
2328 { \prg_break:n { \exp_not:n {#3} } }
2329 { \exp_args:Nf \@@_item:nwn { \int_eval:n { #1 - 1 } } }
2330 }
2331 \cs_generate_variant:Nn \seq_item:Nn { NV , Ne , c , cV , ce }
2332 % \end{macrocode}
2333 % \end{macro}
2334 % \end{macro}
2335 %
2336 % \begin{macro}{\seq_rand_item:N, \seq_rand_item:c}
2337 % Importantly, \cs{seq_item:Nn} only evaluates its argument once.
2338 % \begin{macrocode}
2339 \cs_new:Npn \seq_rand_item:N #1
2340 {
2341 \seq_if_empty:NF #1
2342 { \seq_item:Nn #1 { \int_rand:nn { 1 } { \seq_count:N #1 } } }
2343 }
2344 \cs_generate_variant:Nn \seq_rand_item:N { c }
2345 % \end{macrocode}
2346 % \end{macro}
2347 %
2348 % \subsection{Mapping over sequences}
2349 %
2350 % \begin{macro}{\seq_map_break:}
2351 % \UnitTested
2352 % \begin{macro}{\seq_map_break:n}
2353 % \UnitTested
2354 % To break a function, the special token \cs{prg_break_point:Nn} is
2355 % used to find the end of the code. Any ending code is then inserted
2356 % before the return value of \cs{seq_map_break:n} is inserted.
2357 % \begin{macrocode}
2358 \cs_new:Npn \seq_map_break:
2359 { \prg_map_break:Nn \seq_map_break: { } }
2360 \cs_new:Npn \seq_map_break:n
2361 { \prg_map_break:Nn \seq_map_break: }
2362 % \end{macrocode}
2363 % \end{macro}
2364 % \end{macro}
2365 %
2366 % \begin{macro}{\seq_map_function:NN, \seq_map_function:cN}
2367 % \UnitTested
2368 % \begin{macro}[rEXP]{\@@_map_function:Nw}
2369 % The idea here is to apply the code of |#2| to each item in the
2370 % sequence without altering the definition of \cs{@@_item:n}. The
2371 % even-numbered arguments of \cs{@@_map_function:Nw} delimited by
2372 % \cs{@@_item:n} are almost always empty, except
2373 % at the end of the loop where it is \cs{prg_break:}. This allows to
2374 % break the loop without needing to do a (relatively-expensive) quark
2375 % test.
2376 % \begin{macrocode}
2377 \cs_new:Npn \seq_map_function:NN #1#2
2378 {
2379 \exp_after:wN \use_i_ii:nnn
2380 \exp_after:wN \@@_map_function:Nw
2381 \exp_after:wN #2
2382 #1
2383 \prg_break:
2384 \@@_item:n { } \@@_item:n { } \@@_item:n { } \@@_item:n { }
2385 \prg_break_point:
2386 \prg_break_point:Nn \seq_map_break: { }
2387 }
2388 \cs_new:Npn \@@_map_function:Nw #1
2389 #2 \@@_item:n #3
2390 #4 \@@_item:n #5
2391 #6 \@@_item:n #7
2392 #8 \@@_item:n #9
2393 {
2394 #2 #1 {#3}
2395 #4 #1 {#5}
2396 #6 #1 {#7}
2397 #8 #1 {#9}
2398 \@@_map_function:Nw #1
2399 }
2400 \cs_generate_variant:Nn \seq_map_function:NN { c }
2401 % \end{macrocode}
2402 % \end{macro}
2403 % \end{macro}
2404 %
2405 % \begin{macro}{\@@_push_item_def:n, \@@_push_item_def:e}
2406 % \begin{macro}{\@@_push_item_def:}
2407 % \begin{macro}{\@@_pop_item_def:}
2408 % The definition of \cs{@@_item:n} needs to be saved and restored at
2409 % various points within the mapping and manipulation code. That is handled
2410 % here: as always, this approach uses global assignments.
2411 % \begin{macrocode}
2412 \cs_new_protected:Npn \@@_push_item_def:n
2413 {
2414 \@@_push_item_def:
2415 \cs_gset:Npn \@@_item:n ##1
2416 }
2417 \cs_new_protected:Npn \@@_push_item_def:e
2418 {
2419 \@@_push_item_def:
2420 \cs_gset:Npe \@@_item:n ##1
2421 }
2422 \cs_new_protected:Npn \@@_push_item_def:
2423 {
2424 \int_gincr:N \g__kernel_prg_map_int
2425 \cs_gset_eq:cN { @@_map_ \int_use:N \g__kernel_prg_map_int :w }
2426 \@@_item:n
2427 }
2428 \cs_new_protected:Npn \@@_pop_item_def:
2429 {
2430 \cs_gset_eq:Nc \@@_item:n
2431 { @@_map_ \int_use:N \g__kernel_prg_map_int :w }
2432 \int_gdecr:N \g__kernel_prg_map_int
2433 }
2434 % \end{macrocode}
2435 % \end{macro}
2436 % \end{macro}
2437 % \end{macro}
2438 %
2439 % \begin{macro}{\seq_map_inline:Nn, \seq_map_inline:cn}
2440 % \UnitTested
2441 % The idea here is that \cs{@@_item:n} is already \enquote{applied} to
2442 % each item in a sequence, and so an in-line mapping is just a case of
2443 % redefining \cs{@@_item:n}.
2444 % \begin{macrocode}
2445 \cs_new_protected:Npn \seq_map_inline:Nn #1#2
2446 {
2447 \@@_push_item_def:n {#2}
2448 #1
2449 \prg_break_point:Nn \seq_map_break: { \@@_pop_item_def: }
2450 }
2451 \cs_generate_variant:Nn \seq_map_inline:Nn { c }
2452 % \end{macrocode}
2453 % \end{macro}
2454 %
2455 % \begin{macro}{\seq_map_tokens:Nn, \seq_map_tokens:cn}
2456 % \begin{macro}{\@@_map_tokens:nw}
2457 % This is based on the function mapping but using the same tricks as
2458 % described for \cs{prop_map_tokens:Nn}. The idea is to remove the leading
2459 % \cs{s_@@} and apply the tokens such that they are safe with the
2460 % break points, hence the \cs{use:n}.
2461 % \begin{macrocode}
2462 \cs_new:Npn \seq_map_tokens:Nn #1#2
2463 {
2464 \exp_last_unbraced:Nno
2465 \use_i:nn { \@@_map_tokens:nw {#2} } #1
2466 \prg_break:
2467 \@@_item:n { } \@@_item:n { } \@@_item:n { } \@@_item:n { }
2468 \prg_break_point:
2469 \prg_break_point:Nn \seq_map_break: { }
2470 }
2471 \cs_generate_variant:Nn \seq_map_tokens:Nn { c }
2472 \cs_new:Npn \@@_map_tokens:nw #1
2473 #2 \@@_item:n #3
2474 #4 \@@_item:n #5
2475 #6 \@@_item:n #7
2476 #8 \@@_item:n #9
2477 {
2478 #2 \use:n {#1} {#3}
2479 #4 \use:n {#1} {#5}
2480 #6 \use:n {#1} {#7}
2481 #8 \use:n {#1} {#9}
2482 \@@_map_tokens:nw {#1}
2483 }
2484 % \end{macrocode}
2485 % \end{macro}
2486 % \end{macro}
2487 %
2488 % \begin{macro}
2489 % {
2490 % \seq_map_variable:NNn, \seq_map_variable:Ncn,
2491 % \seq_map_variable:cNn, \seq_map_variable:ccn
2492 % }
2493 % \UnitTested
2494 % This is just a specialized version of the in-line mapping function,
2495 % using an \texttt{e}-type expansion for the code set up so that the
2496 % number of |#| tokens required is as expected.
2497 % \begin{macrocode}
2498 \cs_new_protected:Npn \seq_map_variable:NNn #1#2#3
2499 {
2500 \@@_push_item_def:e
2501 {
2502 \tl_set:Nn \exp_not:N #2 {##1}
2503 \exp_not:n {#3}
2504 }
2505 #1
2506 \prg_break_point:Nn \seq_map_break: { \@@_pop_item_def: }
2507 }
2508 \cs_generate_variant:Nn \seq_map_variable:NNn { Nc }
2509 \cs_generate_variant:Nn \seq_map_variable:NNn { c , cc }
2510 % \end{macrocode}
2511 % \end{macro}
2512 %
2513 % \begin{macro}{\@@_sep:}
2514 % \begin{macrocode}
2515 \cs_new_eq:NN \@@_sep: \__kernel_int_sep:
2516 % \end{macrocode}
2517 % \end{macro}
2518 %
2519 % \begin{macro}
2520 % {
2521 % \seq_map_indexed_function:NN, \seq_map_indexed_inline:Nn,
2522 % \@@_map_indexed:nNN, \@@_map_indexed:Nw
2523 % }
2524 % Similar to \cs{seq_map_function:NN} but we keep track of the item
2525 % index as a |;|-delimited argument of \cs{@@_map_indexed:Nw}.
2526 % \begin{macrocode}
2527 \cs_new:Npn \seq_map_indexed_function:NN #1#2
2528 {
2529 \@@_map_indexed:NN #1#2
2530 \prg_break_point:Nn \seq_map_break: { }
2531 }
2532 \cs_new_protected:Npn \seq_map_indexed_inline:Nn #1#2
2533 {
2534 \int_gincr:N \g__kernel_prg_map_int
2535 \cs_gset_protected:cpn
2536 { @@_map_ \int_use:N \g__kernel_prg_map_int :w } ##1##2 {#2}
2537 \exp_args:NNc \@@_map_indexed:NN #1
2538 { @@_map_ \int_use:N \g__kernel_prg_map_int :w }
2539 \prg_break_point:Nn \seq_map_break:
2540 { \int_gdecr:N \g__kernel_prg_map_int }
2541 }
2542 \cs_new:Npn \@@_map_indexed:NN #1#2
2543 {
2544 \exp_after:wN \@@_map_indexed:Nw
2545 \exp_after:wN #2
2546 \int_value:w 1
2547 \exp_after:wN \use_i:nn
2548 \exp_after:wN \@@_sep:
2549 #1
2550 \prg_break: \@@_item:n { } \prg_break_point:
2551 }
2552 \cs_new:Npn \@@_map_indexed:Nw #1#2 \@@_sep: #3 \@@_item:n #4
2553 {
2554 #3
2555 #1 {#2} {#4}
2556 \exp_after:wN \@@_map_indexed:Nw
2557 \exp_after:wN #1
2558 \int_value:w \int_eval:w 1 + #2 \@@_sep:
2559 }
2560 % \end{macrocode}
2561 % \end{macro}
2562 %
2563 % \begin{macro}
2564 % {
2565 % \seq_map_pairwise_function:NNN, \seq_map_pairwise_function:NcN,
2566 % \seq_map_pairwise_function:cNN, \seq_map_pairwise_function:ccN
2567 % }
2568 % \begin{macro}
2569 % {
2570 % \@@_map_pairwise_function:wNN, \@@_map_pairwise_function:wNw,
2571 % \@@_map_pairwise_function:Nnnwnn
2572 % }
2573 % The idea is to first expand both sequences, adding the
2574 % usual |{ ? \prg_break: } { }| to the end of each one. This is
2575 % most conveniently done in two steps using an auxiliary function.
2576 % The mapping then throws away the first tokens of |#2| and |#5|,
2577 % which for items in both sequences are \cs{s_@@}
2578 % \cs{@@_item:n}. The function to be mapped are then be applied to
2579 % the two entries. When the code hits the end of one of the
2580 % sequences, the break material stops the entire loop and tidy up.
2581 % This avoids needing to find the count of the two sequences, or
2582 % worrying about which is longer.
2583 % \begin{macrocode}
2584 \cs_new:Npn \seq_map_pairwise_function:NNN #1#2#3
2585 { \exp_after:wN \@@_map_pairwise_function:wNN #2 \s_@@_stop #1 #3 }
2586 \cs_new:Npn \@@_map_pairwise_function:wNN \s_@@ #1 \s_@@_stop #2#3
2587 {
2588 \exp_after:wN \@@_map_pairwise_function:wNw #2 \s_@@_stop #3
2589 #1 { ? \prg_break: } { }
2590 \prg_break_point:
2591 \prg_break_point:Nn \seq_map_break: { }
2592 }
2593 \cs_new:Npn \@@_map_pairwise_function:wNw \s_@@ #1 \s_@@_stop #2
2594 {
2595 \@@_map_pairwise_function:Nnnwnn #2
2596 #1 { ? \prg_break: } { }
2597 \s_@@_stop
2598 }
2599 \cs_new:Npn \@@_map_pairwise_function:Nnnwnn #1#2#3#4 \s_@@_stop #5#6
2600 {
2601 \use_none:n #2
2602 \use_none:n #5
2603 #1 {#3} {#6}
2604 \@@_map_pairwise_function:Nnnwnn #1 #4 \s_@@_stop
2605 }
2606 \cs_generate_variant:Nn \seq_map_pairwise_function:NNN { Nc , c , cc }
2607 % \end{macrocode}
2608 % \end{macro}
2609 % \end{macro}
2610 %
2611 % \begin{macro}{\seq_set_map_e:NNn, \seq_gset_map_e:NNn}
2612 % \begin{macro}{\@@_set_map_e:NNNn}
2613 % Very similar to \cs{seq_set_filter:NNn}. We could actually
2614 % merge the two within a single function, but it would have weird
2615 % semantics.
2616 % \begin{macrocode}
2617 \cs_new_protected:Npn \seq_set_map_e:NNn
2618 { \@@_set_map_e:NNNn \__kernel_tl_set:Nx }
2619 \cs_new_protected:Npn \seq_gset_map_e:NNn
2620 { \@@_set_map_e:NNNn \__kernel_tl_gset:Nx }
2621 \cs_new_protected:Npn \@@_set_map_e:NNNn #1#2#3#4
2622 {
2623 \@@_push_item_def:n { \exp_not:N \@@_item:n {#4} }
2624 #1 #2 { #3 }
2625 \@@_pop_item_def:
2626 }
2627 % \end{macrocode}
2628 % \end{macro}
2629 % \end{macro}
2630 %
2631 % \begin{macro}{\seq_set_map:NNn, \seq_gset_map:NNn}
2632 % \begin{macro}{\@@_set_map:NNNn}
2633 % Similar to \cs{seq_set_map_e:NNn}, but prevents expansion of the
2634 % <inline function>.
2635 % \begin{macrocode}
2636 \cs_new_protected:Npn \seq_set_map:NNn
2637 { \@@_set_map:NNNn \__kernel_tl_set:Nx }
2638 \cs_new_protected:Npn \seq_gset_map:NNn
2639 { \@@_set_map:NNNn \__kernel_tl_gset:Nx }
2640 \cs_new_protected:Npn \@@_set_map:NNNn #1#2#3#4
2641 {
2642 \@@_push_item_def:n { \exp_not:n { \@@_item:n {#4} } }
2643 #1 #2 { #3 }
2644 \@@_pop_item_def:
2645 }
2646 % \end{macrocode}
2647 % \end{macro}
2648 % \end{macro}
2649 %
2650 % \begin{macro}{\seq_count:N, \seq_count:c}
2651 % \begin{macro}{\@@_count:w, \@@_count_end:w}
2652 % Since counting the items in a sequence is quite common, we optimize
2653 % it by grabbing $8$~items at a time and correspondingly adding $8$ to
2654 % an integer expression. At the end of the loop, |#9| is
2655 % \cs{@@_count_end:w} instead of being empty. It removes |8+| and
2656 % instead places the number of \cs{@@_item:n} that \cs{@@_count:w}
2657 % grabbed before reaching the end of the sequence.
2658 % \begin{macrocode}
2659 \cs_new:Npn \seq_count:N #1
2660 {
2661 \int_eval:n
2662 {
2663 \exp_after:wN \use_i:nn
2664 \exp_after:wN \@@_count:w
2665 #1
2666 \@@_count_end:w \@@_item:n 7
2667 \@@_count_end:w \@@_item:n 6
2668 \@@_count_end:w \@@_item:n 5
2669 \@@_count_end:w \@@_item:n 4
2670 \@@_count_end:w \@@_item:n 3
2671 \@@_count_end:w \@@_item:n 2
2672 \@@_count_end:w \@@_item:n 1
2673 \@@_count_end:w \@@_item:n 0
2674 \prg_break_point:
2675 }
2676 }
2677 \cs_new:Npn \@@_count:w
2678 #1 \@@_item:n #2 \@@_item:n #3 \@@_item:n #4 \@@_item:n
2679 #5 \@@_item:n #6 \@@_item:n #7 \@@_item:n #8 #9 \@@_item:n
2680 { #9 8 + \@@_count:w }
2681 \cs_new:Npn \@@_count_end:w 8 + \@@_count:w #1#2 \prg_break_point: {#1}
2682 \cs_generate_variant:Nn \seq_count:N { c }
2683 % \end{macrocode}
2684 % \end{macro}
2685 % \end{macro}
2686 %
2687 % \subsection{Using sequences}
2688 %
2689 % \begin{macro}[EXP]{\seq_use:Nnnn, \seq_use:cnnn}
2690 % \begin{macro}[EXP]
2691 % {\@@_use:NNnNnn, \@@_use_setup:w, \@@_use:nwwwwnwn, \@@_use:nwwn}
2692 % \begin{macro}[EXP]{\seq_use:Nn, \seq_use:cn}
2693 % See \cs{clist_use:Nnnn} for a general explanation. The main
2694 % difference is that we use \cs{@@_item:n} as a delimiter rather than
2695 % commas. We also need to add \cs{@@_item:n} at various places, and
2696 % \cs{s_@@}.
2697 % \begin{macrocode}
2698 \cs_new:Npn \seq_use:Nnnn #1#2#3#4
2699 {
2700 \seq_if_exist:NTF #1
2701 {
2702 \int_case:nnF { \seq_count:N #1 }
2703 {
2704 { 0 } { }
2705 { 1 } { \exp_after:wN \@@_use:NNnNnn #1 ? { } { } }
2706 { 2 } { \exp_after:wN \@@_use:NNnNnn #1 {#2} }
2707 }
2708 {
2709 \exp_after:wN \@@_use_setup:w #1 \@@_item:n
2710 \s_@@_mark { \@@_use:nwwwwnwn {#3} }
2711 \s_@@_mark { \@@_use:nwwn {#4} }
2712 \s_@@_stop { }
2713 }
2714 }
2715 {
2716 \msg_expandable_error:nnn
2717 { kernel } { bad-variable } {#1}
2718 }
2719 }
2720 \cs_generate_variant:Nn \seq_use:Nnnn { c }
2721 \cs_new:Npn \@@_use:NNnNnn #1#2#3#4#5#6 { \exp_not:n { #3 #6 #5 } }
2722 \cs_new:Npn \@@_use_setup:w \s_@@ { \@@_use:nwwwwnwn { } }
2723 \cs_new:Npn \@@_use:nwwwwnwn
2724 #1 \@@_item:n #2 \@@_item:n #3 \@@_item:n #4#5
2725 \s_@@_mark #6#7 \s_@@_stop #8
2726 {
2727 #6 \@@_item:n {#3} \@@_item:n {#4} #5
2728 \s_@@_mark {#6} #7 \s_@@_stop { #8 #1 #2 }
2729 }
2730 \cs_new:Npn \@@_use:nwwn #1 \@@_item:n #2 #3 \s_@@_stop #4
2731 { \exp_not:n { #4 #1 #2 } }
2732 \cs_new:Npn \seq_use:Nn #1#2
2733 { \seq_use:Nnnn #1 {#2} {#2} {#2} }
2734 \cs_generate_variant:Nn \seq_use:Nn { c }
2735 % \end{macrocode}
2736 % \end{macro}
2737 % \end{macro}
2738 % \end{macro}
2739 %
2740 % \subsection{Sequence stacks}
2741 %
2742 % The same functions as for sequences, but with the correct naming.
2743 %
2744 % \begin{macro}{
2745 % \seq_push:Nn, \seq_push:NV, \seq_push:Nv, \seq_push:Ne,
2746 % \seq_push:No, \seq_push:Nx,
2747 % \seq_push:cn, \seq_push:cV, \seq_push:cv, \seq_push:ce,
2748 % \seq_push:co, \seq_push:cx
2749 % }
2750 % \UnitTested
2751 % \begin{macro}{
2752 % \seq_gpush:Nn, \seq_gpush:NV, \seq_gpush:Nv, \seq_gpush:Ne,
2753 % \seq_gpush:No, \seq_gpush:Nx,
2754 % \seq_gpush:cn, \seq_gpush:cV, \seq_gpush:cv, \seq_gpush:ce,
2755 % \seq_gpush:co, \seq_gpush:cx
2756 % }
2757 % \UnitTested
2758 % Pushing to a sequence is the same as adding on the left.
2759 % \begin{macrocode}
2760 \cs_new_eq:NN \seq_push:Nn \seq_put_left:Nn
2761 \cs_generate_variant:Nn \seq_push:Nn { NV , Nv , Ne , c , cV , cv , ce }
2762 \cs_generate_variant:Nn \seq_push:Nn { No , Nx , co , cx }
2763 \cs_new_eq:NN \seq_gpush:Nn \seq_gput_left:Nn
2764 \cs_generate_variant:Nn \seq_gpush:Nn { NV , Nv , Ne , c , cV , cv , ce }
2765 \cs_generate_variant:Nn \seq_gpush:Nn { No , Nx , co , cx }
2766 % \end{macrocode}
2767 % \end{macro}
2768 % \end{macro}
2769 %
2770 % \begin{macro}{\seq_get:NN, \seq_get:cN}
2771 % \UnitTested
2772 % \begin{macro}{\seq_pop:NN, \seq_pop:cN}
2773 % \UnitTested
2774 % \begin{macro}{\seq_gpop:NN, \seq_gpop:cN}
2775 % \UnitTested
2776 % In most cases, getting items from the stack does not need to specify
2777 % that this is from the left. So alias are provided.
2778 % \begin{macrocode}
2779 \cs_new_eq:NN \seq_get:NN \seq_get_left:NN
2780 \cs_new_eq:NN \seq_get:cN \seq_get_left:cN
2781 \cs_new_eq:NN \seq_pop:NN \seq_pop_left:NN
2782 \cs_new_eq:NN \seq_pop:cN \seq_pop_left:cN
2783 \cs_new_eq:NN \seq_gpop:NN \seq_gpop_left:NN
2784 \cs_new_eq:NN \seq_gpop:cN \seq_gpop_left:cN
2785 % \end{macrocode}
2786 % \end{macro}
2787 % \end{macro}
2788 % \end{macro}
2789 %
2790 % \begin{macro}[TF]{\seq_get:NN, \seq_get:cN}
2791 % \begin{macro}[TF]{\seq_pop:NN, \seq_pop:cN}
2792 % \begin{macro}[TF]{\seq_gpop:NN, \seq_gpop:cN}
2793 % More copies.
2794 % \begin{macrocode}
2795 \prg_new_eq_conditional:NNn \seq_get:NN \seq_get_left:NN { T , F , TF }
2796 \prg_new_eq_conditional:NNn \seq_get:cN \seq_get_left:cN { T , F , TF }
2797 \prg_new_eq_conditional:NNn \seq_pop:NN \seq_pop_left:NN { T , F , TF }
2798 \prg_new_eq_conditional:NNn \seq_pop:cN \seq_pop_left:cN { T , F , TF }
2799 \prg_new_eq_conditional:NNn \seq_gpop:NN \seq_gpop_left:NN { T , F , TF }
2800 \prg_new_eq_conditional:NNn \seq_gpop:cN \seq_gpop_left:cN { T , F , TF }
2801 % \end{macrocode}
2802 % \end{macro}
2803 % \end{macro}
2804 % \end{macro}
2805 %
2806 % \subsection{Viewing sequences}
2807 %
2808 % \begin{macro}{\seq_show:N, \seq_show:c, \seq_log:N, \seq_log:c, \@@_show:NN}
2809 % \begin{macro}[rEXP]{\@@_show_validate:nn}
2810 % \UnitTested
2811 % Apply the general \cs{__kernel_chk_tl_type:NnnT}.
2812 % \begin{macrocode}
2813 \cs_new_protected:Npn \seq_show:N { \@@_show:NN \msg_show:nneeee }
2814 \cs_generate_variant:Nn \seq_show:N { c }
2815 \cs_new_protected:Npn \seq_log:N { \@@_show:NN \msg_log:nneeee }
2816 \cs_generate_variant:Nn \seq_log:N { c }
2817 \cs_new_protected:Npn \@@_show:NN #1#2
2818 {
2819 \__kernel_chk_tl_type:NnnT #2 { seq }
2820 {
2821 \s_@@
2822 \exp_after:wN \use_i:nn \exp_after:wN \@@_show_validate:nn #2
2823 \q_recursion_tail \q_recursion_tail \q_recursion_stop
2824 }
2825 {
2826 #1 { seq } { show }
2827 { \token_to_str:N #2 }
2828 { \seq_map_function:NN #2 \msg_show_item:n }
2829 { } { }
2830 }
2831 }
2832 \cs_new:Npn \@@_show_validate:nn #1#2
2833 {
2834 \quark_if_recursion_tail_stop:n {#2}
2835 \@@_wrap_item:n {#2}
2836 \@@_show_validate:nn
2837 }
2838 % \end{macrocode}
2839 % \end{macro}
2840 % \end{macro}
2841 %
2842 % \subsection{Scratch sequences}
2843 %
2844 % \begin{variable}{\l_tmpa_seq, \l_tmpb_seq, \g_tmpa_seq, \g_tmpb_seq}
2845 % Temporary comma list variables.
2846 % \begin{macrocode}
2847 \seq_new:N \l_tmpa_seq
2848 \seq_new:N \l_tmpb_seq
2849 \seq_new:N \g_tmpa_seq
2850 \seq_new:N \g_tmpb_seq
2851 % \end{macrocode}
2852 % \end{variable}
2853 %
2854 % \begin{macrocode}
2855 %</code>
2856 % \end{macrocode}
2857 %
2858 % \end{implementation}
2859 %
2860 % \PrintIndex