Lets say I have an mapping, map, which associates various strings to numerical values :
{['a', 1],['b', 2], ['c',5]}
I want to define an operation, op
, which will distribute over arithmetic operations. So I want op to function as follows:
op('a+b') = 3
op('b') = 2
op('a*b+c') = 7
etc.
The only way I could think to do it is simply to parse the string, remember the operations, do the mapping, and do the operations. Seems sorta costly/long and I would hope there could be a more elegant solution with a built-in operation.
EDIT:
I realize the way I phrased this wasn't quiet accurate to my question, the mapping isn't defined on my system so I cannot define all possible mappings. I have to query an outside source for the mappings.