As far as I know, the reason this does not exist is that replacements are order dependent. Consider the following:
julia> str = "ab"
julia> replace(str, "a" => "b") |> x -> replace(x, "b" => "a")
Since the first invocation returns
"bb", the second invocation (rightly so) returns
Now consider your wish - a
replace function that “can perform multiple replacements at once”:
replace(str, "a" => "b", "b" => "a")
What should the result be and is there a generic way to express this?
Should it be
"ba", only applying each rule once, seemingly simultaneously? Or should it be “aa”, applying the first rule until exhaustion, then the second rule until exhaustion and so on. There is no generic way to decide this for a user of the function, since there are many different results that are the “correct” result to someone, so it’s hard to prioritise and leave the order to the user instead.
An even more catastrophic example is the following:
replace("aabbaa", "abba" => "bb", "aa" => "bb", "bb" => "aa")
Possible results, depending on the order of the Pairs and how many times each are applied are:
"aa" (First pair applied until exhaustion, then second doesn’t match, third applied until exhaustion)
"aaaa" (All pairs applied once consecutively)
"bbaabb" (Shortest rules are applied first, rules are only applied once)
- Infinite loop (Pairs are applied one after another in a loop until all pairs are exhausted, which never happens. This happens very easily…)
- …and many more
or even, this with overlapping rules:
replace("aabb", "aabb" => "yes", "ab" => "no", "no" => "")
Which should fire first and is more important? How often is each rule applied?
This is much too implementation dependant to be offered as a generic function in Base, in my opinion.