How can I update an association with a function?
Clash Royale CLAN TAG#URR8PPP
up vote
3
down vote
favorite
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:
<|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:
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!
functions associations
add a comment |Â
up vote
3
down vote
favorite
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:
<|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:
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!
functions associations
add a comment |Â
up vote
3
down vote
favorite
up vote
3
down vote
favorite
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:
<|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:
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!
functions associations
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:
<|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:
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!
functions associations
functions associations
asked 4 hours ago
Jmeeks29ig
34818
34818
add a comment |Â
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
4
down vote
accepted
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
(* <|p10 -> 10, p13 -> 13, p14 -> 14, p15 -> 15, p12 -> -12,
p11 -> -11, p16 -> -16|> *)
AssociateTo
has the attribute 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.
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
SetAttributes[myAssociateTo, HoldFirst]
Now we reset pool
and try it.
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
(* <|p10 -> 10, p11 -> -11, p12 -> -12, p13 -> 13, p14 -> 14,
p15 -> 15, p16 -> -16|> *)
Thank you Carl. Corrected the typo.
â Jack LaVigne
3 hours ago
I am wondering if I can ask about the necessity to rename a built-in function.
â ÃÂûÃÂþñýôÃÂÿàÃÂõóó
2 hours ago
@ÃÂûÃÂþñýôÃÂÿÃÂÃÂõóó because you want to change its Attributes ?
â chris
29 mins ago
@Jack LaVigne, Thanks, I really appreciate that! It makes a lot more sense to me now
â Jmeeks29ig
13 mins ago
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
accepted
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
(* <|p10 -> 10, p13 -> 13, p14 -> 14, p15 -> 15, p12 -> -12,
p11 -> -11, p16 -> -16|> *)
AssociateTo
has the attribute 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.
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
SetAttributes[myAssociateTo, HoldFirst]
Now we reset pool
and try it.
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
(* <|p10 -> 10, p11 -> -11, p12 -> -12, p13 -> 13, p14 -> 14,
p15 -> 15, p16 -> -16|> *)
Thank you Carl. Corrected the typo.
â Jack LaVigne
3 hours ago
I am wondering if I can ask about the necessity to rename a built-in function.
â ÃÂûÃÂþñýôÃÂÿàÃÂõóó
2 hours ago
@ÃÂûÃÂþñýôÃÂÿÃÂÃÂõóó because you want to change its Attributes ?
â chris
29 mins ago
@Jack LaVigne, Thanks, I really appreciate that! It makes a lot more sense to me now
â Jmeeks29ig
13 mins ago
add a comment |Â
up vote
4
down vote
accepted
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
(* <|p10 -> 10, p13 -> 13, p14 -> 14, p15 -> 15, p12 -> -12,
p11 -> -11, p16 -> -16|> *)
AssociateTo
has the attribute 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.
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
SetAttributes[myAssociateTo, HoldFirst]
Now we reset pool
and try it.
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
(* <|p10 -> 10, p11 -> -11, p12 -> -12, p13 -> 13, p14 -> 14,
p15 -> 15, p16 -> -16|> *)
Thank you Carl. Corrected the typo.
â Jack LaVigne
3 hours ago
I am wondering if I can ask about the necessity to rename a built-in function.
â ÃÂûÃÂþñýôÃÂÿàÃÂõóó
2 hours ago
@ÃÂûÃÂþñýôÃÂÿÃÂÃÂõóó because you want to change its Attributes ?
â chris
29 mins ago
@Jack LaVigne, Thanks, I really appreciate that! It makes a lot more sense to me now
â Jmeeks29ig
13 mins ago
add a comment |Â
up vote
4
down vote
accepted
up vote
4
down vote
accepted
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
(* <|p10 -> 10, p13 -> 13, p14 -> 14, p15 -> 15, p12 -> -12,
p11 -> -11, p16 -> -16|> *)
AssociateTo
has the attribute 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.
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
SetAttributes[myAssociateTo, HoldFirst]
Now we reset pool
and try it.
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
(* <|p10 -> 10, p11 -> -11, p12 -> -12, p13 -> 13, p14 -> 14,
p15 -> 15, p16 -> -16|> *)
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
(* <|p10 -> 10, p13 -> 13, p14 -> 14, p15 -> 15, p12 -> -12,
p11 -> -11, p16 -> -16|> *)
AssociateTo
has the attribute 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.
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
SetAttributes[myAssociateTo, HoldFirst]
Now we reset pool
and try it.
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
(* <|p10 -> 10, p11 -> -11, p12 -> -12, p13 -> 13, p14 -> 14,
p15 -> 15, p16 -> -16|> *)
edited 3 hours ago
answered 4 hours ago
Jack LaVigne
11.2k21329
11.2k21329
Thank you Carl. Corrected the typo.
â Jack LaVigne
3 hours ago
I am wondering if I can ask about the necessity to rename a built-in function.
â ÃÂûÃÂþñýôÃÂÿàÃÂõóó
2 hours ago
@ÃÂûÃÂþñýôÃÂÿÃÂÃÂõóó because you want to change its Attributes ?
â chris
29 mins ago
@Jack LaVigne, Thanks, I really appreciate that! It makes a lot more sense to me now
â Jmeeks29ig
13 mins ago
add a comment |Â
Thank you Carl. Corrected the typo.
â Jack LaVigne
3 hours ago
I am wondering if I can ask about the necessity to rename a built-in function.
â ÃÂûÃÂþñýôÃÂÿàÃÂõóó
2 hours ago
@ÃÂûÃÂþñýôÃÂÿÃÂÃÂõóó because you want to change its Attributes ?
â chris
29 mins ago
@Jack LaVigne, Thanks, I really appreciate that! It makes a lot more sense to me now
â Jmeeks29ig
13 mins ago
Thank you Carl. Corrected the typo.
â Jack LaVigne
3 hours ago
Thank you Carl. Corrected the typo.
â Jack LaVigne
3 hours ago
I am wondering if I can ask about the necessity to rename a built-in function.
â ÃÂûÃÂþñýôÃÂÿàÃÂõóó
2 hours ago
I am wondering if I can ask about the necessity to rename a built-in function.
â ÃÂûÃÂþñýôÃÂÿàÃÂõóó
2 hours ago
@ÃÂûÃÂþñýôÃÂÿÃÂÃÂõóó because you want to change its Attributes ?
â chris
29 mins ago
@ÃÂûÃÂþñýôÃÂÿÃÂÃÂõóó because you want to change its Attributes ?
â chris
29 mins ago
@Jack LaVigne, Thanks, I really appreciate that! It makes a lot more sense to me now
â Jmeeks29ig
13 mins ago
@Jack LaVigne, Thanks, I really appreciate that! It makes a lot more sense to me now
â Jmeeks29ig
13 mins ago
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f181924%2fhow-can-i-update-an-association-with-a-function%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password