[latex3-commits] [git/LaTeX3-latex3-latex3] master: Implement \seq_indexed_map_function/inline (a58d693)
Bruno Le Floch
bruno at le-floch.fr
Thu May 3 19:03:04 CEST 2018
Repository : https://github.com/latex3/latex3
On branch : master
Link : https://github.com/latex3/latex3/commit/a58d693ee655e7541b9614cec3dc5c4fedb5676e
>---------------------------------------------------------------
commit a58d693ee655e7541b9614cec3dc5c4fedb5676e
Author: Bruno Le Floch <bruno at le-floch.fr>
Date: Thu May 3 19:03:04 2018 +0200
Implement \seq_indexed_map_function/inline
Not \seq_indexed_map_variable:NNNn because needing two variables sounds
pretty unwieldy and we don't have the similar \prop_map_variable:NNNn
for the same reason. It's easy to implement if wanted.
>---------------------------------------------------------------
a58d693ee655e7541b9614cec3dc5c4fedb5676e
l3kernel/l3candidates.dtx | 64 +++++++++++++++++++++++++++++++++++++++
l3kernel/testfiles/m3seq008.lvt | 48 +++++++++++++++++++++--------
l3kernel/testfiles/m3seq008.tlg | 64 +++++++++++++++++++++++++++++++--------
3 files changed, 151 insertions(+), 25 deletions(-)
diff --git a/l3kernel/l3candidates.dtx b/l3kernel/l3candidates.dtx
index 9439ba2..7a5be9d 100644
--- a/l3kernel/l3candidates.dtx
+++ b/l3kernel/l3candidates.dtx
@@ -677,6 +677,26 @@
% \end{texnote}
% \end{function}
%
+% \begin{function}[added = 2018-05-03]{\seq_indexed_map_function:NN}
+% \begin{syntax}
+% \cs{seq_indexed_map_function:NN} \meta{seq~var} \meta{function}
+% \end{syntax}
+% Applies \meta{function} to every entry in the \meta{sequence
+% variable}. The \meta{function} should have signature |:nn|. It
+% receives two arguments for each iteration: the \meta{index} (namely
+% |1| for the first entry, then |2| and so on) and the \meta{item}.
+% \end{function}
+%
+% \begin{function}[added = 2018-05-03]{\seq_indexed_map_inline:Nn}
+% \begin{syntax}
+% \cs{seq_indexed_map_inline:Nn} \meta{seq~var} \Arg{inline function}
+% \end{syntax}
+% Applies \meta{inline function} to every entry in the \meta{sequence
+% variable}. The \meta{inline function} should consist of code which
+% receives the \meta{index} (namely |1| for the first entry, then |2|
+% and so on) as~|#1| and the \meta{item} as~|#2|.
+% \end{function}
+%
% \section{Additions to \pkg{l3skip}}
%
% \begin{function}{\skip_split_finite_else_action:nnNN}
@@ -2518,6 +2538,50 @@
% \end{variable}
% \end{macro}
%
+% \begin{macro}
+% {
+% \seq_indexed_map_function:NN, \seq_indexed_map_inline:Nn,
+% \@@_indexed_map:nNN, \@@_indexed_map:Nw
+% }
+% Similar to \cs{seq_map_function:NN} but we keep track of the item
+% index as a |;|-delimited argument of \cs{@@_indexed_map:Nw}.
+% \begin{macrocode}
+\cs_new:Npn \seq_indexed_map_function:NN #1#2
+ {
+ \@@_indexed_map:NN #1#2
+ \prg_break_point:Nn \seq_map_break: { }
+ }
+\cs_new_protected:Npn \seq_indexed_map_inline:Nn #1#2
+ {
+ \int_gincr:N \g__kernel_prg_map_int
+ \cs_gset_protected:cpn
+ { @@_map_ \int_use:N \g__kernel_prg_map_int :w } ##1##2 {#2}
+ \exp_args:NNc \@@_indexed_map:NN #1
+ { @@_map_ \int_use:N \g__kernel_prg_map_int :w }
+ \prg_break_point:Nn \seq_map_break:
+ { \int_gdecr:N \g__kernel_prg_map_int }
+ }
+\cs_new:Npn \@@_indexed_map:NN #1#2
+ {
+ \exp_after:wN \@@_indexed_map:Nw
+ \exp_after:wN #2
+ \int_value:w 1
+ \exp_after:wN \use_i:nn
+ \exp_after:wN ;
+ #1
+ \prg_break: \@@_item:n { } \prg_break_point:
+ }
+\cs_new:Npn \@@_indexed_map:Nw #1#2 ; #3 \@@_item:n #4
+ {
+ #3
+ #1 {#2} {#4}
+ \exp_after:wN \@@_indexed_map:Nw
+ \exp_after:wN #1
+ \int_value:w \int_eval:w 1 + #2 ;
+ }
+% \end{macrocode}
+% \end{macro}
+%
% \subsection{Additions to \pkg{l3skip}}
%
% \begin{macrocode}
diff --git a/l3kernel/testfiles/m3seq008.lvt b/l3kernel/testfiles/m3seq008.lvt
index 4214ecb..6b6a740 100644
--- a/l3kernel/testfiles/m3seq008.lvt
+++ b/l3kernel/testfiles/m3seq008.lvt
@@ -1,5 +1,5 @@
%
-% Copyright (C) 2014 LaTeX3 Project
+% Copyright (C) 2014,2018 LaTeX3 Project
%
\documentclass{minimal}
@@ -12,7 +12,7 @@
\begin{document}
\START
-\AUTHOR{Joseph Wright}
+\AUTHOR{Joseph Wright, Bruno Le Floch}
\ExplSyntaxOn
@@ -23,16 +23,16 @@
\seq_push:Nn \l_tmpa_seq { a }
\seq_push:Nn \l_tmpa_seq { b }
\seq_push:Nn \l_tmpa_seq { c }
- \seq_push:Nn \l_tmpb_seq { A }
- \seq_push:Nn \l_tmpb_seq { B }
- \seq_push:Nn \l_tmpb_seq { C }
- \seq_gpush:Nn \g_tmpa_seq { a }
- \seq_gpush:Nn \g_tmpa_seq { b }
- \seq_gpush:Nn \g_tmpa_seq { c }
- \seq_gpush:Nn \g_tmpa_seq { d }
- \seq_gpush:Nn \g_tmpa_seq { e }
- \seq_gpush:Nn \g_tmpa_seq { f }
- \cs_set:Npn \test:nn #1#2 { \TYPE { "#1":"#2" } \NEWLINE }
+ \seq_push:Nn \l_tmpb_seq { A~ }
+ \seq_push:Nn \l_tmpb_seq { ~B }
+ \seq_push:Nn \l_tmpb_seq { {C} }
+ \seq_gpush:Nn \g_tmpa_seq { \a }
+ \seq_gpush:Nn \g_tmpa_seq { \b }
+ \seq_gpush:Nn \g_tmpa_seq { \c }
+ \seq_gpush:Nn \g_tmpa_seq { \d }
+ \seq_gpush:Nn \g_tmpa_seq { \e }
+ \seq_gpush:Nn \g_tmpa_seq { \f }
+ \cs_set:Npn \test:nn #1#2 { \TYPE { " \tl_to_str:n {#1} ":"\tl_to_str:n {#2}" } \NEWLINE }
\TIMO
\TESTEXP { seq_mapthread_function:NNN }
@@ -52,4 +52,28 @@
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+\TESTEXP { seq_indexed_map_function:NN }
+ {
+ \seq_indexed_map_function:NN \l_tmpa_seq \test:nn
+ \SEPARATOR \NEWLINE
+ \seq_indexed_map_function:NN \l_tmpb_seq \test:nn
+ \SEPARATOR \NEWLINE
+ \seq_indexed_map_function:NN \g_tmpa_seq \test:nn
+ \SEPARATOR \NEWLINE
+ \seq_indexed_map_function:NN \g_tmpb_seq \test:nn
+ }
+
+\TEST { seq_indexed_map_inline:Nn }
+ {
+ \seq_indexed_map_inline:Nn \l_tmpa_seq { \TYPE { \tl_to_str:n { "#1":"#2" } } }
+ \SEPARATOR
+ \seq_indexed_map_inline:Nn \l_tmpb_seq { \TYPE { \tl_to_str:n { "#1":"#2" } } }
+ \SEPARATOR
+ \seq_indexed_map_inline:Nn \g_tmpa_seq { \TYPE { \tl_to_str:n { "#1":"#2" } } }
+ \SEPARATOR
+ \seq_indexed_map_inline:Nn \g_tmpb_seq { \TYPE { \tl_to_str:n { "#1":"#2" } } }
+ }
+
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+
\END
diff --git a/l3kernel/testfiles/m3seq008.tlg b/l3kernel/testfiles/m3seq008.tlg
index f9d1a8f..e6c8452 100644
--- a/l3kernel/testfiles/m3seq008.tlg
+++ b/l3kernel/testfiles/m3seq008.tlg
@@ -1,24 +1,62 @@
This is a generated file for the LaTeX (2e + expl3) validation system.
Don't change this file in any respect.
-Author: Joseph Wright
+Author: Joseph Wright, Bruno Le Floch
============================================================
TEST 1: seq_mapthread_function:NNN
============================================================
-"c":"C"
-"b":"B"
-"a":"A"
+"c":"{C}"
+"b":" B"
+"a":"A "
============================================================
-"C":"c"
-"B":"b"
-"A":"a"
+"{C}":"c"
+" B":"b"
+"A ":"a"
============================================================
-"f":"C"
-"e":"B"
-"d":"A"
+"\f ":"{C}"
+"\e ":" B"
+"\d ":"A "
============================================================
-"C":"f"
-"B":"e"
-"A":"d"
+"{C}":"\f "
+" B":"\e "
+"A ":"\d "
============================================================
============================================================
============================================================
+============================================================
+TEST 2: seq_indexed_map_function:NN
+============================================================
+"1":"c"
+"2":"b"
+"3":"a"
+============================================================
+"1":"{C}"
+"2":" B"
+"3":"A "
+============================================================
+"1":"\f "
+"2":"\e "
+"3":"\d "
+"4":"\c "
+"5":"\b "
+"6":"\a "
+============================================================
+============================================================
+============================================================
+TEST 3: seq_indexed_map_inline:Nn
+============================================================
+"1":"c"
+"2":"b"
+"3":"a"
+============================================================
+"1":"{C}"
+"2":" B"
+"3":"A "
+============================================================
+"1":"\f "
+"2":"\e "
+"3":"\d "
+"4":"\c "
+"5":"\b "
+"6":"\a "
+============================================================
+============================================================
More information about the latex3-commits
mailing list