-
Notifications
You must be signed in to change notification settings - Fork 178
/
l3seq.dtx
2015 lines (2015 loc) · 73.4 KB
/
l3seq.dtx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
% \iffalse meta-comment
%
%% File: l3seq.dtx Copyright (C) 1990-2017 The LaTeX3 Project
%
% It may be distributed and/or modified under the conditions of the
% LaTeX Project Public License (LPPL), either version 1.3c of this
% license or (at your option) any later version. The latest version
% of this license is in the file
%
% https://www.latex-project.org/lppl.txt
%
% This file is part of the "l3kernel bundle" (The Work in LPPL)
% and all files in that bundle must be distributed together.
%
% -----------------------------------------------------------------------
%
% The development version of the bundle can be found at
%
% https://github.com/latex3/latex3
%
% for those people who are interested.
%
%<*driver>
\documentclass[full,kernel]{l3doc}
\begin{document}
\DocInput{\jobname.dtx}
\end{document}
%</driver>
% \fi
%
% \title{^^A
% The \pkg{l3seq} package\\ Sequences and stacks^^A
% }
%
% \author{^^A
% The \LaTeX3 Project\thanks
% {^^A
% E-mail:
% \href{mailto:latex-team@latex-project.org}
% {latex-team@latex-project.org}^^A
% }^^A
% }
%
% \date{Released 2018/03/05}
%
% \maketitle
%
% \begin{documentation}
%
% \LaTeX3 implements a \enquote{sequence} data type, which contain
% an ordered list of entries which may contain any \meta{balanced text}.
% It is possible to map functions to sequences such that the function
% is applied to every item in the sequence.
%
% Sequences are also used to implement stack functions in \LaTeX3. This
% is achieved using a number of dedicated stack functions.
%
% \section{Creating and initialising sequences}
%
% \begin{function}{\seq_new:N, \seq_new:c}
% \begin{syntax}
% \cs{seq_new:N} \meta{sequence}
% \end{syntax}
% Creates a new \meta{sequence} or raises an error if the name is
% already taken. The declaration is global. The \meta{sequence}
% initially contains no items.
% \end{function}
%
% \begin{function}{\seq_clear:N, \seq_clear:c, \seq_gclear:N, \seq_gclear:c}
% \begin{syntax}
% \cs{seq_clear:N} \meta{sequence}
% \end{syntax}
% Clears all items from the \meta{sequence}.
% \end{function}
%
% \begin{function}
% {\seq_clear_new:N, \seq_clear_new:c, \seq_gclear_new:N, \seq_gclear_new:c}
% \begin{syntax}
% \cs{seq_clear_new:N} \meta{sequence}
% \end{syntax}
% Ensures that the \meta{sequence} exists globally by applying
% \cs{seq_new:N} if necessary, then applies
% \cs[index=seq_clear:N]{seq_(g)clear:N} to leave
% the \meta{sequence} empty.
% \end{function}
%
% \begin{function}
% {
% \seq_set_eq:NN, \seq_set_eq:cN, \seq_set_eq:Nc, \seq_set_eq:cc,
% \seq_gset_eq:NN, \seq_gset_eq:cN, \seq_gset_eq:Nc, \seq_gset_eq:cc
% }
% \begin{syntax}
% \cs{seq_set_eq:NN} \meta{sequence_1} \meta{sequence_2}
% \end{syntax}
% Sets the content of \meta{sequence_1} equal to that of
% \meta{sequence_2}.
% \end{function}
%
% \begin{function}[added = 2014-07-17]
% {
% \seq_set_from_clist:NN, \seq_set_from_clist:cN,
% \seq_set_from_clist:Nc, \seq_set_from_clist:cc,
% \seq_set_from_clist:Nn, \seq_set_from_clist:cn,
% \seq_gset_from_clist:NN, \seq_gset_from_clist:cN,
% \seq_gset_from_clist:Nc, \seq_gset_from_clist:cc,
% \seq_gset_from_clist:Nn, \seq_gset_from_clist:cn
% }
% \begin{syntax}
% \cs{seq_set_from_clist:NN} \meta{sequence} \meta{comma-list}
% \end{syntax}
% Converts the data in the \meta{comma list} into a \meta{sequence}:
% the original \meta{comma list} is unchanged.
% \end{function}
%
% \begin{function}[added = 2011-08-15, updated = 2012-07-02]
% {
% \seq_set_split:Nnn , \seq_set_split:NnV ,
% \seq_gset_split:Nnn, \seq_gset_split:NnV
% }
% \begin{syntax}
% \cs{seq_set_split:Nnn} \meta{sequence} \Arg{delimiter} \Arg{token list}
% \end{syntax}
% Splits the \meta{token list} into \meta{items} separated
% by \meta{delimiter}, and assigns the result to the \meta{sequence}.
% Spaces on both sides of each \meta{item} are ignored,
% then one set of outer braces is removed (if any);
% this space trimming behaviour is identical to that of
% \pkg{l3clist} functions. Empty \meta{items} are preserved by
% \cs{seq_set_split:Nnn}, and can be removed afterwards using
% \cs{seq_remove_all:Nn} \meta{sequence} \Arg{}.
% The \meta{delimiter} may not contain |{|, |}| or |#|
% (assuming \TeX{}'s normal category code r\'egime).
% If the \meta{delimiter} is empty, the \meta{token list} is split
% into \meta{items} as a \meta{token list}.
% \end{function}
%
% \begin{function}
% {\seq_concat:NNN, \seq_concat:ccc, \seq_gconcat:NNN, \seq_gconcat:ccc}
% \begin{syntax}
% \cs{seq_concat:NNN} \meta{sequence_1} \meta{sequence_2} \meta{sequence_3}
% \end{syntax}
% Concatenates the content of \meta{sequence_2} and \meta{sequence_3}
% together and saves the result in \meta{sequence_1}. The items in
% \meta{sequence_2} are placed at the left side of the new sequence.
% \end{function}
%
% \begin{function}[EXP, pTF, added=2012-03-03]
% {\seq_if_exist:N, \seq_if_exist:c}
% \begin{syntax}
% \cs{seq_if_exist_p:N} \meta{sequence}
% \cs{seq_if_exist:NTF} \meta{sequence} \Arg{true code} \Arg{false code}
% \end{syntax}
% Tests whether the \meta{sequence} is currently defined. This does not
% check that the \meta{sequence} really is a sequence variable.
% \end{function}
%
% \section{Appending data to sequences}
%
% \begin{function}{
% \seq_put_left:Nn, \seq_put_left:NV, \seq_put_left:Nv,
% \seq_put_left:No, \seq_put_left:Nx,
% \seq_put_left:cn, \seq_put_left:cV, \seq_put_left:cv,
% \seq_put_left:co, \seq_put_left:cx,
% \seq_gput_left:Nn, \seq_gput_left:NV, \seq_gput_left:Nv,
% \seq_gput_left:No, \seq_gput_left:Nx,
% \seq_gput_left:cn, \seq_gput_left:cV, \seq_gput_left:cv,
% \seq_gput_left:co, \seq_gput_left:cx
% }
% \begin{syntax}
% \cs{seq_put_left:Nn} \meta{sequence} \Arg{item}
% \end{syntax}
% Appends the \meta{item} to the left of the \meta{sequence}.
% \end{function}
%
% \begin{function}{
% \seq_put_right:Nn, \seq_put_right:NV, \seq_put_right:Nv,
% \seq_put_right:No, \seq_put_right:Nx,
% \seq_put_right:cn, \seq_put_right:cV, \seq_put_right:cv,
% \seq_put_right:co, \seq_put_right:cx,
% \seq_gput_right:Nn, \seq_gput_right:NV, \seq_gput_right:Nv,
% \seq_gput_right:No, \seq_gput_right:Nx,
% \seq_gput_right:cn, \seq_gput_right:cV, \seq_gput_right:cv,
% \seq_gput_right:co, \seq_gput_right:cx
% }
% \begin{syntax}
% \cs{seq_put_right:Nn} \meta{sequence} \Arg{item}
% \end{syntax}
% Appends the \meta{item} to the right of the \meta{sequence}.
% \end{function}
%
% \section{Recovering items from sequences}
%
% Items can be recovered from either the left or the right of sequences.
% For implementation reasons, the actions at the left of the sequence are
% faster than those acting on the right. These functions all assign the
% recovered material locally, \emph{i.e.}~setting the
% \meta{token list variable} used with \cs{tl_set:Nn} and \emph{never}
% \cs{tl_gset:Nn}.
%
% \begin{function}[updated = 2012-05-14]{\seq_get_left:NN, \seq_get_left:cN}
% \begin{syntax}
% \cs{seq_get_left:NN} \meta{sequence} \meta{token list variable}
% \end{syntax}
% Stores the left-most item from a \meta{sequence} in the
% \meta{token list variable} without removing it from the
% \meta{sequence}. The \meta{token list variable} is assigned locally.
% If \meta{sequence} is empty the \meta{token list variable}
% is set to the special marker \cs{q_no_value}.
% \end{function}
%
% \begin{function}[updated = 2012-05-19]{\seq_get_right:NN, \seq_get_right:cN}
% \begin{syntax}
% \cs{seq_get_right:NN} \meta{sequence} \meta{token list variable}
% \end{syntax}
% Stores the right-most item from a \meta{sequence} in the
% \meta{token list variable} without removing it from the
% \meta{sequence}. The \meta{token list variable} is assigned locally.
% If \meta{sequence} is empty the \meta{token list variable}
% is set to the special marker \cs{q_no_value}.
% \end{function}
%
% \begin{function}[updated = 2012-05-14]{\seq_pop_left:NN, \seq_pop_left:cN}
% \begin{syntax}
% \cs{seq_pop_left:NN} \meta{sequence} \meta{token list variable}
% \end{syntax}
% Pops the left-most item from a \meta{sequence} into the
% \meta{token list variable}, \emph{i.e.}~removes the item from the
% sequence and stores it in the \meta{token list variable}.
% Both of the variables are assigned locally. If \meta{sequence} is
% empty the \meta{token list variable} is set to
% the special marker \cs{q_no_value}.
% \end{function}
%
% \begin{function}[updated = 2012-05-14]{\seq_gpop_left:NN, \seq_gpop_left:cN}
% \begin{syntax}
% \cs{seq_gpop_left:NN} \meta{sequence} \meta{token list variable}
% \end{syntax}
% Pops the left-most item from a \meta{sequence} into the
% \meta{token list variable}, \emph{i.e.}~removes the item from the
% sequence and stores it in the \meta{token list variable}.
% The \meta{sequence} is modified globally, while the assignment of
% the \meta{token list variable} is local.
% If \meta{sequence} is empty the \meta{token list variable} is set to
% the special marker \cs{q_no_value}.
% \end{function}
%
% \begin{function}[updated = 2012-05-19]{\seq_pop_right:NN, \seq_pop_right:cN}
% \begin{syntax}
% \cs{seq_pop_right:NN} \meta{sequence} \meta{token list variable}
% \end{syntax}
% Pops the right-most item from a \meta{sequence} into the
% \meta{token list variable}, \emph{i.e.}~removes the item from the
% sequence and stores it in the \meta{token list variable}.
% Both of the variables are assigned locally. If \meta{sequence} is
% empty the \meta{token list variable} is set to
% the special marker \cs{q_no_value}.
% \end{function}
%
% \begin{function}[updated = 2012-05-19]{\seq_gpop_right:NN, \seq_gpop_right:cN}
% \begin{syntax}
% \cs{seq_gpop_right:NN} \meta{sequence} \meta{token list variable}
% \end{syntax}
% Pops the right-most item from a \meta{sequence} into the
% \meta{token list variable}, \emph{i.e.}~removes the item from the
% sequence and stores it in the \meta{token list variable}.
% The \meta{sequence} is modified globally, while the assignment of
% the \meta{token list variable} is local.
% If \meta{sequence} is empty the \meta{token list variable} is set to
% the special marker \cs{q_no_value}.
% \end{function}
%
% \begin{function}[added = 2014-07-17, EXP]{\seq_item:Nn, \seq_item:cn}
% \begin{syntax}
% \cs{seq_item:Nn} \meta{sequence} \Arg{integer expression}
% \end{syntax}
% Indexing items in the \meta{sequence} from~$1$ at the top (left), this
% function evaluates the \meta{integer expression} and leaves the
% appropriate item from the sequence in the input stream. If the
% \meta{integer expression} is negative, indexing occurs from the
% bottom (right) of the sequence. If the \meta{integer expression}
% is larger than the number of items in the \meta{sequence} (as
% calculated by \cs{seq_count:N}) then the function expands to
% nothing.
% \begin{texnote}
% The result is returned within the \tn{unexpanded}
% primitive (\cs{exp_not:n}), which means that the \meta{item}
% does not expand further when appearing in an \texttt{x}-type
% argument expansion.
% \end{texnote}
% \end{function}
%
% \section{Recovering values from sequences with branching}
%
% The functions in this section combine tests for non-empty sequences
% with recovery of an item from the sequence. They offer increased readability
% and performance over separate testing and recovery phases.
%
% \begin{function}[TF, added = 2012-05-14, updated = 2012-05-19]
% {\seq_get_left:NN, \seq_get_left:cN}
% \begin{syntax}
% \cs{seq_get_left:NNTF} \meta{sequence} \meta{token list variable} \Arg{true code} \Arg{false code}
% \end{syntax}
% If the \meta{sequence} is empty, leaves the \meta{false code} in the
% input stream. The value of the \meta{token list variable} is
% not defined in this case and should not be relied upon. If the
% \meta{sequence} is non-empty, stores the left-most item from the
% \meta{sequence}
% in the \meta{token list variable} without removing it from the
% \meta{sequence}, then leaves the \meta{true code} in the input stream.
% The \meta{token list variable} is assigned locally.
% \end{function}
%
% \begin{function}[TF, added = 2012-05-19]
% {\seq_get_right:NN, \seq_get_right:cN}
% \begin{syntax}
% \cs{seq_get_right:NNTF} \meta{sequence} \meta{token list variable} \Arg{true code} \Arg{false code}
% \end{syntax}
% If the \meta{sequence} is empty, leaves the \meta{false code} in the
% input stream. The value of the \meta{token list variable} is
% not defined in this case and should not be relied upon. If the
% \meta{sequence} is non-empty, stores the right-most item from the
% \meta{sequence}
% in the \meta{token list variable} without removing it from the
% \meta{sequence}, then leaves the \meta{true code} in the input stream.
% The \meta{token list variable} is assigned locally.
% \end{function}
%
% \begin{function}[TF, added = 2012-05-14, updated = 2012-05-19]
% {\seq_pop_left:NN, \seq_pop_left:cN}
% \begin{syntax}
% \cs{seq_pop_left:NNTF} \meta{sequence} \meta{token list variable} \Arg{true code} \Arg{false code}
% \end{syntax}
% If the \meta{sequence} is empty, leaves the \meta{false code} in the
% input stream. The value of the \meta{token list variable} is
% not defined in this case and should not be relied upon. If the
% \meta{sequence} is non-empty, pops the left-most item from the
% \meta{sequence}
% in the \meta{token list variable}, \emph{i.e.}~removes the item from the
% \meta{sequence}, then leaves the \meta{true code} in the input stream.
% Both the \meta{sequence} and the \meta{token list variable} are assigned
% locally.
% \end{function}
%
% \begin{function}[TF, added = 2012-05-14, updated = 2012-05-19]
% {\seq_gpop_left:NN, \seq_gpop_left:cN}
% \begin{syntax}
% \cs{seq_gpop_left:NNTF} \meta{sequence} \meta{token list variable} \Arg{true code} \Arg{false code}
% \end{syntax}
% If the \meta{sequence} is empty, leaves the \meta{false code} in the
% input stream. The value of the \meta{token list variable} is
% not defined in this case and should not be relied upon. If the
% \meta{sequence} is non-empty, pops the left-most item from the \meta{sequence}
% in the \meta{token list variable}, \emph{i.e.}~removes the item from the
% \meta{sequence}, then leaves the \meta{true code} in the input stream.
% The \meta{sequence} is modified globally, while the \meta{token list variable}
% is assigned locally.
% \end{function}
%
% \begin{function}[TF, added = 2012-05-19]
% {\seq_pop_right:NN, \seq_pop_right:cN}
% \begin{syntax}
% \cs{seq_pop_right:NNTF} \meta{sequence} \meta{token list variable} \Arg{true code} \Arg{false code}
% \end{syntax}
% If the \meta{sequence} is empty, leaves the \meta{false code} in the
% input stream. The value of the \meta{token list variable} is
% not defined in this case and should not be relied upon. If the
% \meta{sequence} is non-empty, pops the right-most item from the \meta{sequence}
% in the \meta{token list variable}, \emph{i.e.}~removes the item from the
% \meta{sequence}, then leaves the \meta{true code} in the input stream.
% Both the \meta{sequence} and the \meta{token list variable} are assigned
% locally.
% \end{function}
%
% \begin{function}[TF, added = 2012-05-19]
% {\seq_gpop_right:NN, \seq_gpop_right:cN}
% \begin{syntax}
% \cs{seq_gpop_right:NNTF} \meta{sequence} \meta{token list variable} \Arg{true code} \Arg{false code}
% \end{syntax}
% If the \meta{sequence} is empty, leaves the \meta{false code} in the
% input stream. The value of the \meta{token list variable} is
% not defined in this case and should not be relied upon. If the
% \meta{sequence} is non-empty, pops the right-most item from the \meta{sequence}
% in the \meta{token list variable}, \emph{i.e.}~removes the item from the
% \meta{sequence}, then leaves the \meta{true code} in the input stream.
% The \meta{sequence} is modified globally, while the
% \meta{token list variable} is assigned locally.
% \end{function}
%
% \section{Modifying sequences}
%
% While sequences are normally used as ordered lists, it may be
% necessary to modify the content. The functions here may be used
% to update sequences, while retaining the order of the unaffected
% entries.
%
% \begin{function}
% {
% \seq_remove_duplicates:N, \seq_remove_duplicates:c,
% \seq_gremove_duplicates:N, \seq_gremove_duplicates:c
% }
% \begin{syntax}
% \cs{seq_remove_duplicates:N} \meta{sequence}
% \end{syntax}
% Removes duplicate items from the \meta{sequence}, leaving the
% left most copy of each item in the \meta{sequence}. The \meta{item}
% comparison takes place on a token basis, as for \cs{tl_if_eq:nnTF}.
% \begin{texnote}
% This function iterates through every item in the \meta{sequence} and
% does a comparison with the \meta{items} already checked. It is therefore
% relatively slow with large sequences.
% \end{texnote}
% \end{function}
%
% \begin{function}
% {
% \seq_remove_all:Nn , \seq_remove_all:cn,
% \seq_gremove_all:Nn, \seq_gremove_all:cn
% }
% \begin{syntax}
% \cs{seq_remove_all:Nn} \meta{sequence} \Arg{item}
% \end{syntax}
% Removes every occurrence of \meta{item} from the \meta{sequence}.
% The \meta{item} comparison takes place on a token basis, as for
% \cs{tl_if_eq:nnTF}.
% \end{function}
%
% \begin{function}[added = 2014-07-18]
% {
% \seq_reverse:N, \seq_reverse:c,
% \seq_greverse:N, \seq_greverse:c
% }
% \begin{syntax}
% \cs{seq_reverse:N} \meta{sequence}
% \end{syntax}
% Reverses the order of the items stored in the \meta{sequence}.
% \end{function}
%
% \begin{function}[added = 2017-02-06]
% {\seq_sort:Nn, \seq_sort:cn, \seq_gsort:Nn, \seq_gsort:cn}
% \begin{syntax}
% \cs{seq_sort:Nn} \meta{sequence} \Arg{comparison code}
% \end{syntax}
% Sorts the items in the \meta{sequence} according to the
% \meta{comparison code}, and assigns the result to
% \meta{sequence}. The details of sorting comparison are
% described in Section~\ref{sec:l3sort:mech}.
% \end{function}
%
% \section{Sequence conditionals}
%
% \begin{function}[EXP,pTF]{\seq_if_empty:N, \seq_if_empty:c}
% \begin{syntax}
% \cs{seq_if_empty_p:N} \meta{sequence}
% \cs{seq_if_empty:NTF} \meta{sequence} \Arg{true code} \Arg{false code}
% \end{syntax}
% Tests if the \meta{sequence} is empty (containing no items).
% \end{function}
%
% \begin{function}[TF]
% {
% \seq_if_in:Nn, \seq_if_in:NV, \seq_if_in:Nv, \seq_if_in:No, \seq_if_in:Nx,
% \seq_if_in:cn, \seq_if_in:cV, \seq_if_in:cv, \seq_if_in:co, \seq_if_in:cx
% }
% \begin{syntax}
% \cs{seq_if_in:NnTF} \meta{sequence} \Arg{item} \Arg{true code} \Arg{false code}
% \end{syntax}
% Tests if the \meta{item} is present in the \meta{sequence}.
% \end{function}
%
% \section{Mapping to sequences}
%
% \begin{function}[rEXP, updated = 2012-06-29]
% {\seq_map_function:NN, \seq_map_function:cN}
% \begin{syntax}
% \cs{seq_map_function:NN} \meta{sequence} \meta{function}
% \end{syntax}
% Applies \meta{function} to every \meta{item} stored in the
% \meta{sequence}. The \meta{function} will receive one argument for
% each iteration. The \meta{items} are returned from left to right.
% The function \cs{seq_map_inline:Nn} is faster than
% \cs{seq_map_function:NN} for sequences with more than about~$10$
% items.
% \end{function}
%
% \begin{function}[updated = 2012-06-29]
% {\seq_map_inline:Nn, \seq_map_inline:cn}
% \begin{syntax}
% \cs{seq_map_inline:Nn} \meta{sequence} \Arg{inline function}
% \end{syntax}
% Applies \meta{inline function} to every \meta{item} stored
% within the \meta{sequence}. The \meta{inline function} should
% consist of code which will receive the \meta{item} as |#1|.
% The \meta{items} are returned from left to right.
% \end{function}
%
% \begin{function}[updated = 2012-06-29]
% {
% \seq_map_variable:NNn, \seq_map_variable:Ncn,
% \seq_map_variable:cNn, \seq_map_variable:ccn
% }
% \begin{syntax}
% \cs{seq_map_variable:NNn} \meta{sequence} \meta{variable} \Arg{code}
% \end{syntax}
% Stores each \meta{item} of the \meta{sequence} in turn in the (token
% list) \meta{variable} and applies the \meta{code}. The \meta{code}
% will usually make use of the \meta{variable}, but this is not
% enforced. The assignments to the \meta{variable} are local. The
% \meta{items} are returned from left to right.
% \end{function}
%
% \begin{function}[rEXP, updated = 2012-06-29]{\seq_map_break:}
% \begin{syntax}
% \cs{seq_map_break:}
% \end{syntax}
% Used to terminate a \cs[no-index]{seq_map_\ldots} function before all
% entries in the \meta{sequence} have been processed. This
% normally takes place within a conditional statement, for example
% \begin{verbatim}
% \seq_map_inline:Nn \l_my_seq
% {
% \str_if_eq:nnTF { #1 } { bingo }
% { \seq_map_break: }
% {
% % Do something useful
% }
% }
% \end{verbatim}
% Use outside of a \cs[no-index]{seq_map_\ldots} scenario leads to low
% level \TeX{} errors.
% \begin{texnote}
% When the mapping is broken, additional tokens may be inserted
% before further items are taken
% from the input stream. This depends on the design of the mapping
% function.
% \end{texnote}
% \end{function}
%
% \begin{function}[rEXP, updated = 2012-06-29]{\seq_map_break:n}
% \begin{syntax}
% \cs{seq_map_break:n} \Arg{code}
% \end{syntax}
% Used to terminate a \cs[no-index]{seq_map_\ldots} function before all
% entries in the \meta{sequence} have been processed, inserting
% the \meta{code} after the mapping has ended. This
% normally takes place within a conditional statement, for example
% \begin{verbatim}
% \seq_map_inline:Nn \l_my_seq
% {
% \str_if_eq:nnTF { #1 } { bingo }
% { \seq_map_break:n { <code> } }
% {
% % Do something useful
% }
% }
% \end{verbatim}
% Use outside of a \cs[no-index]{seq_map_\ldots} scenario leads to low
% level \TeX{} errors.
% \begin{texnote}
% When the mapping is broken, additional tokens may be inserted
% before the \meta{code} is
% inserted into the input stream.
% This depends on the design of the mapping function.
% \end{texnote}
% \end{function}
%
% \begin{function}[EXP, added = 2012-07-13]{\seq_count:N, \seq_count:c}
% \begin{syntax}
% \cs{seq_count:N} \meta{sequence}
% \end{syntax}
% Leaves the number of items in the \meta{sequence} in the input
% stream as an \meta{integer denotation}. The total number of items
% in a \meta{sequence} includes those which are empty and duplicates,
% \emph{i.e.}~every item in a \meta{sequence} is unique.
% \end{function}
%
% \section{Using the content of sequences directly}
%
% \begin{function}[EXP, added = 2013-05-26]{\seq_use:Nnnn, \seq_use:cnnn}
% \begin{syntax}
% \cs{seq_use:Nnnn} \meta{seq~var} \Arg{separator~between~two} \Arg{separator~between~more~than~two} \Arg{separator~between~final~two}
% \end{syntax}
% Places the contents of the \meta{seq~var} in the input stream, with
% the appropriate \meta{separator} between the items. Namely, if the
% sequence has more than two items, the \meta{separator between more
% than two} is placed between each pair of items except the last,
% for which the \meta{separator between final two} is used. If the
% sequence has exactly two items, then they are placed in the input stream
% separated by the \meta{separator between two}. If the sequence has
% a single item, it is placed in the input stream, and an empty sequence
% produces no output. An error is raised if the variable does
% not exist or if it is invalid.
%
% For example,
% \begin{verbatim}
% \seq_set_split:Nnn \l_tmpa_seq { | } { a | b | c | {de} | f }
% \seq_use:Nnnn \l_tmpa_seq { ~and~ } { ,~ } { ,~and~ }
% \end{verbatim}
% inserts \enquote{\texttt{a, b, c, de, and f}} in the input
% stream. The first separator argument is not used in this case
% because the sequence has more than $2$ items.
% \begin{texnote}
% The result is returned within the \tn{unexpanded}
% primitive (\cs{exp_not:n}), which means that the \meta{items}
% do not expand further when appearing in an \texttt{x}-type
% argument expansion.
% \end{texnote}
% \end{function}
%
% \begin{function}[EXP, added = 2013-05-26]{\seq_use:Nn, \seq_use:cn}
% \begin{syntax}
% \cs{seq_use:Nn} \meta{seq~var} \Arg{separator}
% \end{syntax}
% Places the contents of the \meta{seq~var} in the input stream, with
% the \meta{separator} between the items. If the sequence has
% a single item, it is placed in the input stream with no \meta{separator},
% and an empty sequence produces no output. An error is raised if
% the variable does not exist or if it is invalid.
%
% For example,
% \begin{verbatim}
% \seq_set_split:Nnn \l_tmpa_seq { | } { a | b | c | {de} | f }
% \seq_use:Nn \l_tmpa_seq { ~and~ }
% \end{verbatim}
% inserts \enquote{\texttt{a and b and c and de and f}} in the input
% stream.
% \begin{texnote}
% The result is returned within the \tn{unexpanded}
% primitive (\cs{exp_not:n}), which means that the \meta{items}
% do not expand further when appearing in an \texttt{x}-type
% argument expansion.
% \end{texnote}
% \end{function}
%
% \section{Sequences as stacks}
%
% Sequences can be used as stacks, where data is pushed to and popped
% from the top of the sequence. (The left of a sequence is the top, for
% performance reasons.) The stack functions for sequences are not
% intended to be mixed with the general ordered data functions detailed
% in the previous section: a sequence should either be used as an
% ordered data type or as a stack, but not in both ways.
%
% \begin{function}[updated = 2012-05-14]{\seq_get:NN, \seq_get:cN}
% \begin{syntax}
% \cs{seq_get:NN} \meta{sequence} \meta{token list variable}
% \end{syntax}
% Reads the top item from a \meta{sequence} into the
% \meta{token list variable} without removing it from the
% \meta{sequence}. The \meta{token list variable} is assigned locally.
% If \meta{sequence} is empty the \meta{token list variable} is set to
% the special marker \cs{q_no_value}.
% \end{function}
%
% \begin{function}[updated = 2012-05-14]{\seq_pop:NN, \seq_pop:cN}
% \begin{syntax}
% \cs{seq_pop:NN} \meta{sequence} \meta{token list variable}
% \end{syntax}
% Pops the top item from a \meta{sequence} into the
% \meta{token list variable}. Both of the variables are assigned
% locally. If \meta{sequence} is empty the \meta{token list variable}
% is set to the special marker \cs{q_no_value}.
% \end{function}
%
% \begin{function}[updated = 2012-05-14]{\seq_gpop:NN, \seq_gpop:cN}
% \begin{syntax}
% \cs{seq_gpop:NN} \meta{sequence} \meta{token list variable}
% \end{syntax}
% Pops the top item from a \meta{sequence} into the
% \meta{token list variable}. The \meta{sequence} is modified globally,
% while the \meta{token list variable} is assigned locally. If
% \meta{sequence} is empty the \meta{token list variable} is set to
% the special marker \cs{q_no_value}.
% \end{function}
%
% \begin{function}[TF, added = 2012-05-14, updated = 2012-05-19]{\seq_get:NN, \seq_get:cN}
% \begin{syntax}
% \cs{seq_get:NNTF} \meta{sequence} \meta{token list variable} \Arg{true code} \Arg{false code}
% \end{syntax}
% If the \meta{sequence} is empty, leaves the \meta{false code} in the
% input stream. The value of the \meta{token list variable} is
% not defined in this case and should not be relied upon. If the
% \meta{sequence} is non-empty, stores the top item from a
% \meta{sequence} in the \meta{token list variable} without removing it from
% the \meta{sequence}. The \meta{token list variable} is assigned locally.
% \end{function}
%
% \begin{function}[TF, added = 2012-05-14, updated = 2012-05-19]{\seq_pop:NN, \seq_pop:cN}
% \begin{syntax}
% \cs{seq_pop:NNTF} \meta{sequence} \meta{token list variable} \Arg{true code} \Arg{false code}
% \end{syntax}
% If the \meta{sequence} is empty, leaves the \meta{false code} in the
% input stream. The value of the \meta{token list variable} is
% not defined in this case and should not be relied upon. If the
% \meta{sequence} is non-empty, pops the top item from the
% \meta{sequence} in the \meta{token list variable}, \emph{i.e.}~removes the
% item from the \meta{sequence}. Both the \meta{sequence} and the
% \meta{token list variable} are assigned locally.
% \end{function}
%
% \begin{function}[TF, added = 2012-05-14, updated = 2012-05-19]{\seq_gpop:NN, \seq_gpop:cN}
% \begin{syntax}
% \cs{seq_gpop:NNTF} \meta{sequence} \meta{token list variable} \Arg{true code} \Arg{false code}
% \end{syntax}
% If the \meta{sequence} is empty, leaves the \meta{false code} in the
% input stream. The value of the \meta{token list variable} is
% not defined in this case and should not be relied upon. If the
% \meta{sequence} is non-empty, pops the top item from the \meta{sequence}
% in the \meta{token list variable}, \emph{i.e.}~removes the item from the
% \meta{sequence}. The \meta{sequence} is modified globally, while the
% \meta{token list variable} is assigned locally.
% \end{function}
%
% \begin{function}
% {
% \seq_push:Nn, \seq_push:NV, \seq_push:Nv, \seq_push:No, \seq_push:Nx,
% \seq_push:cn, \seq_push:cV, \seq_push:cv, \seq_push:co, \seq_push:cx,
% \seq_gpush:Nn, \seq_gpush:NV, \seq_gpush:Nv,
% \seq_gpush:No, \seq_gpush:Nx,
% \seq_gpush:cn, \seq_gpush:cV, \seq_gpush:cv,
% \seq_gpush:co, \seq_gpush:cx
% }
% \begin{syntax}
% \cs{seq_push:Nn} \meta{sequence} \Arg{item}
% \end{syntax}
% Adds the \Arg{item} to the top of the \meta{sequence}.
% \end{function}
%
% \section{Sequences as sets}
%
% Sequences can also be used as sets, such that all of their items are
% distinct. Usage of sequences as sets is not currently widespread,
% hence no specific set function is provided. Instead, it is explained
% here how common set operations can be performed by combining several
% functions described in earlier sections. When using sequences to
% implement sets, one should be careful not to rely on the order of
% items in the sequence representing the set.
%
% Sets should not contain several occurrences of a given item. To make
% sure that a \meta{sequence variable} only has distinct items, use
% \cs{seq_remove_duplicates:N} \meta{sequence variable}. This function
% is relatively slow, and to avoid performance issues one should only
% use it when necessary.
%
% Some operations on a set \meta{seq~var} are straightforward. For
% instance, \cs{seq_count:N} \meta{seq~var} expands to the number of
% items, while \cs{seq_if_in:NnTF} \meta{seq~var} \Arg{item} tests if
% the \meta{item} is in the set.
%
% Adding an \meta{item} to a set \meta{seq~var} can be done by appending
% it to the \meta{seq~var} if it is not already in the \meta{seq~var}:
% \begin{quote}\ttfamily
% \cs{seq_if_in:NnF} \meta{seq~var} \Arg{item} \\
% \ \ \{ \cs{seq_put_right:Nn} \meta{seq~var} \Arg{item} \}
% \end{quote}
% Removing an \meta{item} from a set \meta{seq~var} can be done using
% \cs{seq_remove_all:Nn},
% \begin{quote}\ttfamily
% \cs{seq_remove_all:Nn} \meta{seq~var} \Arg{item}
% \end{quote}
%
% The intersection of two sets \meta{seq~var_1} and \meta{seq~var_2} can
% be stored into \meta{seq~var_3} by collecting items of
% \meta{seq~var_1} which are in \meta{seq~var_2}.
% \begin{quote}\ttfamily
% \cs{seq_clear:N} \meta{seq~var_3} \\
% \cs{seq_map_inline:Nn} \meta{seq~var_1} \\
% \ \ \{ \\
% \ \ \ \ \cs{seq_if_in:NnT} \meta{seq~var_2} \{\#1\} \\
% \ \ \ \ \ \ \{ \cs{seq_put_right:Nn} \meta{seq~var_3} \{\#1\} \} \\
% \ \ \}
% \end{quote}
% The code as written here only works if \meta{seq~var_3} is different
% from the other two sequence variables. To cover all cases, items
% should first be collected in a sequence
% |\l__|\meta{pkg}|_internal_seq|, then \meta{seq~var_3} should be set
% equal to this internal sequence. The same remark applies to other set
% functions.
%
% The union of two sets \meta{seq~var_1} and \meta{seq~var_2} can be
% stored into \meta{seq~var_3} through
% \begin{quote}\ttfamily
% \cs{seq_concat:NNN} \meta{seq~var_3} \meta{seq~var_1} \meta{seq~var_2} \\
% \cs{seq_remove_duplicates:N} \meta{seq~var_3}
% \end{quote}
% or by adding items to (a copy of) \meta{seq~var_1} one by one
% \begin{quote}\ttfamily
% \cs{seq_set_eq:NN} \meta{seq~var_3} \meta{seq~var_1} \\
% \cs{seq_map_inline:Nn} \meta{seq~var_2} \\
% \ \ \{ \\
% \ \ \ \ \cs{seq_if_in:NnF} \meta{seq~var_3} \{\#1\} \\
% \ \ \ \ \ \ \{ \cs{seq_put_right:Nn} \meta{seq~var_3} \{\#1\} \} \\
% \ \ \}
% \end{quote}
% The second approach is faster than the first when the \meta{seq~var_2}
% is short compared to \meta{seq~var_1}.
%
% The difference of two sets \meta{seq~var_1} and \meta{seq~var_2} can
% be stored into \meta{seq~var_3} by removing items of the
% \meta{seq~var_2} from (a copy of) the \meta{seq~var_1} one by one.
% \begin{quote}\ttfamily
% \cs{seq_set_eq:NN} \meta{seq~var_3} \meta{seq~var_1} \\
% \cs{seq_map_inline:Nn} \meta{seq~var_2} \\
% \ \ \{ \cs{seq_remove_all:Nn} \meta{seq~var_3} \{\#1\} \}
% \end{quote}
%
% The symmetric difference of two sets \meta{seq~var_1} and
% \meta{seq~var_2} can be stored into \meta{seq~var_3} by computing the
% difference between \meta{seq~var_1} and \meta{seq~var_2} and storing
% the result as |\l__|\meta{pkg}|_internal_seq|, then the difference
% between \meta{seq~var_2} and \meta{seq~var_1}, and finally
% concatenating the two differences to get the symmetric differences.
% \begin{quote}\ttfamily
% \cs{seq_set_eq:NN} |\l__|\meta{pkg}|_internal_seq| \meta{seq~var_1} \\
% \cs{seq_map_inline:Nn} \meta{seq~var_2} \\
% \ \ \{ \cs{seq_remove_all:Nn} |\l__|\meta{pkg}|_internal_seq| \{\#1\} \} \\
% \cs{seq_set_eq:NN} \meta{seq~var_3} \meta{seq~var_2} \\
% \cs{seq_map_inline:Nn} \meta{seq~var_1} \\
% \ \ \{ \cs{seq_remove_all:Nn} \meta{seq~var_3} \{\#1\} \} \\
% \cs{seq_concat:NNN} \meta{seq~var_3} \meta{seq~var_3} |\l__|\meta{pkg}|_internal_seq|
% \end{quote}
%
% \section{Constant and scratch sequences}
%
% \begin{variable}[added = 2012-07-02]{\c_empty_seq}
% Constant that is always empty.
% \end{variable}
%
% \begin{variable}[added = 2012-04-26]{\l_tmpa_seq, \l_tmpb_seq}
% Scratch sequences for local assignment. These are never used by
% the kernel code, and so are safe for use with any \LaTeX3-defined
% function. However, they may be overwritten by other non-kernel
% code and so should only be used for short-term storage.
% \end{variable}
%
% \begin{variable}[added = 2012-04-26]{\g_tmpa_seq, \g_tmpb_seq}
% Scratch sequences for global assignment. These are never used by
% the kernel code, and so are safe for use with any \LaTeX3-defined
% function. However, they may be overwritten by other non-kernel
% code and so should only be used for short-term storage.
% \end{variable}
%
% \section{Viewing sequences}
%
% \begin{function}[updated = 2015-08-01]{\seq_show:N, \seq_show:c}
% \begin{syntax}
% \cs{seq_show:N} \meta{sequence}
% \end{syntax}
% Displays the entries in the \meta{sequence} in the terminal.
% \end{function}
%
% \begin{function}[added = 2014-08-12, updated = 2015-08-01]{\seq_log:N, \seq_log:c}
% \begin{syntax}
% \cs{seq_log:N} \meta{sequence}
% \end{syntax}
% Writes the entries in the \meta{sequence} in the log file.
% \end{function}
%
% \section{Internal sequence functions}
%
% \begin{variable}{\s__seq}
% This scan mark (equal to \cs{scan_stop:}) marks the beginning of a
% sequence variable.
% \end{variable}
%
% \begin{function}[EXP]{\__seq_item:n}
% \begin{syntax}
% \cs{__seq_item:n} \Arg{item}
% \end{syntax}
% The internal token used to begin each sequence entry. If expanded
% outside of a mapping or manipulation function, an error is
% raised. The definition should always be set globally.
% \end{function}
%
% \begin{function}{\__seq_push_item_def:n, \__seq_push_item_def:x}
% \begin{syntax}
% \cs{__seq_push_item_def:n} \Arg{code}
% \end{syntax}
% Saves the definition of \cs{__seq_item:n} and redefines it to
% accept one parameter and expand to \meta{code}. This function
% should always be balanced by use of \cs{__seq_pop_item_def:}.
% \end{function}
%
% \begin{function}{\__seq_pop_item_def:}
% \begin{syntax}
% \cs{__seq_pop_item_def:}
% \end{syntax}
% Restores the definition of \cs{__seq_item:n} most recently saved by
% \cs{__seq_push_item_def:n}. This function should always be used in
% a balanced pair with \cs{__seq_push_item_def:n}.
% \end{function}
%
% \end{documentation}
%
% \begin{implementation}
%
% \section{\pkg{l3seq} implementation}
%
% \TestFiles{m3seq002,m3seq003}
%
% \begin{macrocode}
%<*initex|package>
% \end{macrocode}
%
% \begin{macrocode}
%<@@=seq>
% \end{macrocode}
%
% A sequence is a control sequence whose top-level expansion is of the
% form \enquote{\cs{s_@@} \cs{@@_item:n} \marg{item_1} \ldots
% \cs{@@_item:n} \marg{item_n}}, with a leading scan
% mark followed by $n$~items of the same form. An
% earlier implementation used the structure \enquote{\cs{seq_elt:w}
% \meta{item_1} \cs{seq_elt_end:} \ldots \cs{seq_elt:w} \meta{item_n}
% \cs{seq_elt_end:}}. This allowed rapid searching using a delimited
% function, but was not suitable for items containing |{|, |}| and |#|
% tokens, and also lead to the loss of surrounding braces around items.
%
% \begin{variable}{\s_@@}
% The variable is defined in the \pkg{l3quark} module, loaded later.
% \end{variable}
%
% \begin{macro}{\@@_item:n}
% The delimiter is always defined, but when used incorrectly simply
% removes its argument and hits an undefined control sequence to
% raise an error.
% \begin{macrocode}
\cs_new:Npn \@@_item:n
{
\__kernel_msg_expandable_error:nn { kernel } { misused-sequence }
\use_none:n
}
% \end{macrocode}
% \end{macro}
%
% \begin{variable}{\l_@@_internal_a_tl, \l_@@_internal_b_tl}
% Scratch space for various internal uses.
% \begin{macrocode}
\tl_new:N \l_@@_internal_a_tl
\tl_new:N \l_@@_internal_b_tl
% \end{macrocode}
% \end{variable}
%
% \begin{macro}{\@@_tmp:w}
% Scratch function for internal use.
% \begin{macrocode}
\cs_new_eq:NN \@@_tmp:w ?
% \end{macrocode}
% \end{macro}
%
% \begin{variable}{\c_empty_seq}
% A sequence with no item, following the structure mentioned above.
% \begin{macrocode}
\tl_const:Nn \c_empty_seq { \s_@@ }
% \end{macrocode}
% \end{variable}
%
% \subsection{Allocation and initialisation}
%
% \begin{macro}{\seq_new:N, \seq_new:c}
% \UnitTested
% Sequences are initialized to \cs{c_empty_seq}.
% \begin{macrocode}
\cs_new_protected:Npn \seq_new:N #1
{
\__chk_if_free_cs:N #1
\cs_gset_eq:NN #1 \c_empty_seq
}
\cs_generate_variant:Nn \seq_new:N { c }
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\seq_clear:N, \seq_clear:c}
% \UnitTested
% \begin{macro}{\seq_gclear:N, \seq_gclear:c}
% \UnitTested
% Clearing a sequence is similar to setting it equal to the empty one.
% \begin{macrocode}
\cs_new_protected:Npn \seq_clear:N #1
{ \seq_set_eq:NN #1 \c_empty_seq }
\cs_generate_variant:Nn \seq_clear:N { c }
\cs_new_protected:Npn \seq_gclear:N #1
{ \seq_gset_eq:NN #1 \c_empty_seq }
\cs_generate_variant:Nn \seq_gclear:N { c }
% \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\seq_clear_new:N, \seq_clear_new:c}
% \UnitTested
% \begin{macro}{\seq_gclear_new:N, \seq_gclear_new:c}
% \UnitTested
% Once again we copy code from the token list functions.
% \begin{macrocode}
\cs_new_protected:Npn \seq_clear_new:N #1
{ \seq_if_exist:NTF #1 { \seq_clear:N #1 } { \seq_new:N #1 } }
\cs_generate_variant:Nn \seq_clear_new:N { c }
\cs_new_protected:Npn \seq_gclear_new:N #1
{ \seq_if_exist:NTF #1 { \seq_gclear:N #1 } { \seq_new:N #1 } }
\cs_generate_variant:Nn \seq_gclear_new:N { c }
% \end{macrocode}