expl3의 루프 처리

expl3의 루프 처리

나는 이것을 내부에서 예상했지만 \clist_map이 코드에서는 그렇지 않았습니다. 아마도 "더미" 질문은...

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\pairwise}{mmm}{
    \clist_set_eq:NN \l_tmpa_clist #1
    \clist_set_eq:NN \l_tmpb_clist #2
    \clist_clear:N #3
    \bool_until_do:nn {\clist_if_empty_p:N \l_tmpa_clist}{
        \clist_pop:NN \l_tmpa_clist \l_tmpa_tl
        \clist_pop:NN \l_tmpb_clist \l_tmpb_tl
        \clist_put_right:Nn #3 {\l_tmpa_tl / \l_tmpb_tl}
    }
}
\ExplSyntaxOff

\begin{document}

\def\this{1,2,3,4,5}
\def\that{a,b,c,d,e}

There is \{\this\} and \{\that\}.

\pairwise{\this}{\that}{\result}
\def\expected{1/a,2/b,3/c,4/d,5/e}

Why \{\result\} not equal \{\expected\}?

\end{document}

답변1

명령은 입력에 \clist_put_right:Nn남습니다 . 즉, 동일성을 확인하기 위해 \l_tmpa_tl호출할 때 포함 하고 포함합니다 .\result\l_tmpa_tl5\l_tmpb_tle

그러나 쌍별 콘텐츠를 얻으려면 및 X/Y의 현재 값을 평가(확장)하고 변수에 저장해야 하므로 다음 유형을 사용하십시오 .\l_tmpa_tl\l_tmpb_tl\clistx

\clist_put_right:Nx #3 {\l_tmpa_tl/\l_tmpb_tl}

목록이 더 빠를 가능성이 높지만 \seq이는 사용 사례에 따라 다릅니다.

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\pairwise}{mmm}{
  \clist_set_eq:NN \l_tmpa_clist #1
  \clist_set_eq:NN \l_tmpb_clist #2
  \clist_clear:N #3
  \bool_until_do:nn {\clist_if_empty_p:N \l_tmpa_clist}{
    \clist_pop:NN \l_tmpa_clist \l_tmpa_tl
    \clist_pop:NN \l_tmpb_clist \l_tmpb_tl
    \clist_put_right:Nx #3 {\l_tmpa_tl / \l_tmpb_tl}
  }
}
\ExplSyntaxOff

\begin{document}

\def\this{1,2,3,4,5}
\def\that{a,b,c,d,e}

There is \{\this\} and \{\that\}.

\pairwise{\this}{\that}{\result}
\def\expected{1/a,2/b,3/c,4/d,5/e}

Now \{\result\} is equal to \{\expected\}?

\end{document}

여기에 이미지 설명을 입력하세요

답변2

당신은토큰 목록이 아닌 토큰 목록 변수의 것입니다.

\seq_mapthread_function:NNN다음은 두 개의 시퀀스를 순회하고 항목을 두 개의 인수 함수에 전달하는 시퀀스 및 를 사용하는 다른 접근 방식입니다 .

`\pairwise에 대한 선택적 인수로 다른 출력 구분 기호를 제공할 수 있습니다. 예를 들어

\pairwise[|]{a,b}{1,2}{\result}

\result으로 정의할 것입니다 a/1|b/2.

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\pairwise}{O{,}mmm}
 {
  \seq_set_from_clist:No \l_komarov_first_seq { #2 }
  \seq_set_from_clist:No \l_komarov_second_seq { #3 }
  \seq_clear:N \l_komarov_output_seq
  \seq_mapthread_function:NNN
    \l_komarov_first_seq
    \l_komarov_second_seq
    \komarov_addentry:nn
  \tl_set:Nx #4 { \seq_use:Nn \l_komarov_output_seq { #1 } }
 }
\seq_new:N \l_komarov_first_seq
\seq_new:N \l_komarov_second_seq
\seq_new:N \l_komarov_output_seq
\cs_generate_variant:Nn \seq_set_from_clist:Nn { No }
\cs_new_protected:Nn \komarov_addentry:nn
 {
  \seq_put_right:Nn \l_komarov_output_seq { #1 / #2 }
 }
\ExplSyntaxOff

\begin{document}

\newcommand\this{1,2,3,4,5}
\newcommand\that{a,b,c,d,e}

There is \{\this\} and \{\that\}.

\pairwise{\this}{\that}{\result}
\newcommand\expected{1/a,2/b,3/c,4/d,5/e}

Now \{\result\} equals \{\expected\}

\pairwise{1,2,3,4,5}{a,b,c,d,e}{\newresult}

Also \{\newresult\} equals \{\expected\}

\end{document}

여기에 이미지 설명을 입력하세요

관련 정보