Brace expansion: Difference between revisions
Content added Content deleted
(Add Go solution, this may not be a great or clear way to do it … but it works) |
(Added zkl) |
||
Line 1,611: | Line 1,611: | ||
{}} some }{,{\\ edge \,}{ cases, {here} \\\\\} |
{}} some }{,{\\ edge \,}{ cases, {here} \\\\\} |
||
{}} some }{,{\\ edge \,}{ cases, {here} \\\\\} |
{}} some }{,{\\ edge \,}{ cases, {here} \\\\\} |
||
</pre> |
|||
=={{header|zkl}}== |
|||
<lang zkl>fcn eyeball(code,ps=L(),brace=False){ //-->indexes of valid braces & commas |
|||
cs:=L(); |
|||
foreach c in (code){ // start fresh or continue (if recursing) |
|||
switch(c){ |
|||
case("\\"){ __cWalker.next(); } |
|||
case(",") { if(brace) cs.append(__cWalker.n); } // maybe valid |
|||
case("{") { // this is real only if there is matching } and a comma |
|||
n:=__cWalker.n; |
|||
_,cz:=self.fcn(__cWalker,ps,True); |
|||
if(cz){ ps.append(n,__cWalker.n); ps.extend(cz) } // valid {} pair |
|||
} |
|||
case("}"){ if(brace) return(ps,cs); } |
|||
} |
|||
} |
|||
return(ps,False) |
|||
} |
|||
fcn expando(code,strings=T("")){ |
|||
reg [const] stack=List(); reg roots; bs,_:=eyeball(code); |
|||
foreach c in (code){ |
|||
bn:=bs.holds(__cWalker.n); |
|||
if (c=="{" and bn) roots=strings; |
|||
else if(c=="," and bn){ stack.append(strings); strings=roots; } |
|||
else if(c=="}" and bn) strings=stack.pop().extend(strings); |
|||
else if(c=="\\"){ |
|||
c="\\"+__cWalker.next(); |
|||
strings=strings.apply('+(c)); |
|||
} |
|||
else strings=strings.apply('+(c)); |
|||
} |
|||
strings.extend(stack.flatten()) |
|||
}</lang> |
|||
<lang zkl>foreach bs in (T("It{{em,alic}iz,erat}e{d,}", "~/{Downloads,Pictures}/*.{jpg,gif,png}", |
|||
"It{{em,alic}iz,erat}e{d,}, please.", "a{2,1}b{X,Y,X}c", 0'|a\\{\\\{b,c\,d}|, |
|||
"{a,b{c{,{d}}e}f", 0'|{,{,gotta have{ ,\, again\, }}more }cowbell!|, |
|||
0'|{}} some }{,{\\{ edge, edge} \,}{ cases, {here} \\\\\}|)) |
|||
{ |
|||
"%s expands to\n %s".fmt(bs,expando(bs)).println(); |
|||
}</lang> |
|||
{{out}} |
|||
<pre> |
|||
It{{em,alic}iz,erat}e{d,} expands to |
|||
L("Itemized","Italicized","Iterated","Itemize","Italicize","Iterate") |
|||
~/{Downloads,Pictures}/*.{jpg,gif,png} expands to |
|||
L("~/Downloads/*.gif","~/Pictures/*.gif","~/Downloads/*.png","~/Pictures/*.png","~/Downloads/*.jpg","~/Pictures/*.jpg") |
|||
It{{em,alic}iz,erat}e{d,}, please. expands to |
|||
L("Itemized, please.","Italicized, please.","Iterated, please.","Itemize, please.","Italicize, please.","Iterate, please.") |
|||
a{2,1}b{X,Y,X}c expands to |
|||
L("a2bYc","a1bYc","a2bXc","a1bXc","a2bX","a1bX") |
|||
a\\{\\\{b,c\,d} expands to |
|||
L("a\\\\\{b","a\\c\,d") |
|||
{a,b{c{,{d}}e}f expands to |
|||
L("{a,b{ce}f","{a,b{c{d}e}f") |
|||
{,{,gotta have{ ,\, again\, }}more }cowbell! expands to |
|||
L("cowbell!","more cowbell!","gotta have more cowbell!","gotta have\, again\, more cowbell!") |
|||
{}} some }{,{\\{ edge, edge} \,}{ cases, {here} \\\\\} expands to |
|||
L("{}} some }{,{\\ edge \,}{ cases, {here} \\\\\}","{}} some }{,{\\ edge \,}{ cases, {here} \\\\\}") |
|||
</pre> |
</pre> |