DOWNLOAD-DESKTOP-NOTEBOOK.nb (4.17 MB)
– you have earned Featured Contributor Badge ![]()
Your exceptional post has been selected for our editorial column Staff Picks http://wolfr.am/StaffPicks and Your Profile is now distinguished by a Featured Contributor Badge and is displayed on the Featured Contributor Board. Thank you!
Hi Thomas, This is not correct:
In Mathematica, on the other hand, arguments are passed as values, meaning that the arguments are copied into the function’s local environment. This means that any modifications made to the arguments inside the function do not affect the original variables.
…associations are immutable data types, so we cannot modify them in-place anyways..
ClearAll[fChangeAssoc];
SetAttributes[fChangeAssoc, HoldFirst];
fChangeAssoc[assoc_, key_, newVal_] := AppendTo[assoc, key -> newVal];
assoc = <| "this" -> 1, "that" -> 2 |>
fChangeAssoc[assoc, "this", 3]
assoc
<| "that" -> 2, "this" -> 3 |>
Note that the first argument is NOT restricted as _Association when using the HoldFirst:
ClearAll[fChangeAssocWRONG];
SetAttributes[fChangeAssocWRONG, HoldFirst];
fChangeAssocWRONG[assoc_Association, key_, newVal_] := AppendTo[assoc, key -> newVal];
The following is not matched:
fChangeAssocWRONG[assoc]
fChangeAssocWRONG[assoc]
Hello, here’s my experience with Python versus Mathematica. As opposed to Mathematica which I use since 20 years, I hardly know Python. Part of the reason is this: Learning Mathematica on your own (with its lush explanations of in-built functions, including toy examples) is certainly easier than learning Python. Furthermore various versions of Pythons are incompatible. Unfortunately Mathematica does not offer Binary Decision Diagrams (BDD) to work with (why??). For the latter I had to turn to expr2bdd from Python, and to the help of Python pro’s.
Hi Darren,
Thanks for pointing that out, that is a mistake from my part. I’ve rectified it my original post.