Unexpected behaviour of Nothing inside List inside Association
$begingroup$
When evaluating the following input:
<|a -> {c, d, e}|> /. d -> Nothing
I get (expression 1):
<|a -> {c, Nothing, e}|>
However I would expect to get (expression 2):
<|a -> {c, e}|>
Nevertheless, if i place the cursor on the unexpected output (expression 1) and evaluate it by pressing Shift + Enter, I get the expected output (expression 2), which is even more puzzling.
Mathematica version is 11.0.0.0
Should this behaviour be expected or is it a bug? Why?
list-manipulation replacement associations
$endgroup$
add a comment |
$begingroup$
When evaluating the following input:
<|a -> {c, d, e}|> /. d -> Nothing
I get (expression 1):
<|a -> {c, Nothing, e}|>
However I would expect to get (expression 2):
<|a -> {c, e}|>
Nevertheless, if i place the cursor on the unexpected output (expression 1) and evaluate it by pressing Shift + Enter, I get the expected output (expression 2), which is even more puzzling.
Mathematica version is 11.0.0.0
Should this behaviour be expected or is it a bug? Why?
list-manipulation replacement associations
$endgroup$
1
$begingroup$
closely related: Pattern matching to a function evaluation inside an Association
$endgroup$
– kglr
Jan 15 at 14:29
add a comment |
$begingroup$
When evaluating the following input:
<|a -> {c, d, e}|> /. d -> Nothing
I get (expression 1):
<|a -> {c, Nothing, e}|>
However I would expect to get (expression 2):
<|a -> {c, e}|>
Nevertheless, if i place the cursor on the unexpected output (expression 1) and evaluate it by pressing Shift + Enter, I get the expected output (expression 2), which is even more puzzling.
Mathematica version is 11.0.0.0
Should this behaviour be expected or is it a bug? Why?
list-manipulation replacement associations
$endgroup$
When evaluating the following input:
<|a -> {c, d, e}|> /. d -> Nothing
I get (expression 1):
<|a -> {c, Nothing, e}|>
However I would expect to get (expression 2):
<|a -> {c, e}|>
Nevertheless, if i place the cursor on the unexpected output (expression 1) and evaluate it by pressing Shift + Enter, I get the expected output (expression 2), which is even more puzzling.
Mathematica version is 11.0.0.0
Should this behaviour be expected or is it a bug? Why?
list-manipulation replacement associations
list-manipulation replacement associations
asked Jan 15 at 14:04
darmualdarmual
413
413
1
$begingroup$
closely related: Pattern matching to a function evaluation inside an Association
$endgroup$
– kglr
Jan 15 at 14:29
add a comment |
1
$begingroup$
closely related: Pattern matching to a function evaluation inside an Association
$endgroup$
– kglr
Jan 15 at 14:29
1
1
$begingroup$
closely related: Pattern matching to a function evaluation inside an Association
$endgroup$
– kglr
Jan 15 at 14:29
$begingroup$
closely related: Pattern matching to a function evaluation inside an Association
$endgroup$
– kglr
Jan 15 at 14:29
add a comment |
5 Answers
5
active
oldest
votes
$begingroup$
The observed puzzle is resolved by the following two observations:
From Leonid's answer in the linked q/a:
Association
isHoldAllComplete
. Once it is created, its parts will then normally be held unevaluated.
From Nothing
>> Details
- Nothing is removed as part of the standard evaluation process. It is not removed in expressions that are held or inactive.
You can Map
ReplaceAll
on the association to force removal of Nothing
s:
ReplaceAll[d -> Nothing] /@ assoc (* or *)
Map[# /. d -> Nothing &]@<|a -> {c, d, e}|>
<|a -> {c, e}|>
$endgroup$
add a comment |
$begingroup$
Use Replace
with level spec All
rather than ReplaceAll
:
Replace[<|a -> {c, d, e}|>, d -> Nothing, All]
<|a -> {c, e}|>
Related: Is there a difference between Replace with parameter "All" and ReplaceAll
$endgroup$
add a comment |
$begingroup$
Pattern matching to a function evaluation inside an Association answers your question about bug vs expected result.
This will not do what your input suggests but let me anticipate your needs:
<|a -> {b, c, d}, e :> {d, Print[1], d}, f -> d|> //.
{a___, d, b___} :> {a, b} /. (*this way Print is not evaluated*)
d -> Nothing
<|a -> {b, c}, e :> {Print[1]}, f -> Nothing|>
$endgroup$
add a comment |
$begingroup$
For completeness sakes (I believe Coolwater's answer is best):
assoc = Association[ a → {c,d,e} ];
Query
assoc // Query[ All, ReplaceAll[ d → Nothing ] ]
(* <|a -> {c, e}|> *)
Note: This will need repeated use of All
for nested associations.
DeleteCases
Before there was Nothing
there was DeleteCases
and the like:
assoc // DeleteCases[ #, d, Infinity ]&
(* <|a -> {c, e}|> *)
$endgroup$
add a comment |
$begingroup$
You may use Query
to evaluate the result of the replace in an Association
. Query
gives direct access to the key's value which then evaluated in Query
.
Query[All, # /. d -> Nothing &]@<|a -> {c, d, e}|>
<|a -> {c, e}|>
Hope this helps.
$endgroup$
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
});
});
}, "mathjax-editing");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "387"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f189530%2funexpected-behaviour-of-nothing-inside-list-inside-association%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
The observed puzzle is resolved by the following two observations:
From Leonid's answer in the linked q/a:
Association
isHoldAllComplete
. Once it is created, its parts will then normally be held unevaluated.
From Nothing
>> Details
- Nothing is removed as part of the standard evaluation process. It is not removed in expressions that are held or inactive.
You can Map
ReplaceAll
on the association to force removal of Nothing
s:
ReplaceAll[d -> Nothing] /@ assoc (* or *)
Map[# /. d -> Nothing &]@<|a -> {c, d, e}|>
<|a -> {c, e}|>
$endgroup$
add a comment |
$begingroup$
The observed puzzle is resolved by the following two observations:
From Leonid's answer in the linked q/a:
Association
isHoldAllComplete
. Once it is created, its parts will then normally be held unevaluated.
From Nothing
>> Details
- Nothing is removed as part of the standard evaluation process. It is not removed in expressions that are held or inactive.
You can Map
ReplaceAll
on the association to force removal of Nothing
s:
ReplaceAll[d -> Nothing] /@ assoc (* or *)
Map[# /. d -> Nothing &]@<|a -> {c, d, e}|>
<|a -> {c, e}|>
$endgroup$
add a comment |
$begingroup$
The observed puzzle is resolved by the following two observations:
From Leonid's answer in the linked q/a:
Association
isHoldAllComplete
. Once it is created, its parts will then normally be held unevaluated.
From Nothing
>> Details
- Nothing is removed as part of the standard evaluation process. It is not removed in expressions that are held or inactive.
You can Map
ReplaceAll
on the association to force removal of Nothing
s:
ReplaceAll[d -> Nothing] /@ assoc (* or *)
Map[# /. d -> Nothing &]@<|a -> {c, d, e}|>
<|a -> {c, e}|>
$endgroup$
The observed puzzle is resolved by the following two observations:
From Leonid's answer in the linked q/a:
Association
isHoldAllComplete
. Once it is created, its parts will then normally be held unevaluated.
From Nothing
>> Details
- Nothing is removed as part of the standard evaluation process. It is not removed in expressions that are held or inactive.
You can Map
ReplaceAll
on the association to force removal of Nothing
s:
ReplaceAll[d -> Nothing] /@ assoc (* or *)
Map[# /. d -> Nothing &]@<|a -> {c, d, e}|>
<|a -> {c, e}|>
edited Jan 15 at 14:39
answered Jan 15 at 14:07
kglrkglr
181k10200413
181k10200413
add a comment |
add a comment |
$begingroup$
Use Replace
with level spec All
rather than ReplaceAll
:
Replace[<|a -> {c, d, e}|>, d -> Nothing, All]
<|a -> {c, e}|>
Related: Is there a difference between Replace with parameter "All" and ReplaceAll
$endgroup$
add a comment |
$begingroup$
Use Replace
with level spec All
rather than ReplaceAll
:
Replace[<|a -> {c, d, e}|>, d -> Nothing, All]
<|a -> {c, e}|>
Related: Is there a difference between Replace with parameter "All" and ReplaceAll
$endgroup$
add a comment |
$begingroup$
Use Replace
with level spec All
rather than ReplaceAll
:
Replace[<|a -> {c, d, e}|>, d -> Nothing, All]
<|a -> {c, e}|>
Related: Is there a difference between Replace with parameter "All" and ReplaceAll
$endgroup$
Use Replace
with level spec All
rather than ReplaceAll
:
Replace[<|a -> {c, d, e}|>, d -> Nothing, All]
<|a -> {c, e}|>
Related: Is there a difference between Replace with parameter "All" and ReplaceAll
answered Jan 15 at 16:13
CoolwaterCoolwater
14.9k32553
14.9k32553
add a comment |
add a comment |
$begingroup$
Pattern matching to a function evaluation inside an Association answers your question about bug vs expected result.
This will not do what your input suggests but let me anticipate your needs:
<|a -> {b, c, d}, e :> {d, Print[1], d}, f -> d|> //.
{a___, d, b___} :> {a, b} /. (*this way Print is not evaluated*)
d -> Nothing
<|a -> {b, c}, e :> {Print[1]}, f -> Nothing|>
$endgroup$
add a comment |
$begingroup$
Pattern matching to a function evaluation inside an Association answers your question about bug vs expected result.
This will not do what your input suggests but let me anticipate your needs:
<|a -> {b, c, d}, e :> {d, Print[1], d}, f -> d|> //.
{a___, d, b___} :> {a, b} /. (*this way Print is not evaluated*)
d -> Nothing
<|a -> {b, c}, e :> {Print[1]}, f -> Nothing|>
$endgroup$
add a comment |
$begingroup$
Pattern matching to a function evaluation inside an Association answers your question about bug vs expected result.
This will not do what your input suggests but let me anticipate your needs:
<|a -> {b, c, d}, e :> {d, Print[1], d}, f -> d|> //.
{a___, d, b___} :> {a, b} /. (*this way Print is not evaluated*)
d -> Nothing
<|a -> {b, c}, e :> {Print[1]}, f -> Nothing|>
$endgroup$
Pattern matching to a function evaluation inside an Association answers your question about bug vs expected result.
This will not do what your input suggests but let me anticipate your needs:
<|a -> {b, c, d}, e :> {d, Print[1], d}, f -> d|> //.
{a___, d, b___} :> {a, b} /. (*this way Print is not evaluated*)
d -> Nothing
<|a -> {b, c}, e :> {Print[1]}, f -> Nothing|>
answered Jan 15 at 14:40
Kuba♦Kuba
105k12204526
105k12204526
add a comment |
add a comment |
$begingroup$
For completeness sakes (I believe Coolwater's answer is best):
assoc = Association[ a → {c,d,e} ];
Query
assoc // Query[ All, ReplaceAll[ d → Nothing ] ]
(* <|a -> {c, e}|> *)
Note: This will need repeated use of All
for nested associations.
DeleteCases
Before there was Nothing
there was DeleteCases
and the like:
assoc // DeleteCases[ #, d, Infinity ]&
(* <|a -> {c, e}|> *)
$endgroup$
add a comment |
$begingroup$
For completeness sakes (I believe Coolwater's answer is best):
assoc = Association[ a → {c,d,e} ];
Query
assoc // Query[ All, ReplaceAll[ d → Nothing ] ]
(* <|a -> {c, e}|> *)
Note: This will need repeated use of All
for nested associations.
DeleteCases
Before there was Nothing
there was DeleteCases
and the like:
assoc // DeleteCases[ #, d, Infinity ]&
(* <|a -> {c, e}|> *)
$endgroup$
add a comment |
$begingroup$
For completeness sakes (I believe Coolwater's answer is best):
assoc = Association[ a → {c,d,e} ];
Query
assoc // Query[ All, ReplaceAll[ d → Nothing ] ]
(* <|a -> {c, e}|> *)
Note: This will need repeated use of All
for nested associations.
DeleteCases
Before there was Nothing
there was DeleteCases
and the like:
assoc // DeleteCases[ #, d, Infinity ]&
(* <|a -> {c, e}|> *)
$endgroup$
For completeness sakes (I believe Coolwater's answer is best):
assoc = Association[ a → {c,d,e} ];
Query
assoc // Query[ All, ReplaceAll[ d → Nothing ] ]
(* <|a -> {c, e}|> *)
Note: This will need repeated use of All
for nested associations.
DeleteCases
Before there was Nothing
there was DeleteCases
and the like:
assoc // DeleteCases[ #, d, Infinity ]&
(* <|a -> {c, e}|> *)
edited Jan 15 at 17:28
answered Jan 15 at 17:22
gwrgwr
8,19822761
8,19822761
add a comment |
add a comment |
$begingroup$
You may use Query
to evaluate the result of the replace in an Association
. Query
gives direct access to the key's value which then evaluated in Query
.
Query[All, # /. d -> Nothing &]@<|a -> {c, d, e}|>
<|a -> {c, e}|>
Hope this helps.
$endgroup$
add a comment |
$begingroup$
You may use Query
to evaluate the result of the replace in an Association
. Query
gives direct access to the key's value which then evaluated in Query
.
Query[All, # /. d -> Nothing &]@<|a -> {c, d, e}|>
<|a -> {c, e}|>
Hope this helps.
$endgroup$
add a comment |
$begingroup$
You may use Query
to evaluate the result of the replace in an Association
. Query
gives direct access to the key's value which then evaluated in Query
.
Query[All, # /. d -> Nothing &]@<|a -> {c, d, e}|>
<|a -> {c, e}|>
Hope this helps.
$endgroup$
You may use Query
to evaluate the result of the replace in an Association
. Query
gives direct access to the key's value which then evaluated in Query
.
Query[All, # /. d -> Nothing &]@<|a -> {c, d, e}|>
<|a -> {c, e}|>
Hope this helps.
answered Jan 15 at 17:19
EdmundEdmund
26.3k330100
26.3k330100
add a comment |
add a comment |
Thanks for contributing an answer to Mathematica Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
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
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f189530%2funexpected-behaviour-of-nothing-inside-list-inside-association%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
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
Required, but never shown
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
Required, but never shown
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
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
$begingroup$
closely related: Pattern matching to a function evaluation inside an Association
$endgroup$
– kglr
Jan 15 at 14:29