* TeX macro to merge two lists, bit like Python's zip
@ 2010-06-08 22:16 luigi scarso
0 siblings, 0 replies; only message in thread
From: luigi scarso @ 2010-06-08 22:16 UTC (permalink / raw)
To: Hans Hagen, mailing list for ConTeXt users
J. Fine asked sometime ago something like python's zip tha merge two
"lists" element by element, cfr.
http://groups.google.com/group/comp.text.tex/browse_thread/thread/2c1feea4ad19501e/cf6f849c5b81e76c?show_docid=cf6f849c5b81e76c
Here is the idea: a list is zero or more control sequence, ie
{} is an empty list,
\Alpha\Beta
is a list with 2 elements, and so on.
\zip{\Alpha\Beta}{\Gamma\Delta} gives \Alpha\Gamma\Beta\Delta
\starttext
% Hans's hint
% \def\zipthem#1#2%
% {\def\dozipthem##1{\def\dodozipthem####1{1\string##1
2\string####1}\handletokens#2\with\dodozipthem}%
% \handletokens#1\with\dozipthem}
\def\myzipthem#1#2%
{\ctxlua{A = {}; B = {}}
\def\dozipthemA##1{\ctxlua{table.insert(A,[[\string##1]])}}%
\def\dozipthemB##1{\ctxlua{table.insert(B,[[\string##1]])}}%
\handletokens#1\with\dozipthemA
\handletokens#2\with\dozipthemB%
\ctxlua{%
for i=1,math.max(table.maxn(A),table.maxn(B)) do
local a = ""
local b = ""
if A[i] then a = '\\string' ..A[i] end
if B[i] then b = '\\string' ..B[i] end
tex.sprint(a,b)
end
tex.print([[\blank]])
for i=1,math.max(table.maxn(A),table.maxn(B)) do
local a = A[i] or ""
local b = B[i] or ""
tex.sprint(a,b)
end
tex.print([[\blank]])
}
}
\def\Alpha{A}
\def\Beta{B}
\def\Gamma{C}
\def\Delta{D}
\myzipthem{\Alpha\Beta}{\Gamma\Delta}%gives \Alpha\Gamma\Beta\Delta
\myzipthem{}{\Gamma\Delta}
\stoptext
For ours bloggers.
--
luigi
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!
maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage : http://www.pragma-ade.nl / http://tex.aanhet.net
archive : http://foundry.supelec.fr/projects/contextrev/
wiki : http://contextgarden.net
___________________________________________________________________________________
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2010-06-08 22:16 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-06-08 22:16 TeX macro to merge two lists, bit like Python's zip luigi scarso
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).