Brace expansion: Difference between revisions

(→‎{{header|TXR}}: New entry.)
Line 5,145:
{}} some }{,{\\ edge \,}{ cases, {here} \\\\\}
</pre>
=={{header|TXR}}==
<syntaxhighlight lang="txrlisp">;; API
(defun brace-expand (str)
(bexp-expand (bexp-parse str)))
 
;; parser
(defstruct bexp-parse-ctx ()
str
toks)
 
(defun bexp-parse (str)
(let ((ctx (new bexp-parse-ctx
str str
;; tokenizer
toks (remqual "" (tok #/[{},]/ t str)))))
(build
(whilet ((next (pop ctx.toks)))
(add
(if (equal next "{")
(bexp-parse-brace ctx)
next))))))
 
(defun bexp-parse-brace (ctx)
(flow
(build
(whilet ((next (pop ctx.toks)))
(casequal next
("{" (add (bexp-parse-brace ctx)))
("," (add :))
("}" (return))
(t (add next)))))
(split* @1 (op where (op eq :)))
(mapcar (tc
(((x)) x)
((x) x)))
(cons '/)))
 
;; expander
(defun bexp-expand (tree : (path (new list-builder)))
(build
(match-case tree
(() (add (cat-str path.(get))))
(((/ . @alt) . @rest)
(let ((saved-path path.(get)))
(each ((elem alt))
path.(oust saved-path)
(pend (bexp-expand (cons elem rest) path)))))
((@(consp @succ) . @rest)
(pend (bexp-expand (append succ rest) path)))
((@head . @rest)
path.(add head)
(pend (bexp-expand rest path))))))
 
;; Tests
(prinl (brace-expand ""))
(prinl (brace-expand "a"))
(prinl (brace-expand "a,b,c"))
(prinl (brace-expand "{a,b}"))
(prinl (brace-expand "{a,b,c}"))
(prinl (brace-expand "x{a,b,c}y"))
(prinl (brace-expand "x{a,b}y{c,d}z"))
(prinl (brace-expand "x{a{b,c}d,e{f,g}h}y"))
(prinl (bexp-parse "It{{em,alic}iz,erat}e{d,}"))
(prinl (brace-expand "It{{em,alic}iz,erat}e{d,}"))</syntaxhighlight>
 
{{out}}
 
<pre>("")
("a")
("a,b,c")
("a" "b")
("a" "b" "c")
("xay" "xby" "xcy")
("xaycz" "xaydz" "xbycz" "xbydz")
("xabdy" "xacdy" "xefhy" "xeghy")
("It" (/ ((/ "em" "alic")
"iz")
"erat")
"e" (/ "d" ()))
("Itemized" "Itemize" "Italicized" "Italicize" "Iterated" "Iterate")
</pre>
 
=={{header|Visual Basic .NET}}==
{{trans|Python}}
Line 5,245 ⟶ 5,327:
{}} some }{,{\ edge \}{ cases, {here} \\\
{}} some }{,{\ edge \}{ cases, {here} \\\</pre>
 
=={{header|Wren}}==
{{trans|Python}}
543

edits