Perform localized, evaluation-leak free replacements
Consider the following expression:
expr = Hold[
{
f[
{1, {Print@1}}
],
g[
{{{Print@1}}}
]
}
];
I'm looking for a way to apply a replacement rule to the contents of all expressions with head f
. To give a more concrete example, let's say I want to replace Print
by Echo
, but only inside f
.
Restrictions: I have no knowledge about the exact structure around and inside f
- there could be more or less nesting going on. This means the replacement rule can't capture the wrapper with Hold
attribute, nor can it capture the individual parts of f[…]
. (And the evaluation of Print
should be prevented of course)
evaluation replacement hold
add a comment |
Consider the following expression:
expr = Hold[
{
f[
{1, {Print@1}}
],
g[
{{{Print@1}}}
]
}
];
I'm looking for a way to apply a replacement rule to the contents of all expressions with head f
. To give a more concrete example, let's say I want to replace Print
by Echo
, but only inside f
.
Restrictions: I have no knowledge about the exact structure around and inside f
- there could be more or less nesting going on. This means the replacement rule can't capture the wrapper with Hold
attribute, nor can it capture the individual parts of f[…]
. (And the evaluation of Print
should be prevented of course)
evaluation replacement hold
add a comment |
Consider the following expression:
expr = Hold[
{
f[
{1, {Print@1}}
],
g[
{{{Print@1}}}
]
}
];
I'm looking for a way to apply a replacement rule to the contents of all expressions with head f
. To give a more concrete example, let's say I want to replace Print
by Echo
, but only inside f
.
Restrictions: I have no knowledge about the exact structure around and inside f
- there could be more or less nesting going on. This means the replacement rule can't capture the wrapper with Hold
attribute, nor can it capture the individual parts of f[…]
. (And the evaluation of Print
should be prevented of course)
evaluation replacement hold
Consider the following expression:
expr = Hold[
{
f[
{1, {Print@1}}
],
g[
{{{Print@1}}}
]
}
];
I'm looking for a way to apply a replacement rule to the contents of all expressions with head f
. To give a more concrete example, let's say I want to replace Print
by Echo
, but only inside f
.
Restrictions: I have no knowledge about the exact structure around and inside f
- there could be more or less nesting going on. This means the replacement rule can't capture the wrapper with Hold
attribute, nor can it capture the individual parts of f[…]
. (And the evaluation of Print
should be prevented of course)
evaluation replacement hold
evaluation replacement hold
edited 8 hours ago
Kuba♦
103k12201516
103k12201516
asked yesterday
Lukas Lang
6,4351929
6,4351929
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
Here's a way making use of Block
to cause f
to be inert:
Block[{f},
SetAttributes[f, HoldAllComplete];
expr /. f[args__] :>
RuleCondition[f[args] /. Print -> Echo]
]
Hold[{f[{1, {Echo[1]}}], g[{{{Print[1]}}}]}]
Note of course that this only works if you have a pattern of the form _Symbol[...]
add a comment |
I can think of one (ugly) way to do it:
Attributes[myHold] = {HoldAll};
expr /.
f[args__] :> With[
{res = myHold[args] /. Print -> Echo},
f @@ res /; True
] /.
HoldPattern[f_ @@ myHold[args__]] :> f[args]
(* Hold[{f[{1, {Echo[1]}}], g[{{{Print[1]}}}]}] *)
The idea is to wrap the contents of f
inside a function with HoldAll
attribute (not Hold
, to be able to identify it uniquely later on). In a first step, the expression is returned with myHold[…]
still in place. In a second round of replacements, myHold
is stripped out again.
1
+1. If we use the vanishing macro defined in (43096), we can writevanishing[{h}, expr /. x_f :> RuleCondition[h[x] /. Print -> Echo]]
.
– WReach
yesterday
add a comment |
Alternatively:
expr /.
foo_f :> RuleCondition[Hold[foo] /. Print -> Echo] /.
Hold[foo_f] :> foo
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%2f188887%2fperform-localized-evaluation-leak-free-replacements%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Here's a way making use of Block
to cause f
to be inert:
Block[{f},
SetAttributes[f, HoldAllComplete];
expr /. f[args__] :>
RuleCondition[f[args] /. Print -> Echo]
]
Hold[{f[{1, {Echo[1]}}], g[{{{Print[1]}}}]}]
Note of course that this only works if you have a pattern of the form _Symbol[...]
add a comment |
Here's a way making use of Block
to cause f
to be inert:
Block[{f},
SetAttributes[f, HoldAllComplete];
expr /. f[args__] :>
RuleCondition[f[args] /. Print -> Echo]
]
Hold[{f[{1, {Echo[1]}}], g[{{{Print[1]}}}]}]
Note of course that this only works if you have a pattern of the form _Symbol[...]
add a comment |
Here's a way making use of Block
to cause f
to be inert:
Block[{f},
SetAttributes[f, HoldAllComplete];
expr /. f[args__] :>
RuleCondition[f[args] /. Print -> Echo]
]
Hold[{f[{1, {Echo[1]}}], g[{{{Print[1]}}}]}]
Note of course that this only works if you have a pattern of the form _Symbol[...]
Here's a way making use of Block
to cause f
to be inert:
Block[{f},
SetAttributes[f, HoldAllComplete];
expr /. f[args__] :>
RuleCondition[f[args] /. Print -> Echo]
]
Hold[{f[{1, {Echo[1]}}], g[{{{Print[1]}}}]}]
Note of course that this only works if you have a pattern of the form _Symbol[...]
answered yesterday
b3m2a1
26.9k257154
26.9k257154
add a comment |
add a comment |
I can think of one (ugly) way to do it:
Attributes[myHold] = {HoldAll};
expr /.
f[args__] :> With[
{res = myHold[args] /. Print -> Echo},
f @@ res /; True
] /.
HoldPattern[f_ @@ myHold[args__]] :> f[args]
(* Hold[{f[{1, {Echo[1]}}], g[{{{Print[1]}}}]}] *)
The idea is to wrap the contents of f
inside a function with HoldAll
attribute (not Hold
, to be able to identify it uniquely later on). In a first step, the expression is returned with myHold[…]
still in place. In a second round of replacements, myHold
is stripped out again.
1
+1. If we use the vanishing macro defined in (43096), we can writevanishing[{h}, expr /. x_f :> RuleCondition[h[x] /. Print -> Echo]]
.
– WReach
yesterday
add a comment |
I can think of one (ugly) way to do it:
Attributes[myHold] = {HoldAll};
expr /.
f[args__] :> With[
{res = myHold[args] /. Print -> Echo},
f @@ res /; True
] /.
HoldPattern[f_ @@ myHold[args__]] :> f[args]
(* Hold[{f[{1, {Echo[1]}}], g[{{{Print[1]}}}]}] *)
The idea is to wrap the contents of f
inside a function with HoldAll
attribute (not Hold
, to be able to identify it uniquely later on). In a first step, the expression is returned with myHold[…]
still in place. In a second round of replacements, myHold
is stripped out again.
1
+1. If we use the vanishing macro defined in (43096), we can writevanishing[{h}, expr /. x_f :> RuleCondition[h[x] /. Print -> Echo]]
.
– WReach
yesterday
add a comment |
I can think of one (ugly) way to do it:
Attributes[myHold] = {HoldAll};
expr /.
f[args__] :> With[
{res = myHold[args] /. Print -> Echo},
f @@ res /; True
] /.
HoldPattern[f_ @@ myHold[args__]] :> f[args]
(* Hold[{f[{1, {Echo[1]}}], g[{{{Print[1]}}}]}] *)
The idea is to wrap the contents of f
inside a function with HoldAll
attribute (not Hold
, to be able to identify it uniquely later on). In a first step, the expression is returned with myHold[…]
still in place. In a second round of replacements, myHold
is stripped out again.
I can think of one (ugly) way to do it:
Attributes[myHold] = {HoldAll};
expr /.
f[args__] :> With[
{res = myHold[args] /. Print -> Echo},
f @@ res /; True
] /.
HoldPattern[f_ @@ myHold[args__]] :> f[args]
(* Hold[{f[{1, {Echo[1]}}], g[{{{Print[1]}}}]}] *)
The idea is to wrap the contents of f
inside a function with HoldAll
attribute (not Hold
, to be able to identify it uniquely later on). In a first step, the expression is returned with myHold[…]
still in place. In a second round of replacements, myHold
is stripped out again.
answered yesterday
Lukas Lang
6,4351929
6,4351929
1
+1. If we use the vanishing macro defined in (43096), we can writevanishing[{h}, expr /. x_f :> RuleCondition[h[x] /. Print -> Echo]]
.
– WReach
yesterday
add a comment |
1
+1. If we use the vanishing macro defined in (43096), we can writevanishing[{h}, expr /. x_f :> RuleCondition[h[x] /. Print -> Echo]]
.
– WReach
yesterday
1
1
+1. If we use the vanishing macro defined in (43096), we can write
vanishing[{h}, expr /. x_f :> RuleCondition[h[x] /. Print -> Echo]]
.– WReach
yesterday
+1. If we use the vanishing macro defined in (43096), we can write
vanishing[{h}, expr /. x_f :> RuleCondition[h[x] /. Print -> Echo]]
.– WReach
yesterday
add a comment |
Alternatively:
expr /.
foo_f :> RuleCondition[Hold[foo] /. Print -> Echo] /.
Hold[foo_f] :> foo
add a comment |
Alternatively:
expr /.
foo_f :> RuleCondition[Hold[foo] /. Print -> Echo] /.
Hold[foo_f] :> foo
add a comment |
Alternatively:
expr /.
foo_f :> RuleCondition[Hold[foo] /. Print -> Echo] /.
Hold[foo_f] :> foo
Alternatively:
expr /.
foo_f :> RuleCondition[Hold[foo] /. Print -> Echo] /.
Hold[foo_f] :> foo
answered 8 hours ago
Kuba♦
103k12201516
103k12201516
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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.
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%2f188887%2fperform-localized-evaluation-leak-free-replacements%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