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
$\endgroup$DownValues
onPlus
you will slow down anything that has aPlus
in it, by a considerable amount. Either define a new operationmyNewListPlus
, or do this withUpValues
. $\endgroup$