I'm trying to develop an domain-specific language with Mathematica. I want to define a customized behavior for the Plus function when applied to my data.
For simplifying the problem, let's say that my custom data is of a list of associations.
Unprotect[Plus];
Plus[x_List, y_List] := Join[x, y] // Merge[Total]
Protect[Plus];
Then,
{<|"a" -> 1|>, <|"b" -> 2|>} + {<|"a" -> 2|>}
gives the correct answer with an error message.
Thread::tdlen: Objects of unequal length in {<|a -> 1|>, <|b -> 2|>} + {<|a -> 2|>} cannot be combined. >>
<|"a" -> 3, "b" -> 2|>
How can I avoid the error message? I suspect an evaluation control problem, but I can't find any clues to solve it.
CirclePlus– bobthechemist Sep 29 '14 at 12:01DownValuesonPlusyou will slow down anything that has aPlusin it, by a considerable amount. Either define a new operationmyNewListPlus, or do this withUpValues. – Daniel Lichtblau Sep 29 '14 at 15:07Plus[x_?MyDataQ, y_?MyDataQ]^:=myPlus[x,y]. But it is not possible. However, the link is useful. Thanks. – Y. Kwon Sep 30 '14 at 12:44UpValuesis not a good idea in this specific situation. The reason is that they would be put onList, which is another object so critical to internals that it will likely mess up performance. – Daniel Lichtblau Sep 30 '14 at 14:53