How can I update an association with a function?
How can I update an association with a function?
I'm not sure if this is a bug, or if I just am not understanding, properly, how to create a function.
I am able to use a list of rules to update an association like so:
AssociateTo[pools, asocs]
Where pools is:
pools
<|p10->-0.271069,p11->-0.748554,p12->-0.772571,p13->0.801179,p14->0.823549,p15->-0.617603,p16->-0.171479,p17->0.860391,p18->-0.0937823,p19->0.142897|>
And asocs is:
asocs
p12 -> -1.0208, p11 -> -1.09988*10^7, p15 -> -0.0190685,
p19 -> 0.075402, p10 -> -0.0904602, p16 -> -0.147492,
p17 -> 0.353486, p13 -> 869.624
However, when I try to use this function:
updatePoolwithValues[a_, newVals_] := AssociateTo[a, newVals];
I get the following error message:
<|p10->-0.271069,p11->-0.748554,p12->-0.772571,p13->0.801179,p14->0.823549,p15->-0.617603,p16->-0.171479,p17->0.860391,p18->-0.0937823,p19->0.142897|> is not a variable with a value, so its value cannot be changed.
Is this an error, or is there something I am not understanding?
Thanks!
1 Answer
1
I am going to use simpler numbers and a shorter set of rules and lists to make it easier to focus on the problem.
pool = <|p10 -> 10, p11 -> 11, p12 -> 12, p13 -> 13, p14 -> 14,
p15 -> 15|>;
assoc = p12 -> -12, p11 -> -11, p16 -> -16;
AssociateTo[pool, assoc]
(* <|p10 -> 10, p13 -> 13, p14 -> 14, p15 -> 15, p12 -> -12,
p11 -> -11, p16 -> -16|> *)
After execution check that the symbol pool has been changed.
pool
pool
(* <|p10 -> 10, p13 -> 13, p14 -> 14, p15 -> 15, p12 -> -12,
p11 -> -11, p16 -> -16|> *)
AssociateTo has the attribute HoldFirst
AssociateTo
HoldFirst
Attributes[AssociateTo]
(* HoldFirst, Protected *)
Now let's define your function. I am going to use a slightly different name to clarify what we are attempting to do:
myAssociateTo[a_, newVals_] := AssociateTo[a, newVals]
If you apply this to pool and assoc you will indeed get an error.
pool
assoc
myAssociateTo[pool, assoc]
During evaluation of AssociateTo::rvalue: <|p10->10,p11->11,p12->12,
p13->13,p14->14,p15->15|> is not a variable with a value, so its value
cannot be changed.
Let's give myAssociateTo the same attribute as AssociateTo
myAssociateTo
AssociateTo
SetAttributes[myAssociateTo, HoldFirst]
Now we reset pool and try it.
pool
pool = <|p10 -> 10, p11 -> 11, p12 -> 12, p13 -> 13, p14 -> 14,
p15 -> 15|>;
myAssociateTo[pool, assoc]
(* <|p10 -> 10, p11 -> -11, p12 -> -12, p13 -> 13, p14 -> 14,
p15 -> 15, p16 -> -16|> *)
The desired result has been achieved.
Note further that the symbol pool has been equated to the new value.
pool
pool
(* <|p10 -> 10, p11 -> -11, p12 -> -12, p13 -> 13, p14 -> 14,
p15 -> 15, p16 -> -16|> *)
$begingroup$
I am wondering if I can ask about the necessity to rename a built-in function.
$endgroup$
– Αλέξανδρος Ζεγγ
Sep 15 '18 at 0:30
$begingroup$
@ΑλέξανδροςΖεγγ because you want to change its Attributes ?
$endgroup$
– chris
Sep 15 '18 at 2:39
$begingroup$
@Jack LaVigne, Thanks, I really appreciate that! It makes a lot more sense to me now
$endgroup$
– Jmeeks29ig
Sep 15 '18 at 2:55
$begingroup$
@Αλέξανδρος Ζεγγ, Also, I want to put this function in another larger function, and want to be sure I can understand it when I do that.
$endgroup$
– Jmeeks29ig
Sep 15 '18 at 3:11
Thanks for contributing an answer to Mathematica Stack Exchange!
But avoid …
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
$begingroup$
Thank you Carl. Corrected the typo.
$endgroup$
– Jack LaVigne
Sep 14 '18 at 23:26