LaTeX 3 標記列表變數、映射函數、映射內聯和函數之間的差異

LaTeX 3 標記列表變數、映射函數、映射內聯和函數之間的差異

tl_map_function:nN我正在尋找和之間的差異和用例的良好解釋tl_map_inline:Nn。我提供了一個 MWE,它inline似乎也是最快的。

\documentclass{article}
\usepackage{expl3,xparse}

\begin{document}
\ExplSyntaxOn
\cs_new:Nn\whatever:n{..{#1}~}
\tl_new:N  \tl_phd_temp_a
\tl_new:N  \tl_phd_temp_b
\tl_new:N  \tl_phd_temp_c
\DeclareDocumentCommand\Test{ }
    {
       \tl_set:Nn  \tl_phd_temp_a {Yiannis Mary John}
       \tl_set:Nn  \tl_phd_temp_b {{Lazarides}{Lou}{Smith}}
       \tl_concat:NNN \tl_phd_temp_c \tl_phd_temp_a \tl_phd_temp_b
       \tl_map_inline:Nn\tl_phd_temp_c{\whatever:n{##1}}
   }   
\ExplSyntaxOff 
\Test
\end{document}

答案1

函數\tl_map_function:nN\tl_map_function:NN是完全可擴展的(使用x-expansion,而不是f-expansion),而inline版本則不然。

function版本也稍快一些。

範例文件

\documentclass{article}
\usepackage{expl3}

\begin{document}

\ExplSyntaxOn
\cs_new:Nn\whatever:n{}
\tl_new:N  \tl_phd_temp_a
\tl_new:N  \tl_phd_temp_b
\tl_new:N  \tl_phd_temp_c
\tl_set:Nn  \tl_phd_temp_a {Yiannis Mary John}
\tl_set:Nn  \tl_phd_temp_b {{Lazarides}{Lou}{Smith}}
\tl_concat:NNN \tl_phd_temp_c \tl_phd_temp_a \tl_phd_temp_b

\cs_new:Npn \xinline
 {
  \tl_map_inline:Nn\tl_phd_temp_c{\whatever:n{##1}}
  \tl_map_inline:Nn\tl_phd_temp_c{\whatever:n{##1}}
  \tl_map_inline:Nn\tl_phd_temp_c{\whatever:n{##1}}
  \tl_map_inline:Nn\tl_phd_temp_c{\whatever:n{##1}}
  \tl_map_inline:Nn\tl_phd_temp_c{\whatever:n{##1}}
  \tl_map_inline:Nn\tl_phd_temp_c{\whatever:n{##1}}
  \tl_map_inline:Nn\tl_phd_temp_c{\whatever:n{##1}}
  \tl_map_inline:Nn\tl_phd_temp_c{\whatever:n{##1}}
  \tl_map_inline:Nn\tl_phd_temp_c{\whatever:n{##1}}
  \tl_map_inline:Nn\tl_phd_temp_c{\whatever:n{##1}}
 }
\cs_new:Npn \xfunction
 {
  \tl_map_function:NN\tl_phd_temp_c \whatever:n
  \tl_map_function:NN\tl_phd_temp_c \whatever:n
  \tl_map_function:NN\tl_phd_temp_c \whatever:n
  \tl_map_function:NN\tl_phd_temp_c \whatever:n
  \tl_map_function:NN\tl_phd_temp_c \whatever:n
  \tl_map_function:NN\tl_phd_temp_c \whatever:n
  \tl_map_function:NN\tl_phd_temp_c \whatever:n
  \tl_map_function:NN\tl_phd_temp_c \whatever:n
  \tl_map_function:NN\tl_phd_temp_c \whatever:n
  \tl_map_function:NN\tl_phd_temp_c \whatever:n
 }

\cs_new:Npn \y #1
 {
  #1 #1 #1 #1 #1 #1 #1 #1 #1 #1
 }
\cs_new:Npn \z #1
 {
  \y{#1}\y{#1}\y{#1}\y{#1}\y{#1}
  \y{#1}\y{#1}\y{#1}\y{#1}\y{#1}
 }
\cs_new:Npn \zz #1
 {
  \z{#1}\z{#1}\z{#1}\z{#1}\z{#1}
  \z{#1}\z{#1}\z{#1}\z{#1}\z{#1}
 }
\cs_new:Npn \zzz #1
 {
  \zz{#1}\zz{#1}\zz{#1}\zz{#1}\zz{#1}
  \zz{#1}\zz{#1}\zz{#1}\zz{#1}\zz{#1}
 }

\ExplSyntaxOff

\zzz{\xinline}
%\zzz{\xfunction}

\stop

inline版本

這是由time

real    0m4.635s
user    0m4.613s
sys     0m0.016s

這是記憶體報告

 140188 words of memory out of 5000000

function版本

這是由time

real    0m4.163s
user    0m4.139s
sys     0m0.020s

這是記憶體報告

 140188 words of memory out of 5000000

應該優先選擇什麼?我傾向於認為\tl_map_inline:nn更容易,因為它不需要定義新函數,但是當程式碼變得複雜時,function應該使用版本以獲得更好的清晰度。只要要執行的映射使用其他地方定義的函數,就應該採用它。

inline版本使添加斷點變得更加容易;此function版本需要一個專門定制的函數,其中包含\tl_map_break:.

此外,function當映射需要多個參數時,版本會稍微複雜一些(例如,我們處於嵌套映射中,同時使用外部和內部項)。

相關內容