www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Hash-Table-Based Multiple Arguments Replacement

reply =?UTF-8?B?Tm9yZGzDtnc=?= <per.nordlow gmail.com> writes:
Is there an algorithm somewhere in Phobos which performs when 
possible a replacement/substitution based on a variadic 
definition of replacements using hash-table search similar to

string replaceWhole(string a)
{
     switch (x)
     {
     case "a": return "1";
     case "b": return "2";
     default:  return x;
     }
}

?

Desired interface

y = x.replaceWhole!("a","x",
                     "b","y",
                     "c","z")

or perhaps

y = x.replaceWhole!(tuple("a","x"),
                     tuple("b","y"),
                     tuple("c","z"))


kind of like

"a".among!("a", "b", "c")

but for replacements.
Oct 10 2015
next sibling parent reply =?UTF-8?B?Tm9yZGzDtnc=?= <per.nordlow gmail.com> writes:
On Saturday, 10 October 2015 at 16:19:53 UTC, Nordlöw wrote:
 Is there an algorithm somewhere in Phobos which performs when 
 possible a replacement/substitution based on a variadic 
 definition of replacements using hash-table search similar to
Found it: http://dlang.org/phobos/std_algorithm_comparison.html#predSwitch An alias would be perhaps be motivated to make newcomers easier grap that this can be used for whole replacements.
Oct 10 2015
parent =?UTF-8?B?Tm9yZGzDtnc=?= <per.nordlow gmail.com> writes:
On Saturday, 10 October 2015 at 16:42:52 UTC, Nordlöw wrote:
 Found it:

 http://dlang.org/phobos/std_algorithm_comparison.html#predSwitch

 An alias would be perhaps be motivated to make newcomers easier 
 grap that this can be used for whole replacements.
Ahh, but this doesn't use a hash-table because it doesn't support taking its arguments as CT-params...so that doesn't qualify. Maybe we should add a new CT-param-only overload for `predSwitch` similar to `among`? Destroy!
Oct 10 2015
prev sibling next sibling parent reply Meta <jared771 gmail.com> writes:
On Saturday, 10 October 2015 at 16:19:53 UTC, Nordlöw wrote:
 Is there an algorithm somewhere in Phobos which performs when 
 possible a replacement/substitution based on a variadic 
 definition of replacements using hash-table search similar to

 string replaceWhole(string a)
 {
     switch (x)
     {
     case "a": return "1";
     case "b": return "2";
     default:  return x;
     }
 }

 ?

 Desired interface

 y = x.replaceWhole!("a","x",
                     "b","y",
                     "c","z")

 or perhaps

 y = x.replaceWhole!(tuple("a","x"),
                     tuple("b","y"),
                     tuple("c","z"))


 kind of like

 "a".among!("a", "b", "c")

 but for replacements.
There was something like this proposed quite awhile ago (can't remember what it was, might've been extending unary/binaryFun to accept an AA), but it was rejected.
Oct 10 2015
parent reply =?UTF-8?B?Tm9yZGzDtnc=?= <per.nordlow gmail.com> writes:
On Sunday, 11 October 2015 at 00:16:44 UTC, Meta wrote:
 There was something like this proposed quite awhile ago (can't 
 remember what it was, might've been extending unary/binaryFun 
 to accept an AA), but it was rejected.
With static foreach in a switch we can do better. I'll put together a solution.
Oct 10 2015
parent reply =?UTF-8?B?Tm9yZGzDtnc=?= <per.nordlow gmail.com> writes:
On Sunday, 11 October 2015 at 05:06:04 UTC, Nordlöw wrote:
 On Sunday, 11 October 2015 at 00:16:44 UTC, Meta wrote:
 There was something like this proposed quite awhile ago (can't 
 remember what it was, might've been extending unary/binaryFun 
 to accept an AA), but it was rejected.
With static foreach in a switch we can do better. I'll put together a solution.
Here's a solution: https://github.com/nordlow/justd/blob/f8519e8a1af68bc25cc00c6ef12d13efa791250c/comparison_ex.d
Oct 11 2015
parent Per =?UTF-8?B?Tm9yZGzDtnc=?= <per.nordlow gmail.com> writes:
On Sunday, 11 October 2015 at 11:17:29 UTC, Nordlöw wrote:
 Here's a solution:
 https://github.com/nordlow/justd/blob/f8519e8a1af68bc25cc00c6ef12d13efa791250c/comparison_ex.d
Latest version contains a few fixes: https://github.com/nordlow/justd/blob/master/comparison_ex.d
Oct 13 2015
prev sibling parent ixid <adamsibson hotmail.com> writes:
On Saturday, 10 October 2015 at 16:19:53 UTC, Nordlöw wrote:
 Is there an algorithm somewhere in Phobos which performs when 
 possible a replacement/substitution based on a variadic 
 definition of replacements using hash-table search similar to

 string replaceWhole(string a)
 {
     switch (x)
     {
     case "a": return "1";
     case "b": return "2";
     default:  return x;
     }
 }

 ?

 Desired interface

 y = x.replaceWhole!("a","x",
                     "b","y",
                     "c","z")

 or perhaps

 y = x.replaceWhole!(tuple("a","x"),
                     tuple("b","y"),
                     tuple("c","z"))


 kind of like

 "a".among!("a", "b", "c")

 but for replacements.
It would also be nice to have a splitter that can split on any of a number of conditions being fulfilled in a similar vein.
Oct 12 2015