Cleanest way to take a[b[c]] to a[b][c]
$begingroup$
As indicated in the title I'm looking for the fastest way to transform a[b[c]]
into a[b][c]
, and the natural generalization to an arbitrary chaining of arguments. I'm sure there's got to be a convenient way that I've overlooked.
In my cases a
, b
, and c
can be any expression with any complicated internal structure they like.
function-construction
$endgroup$
add a comment |
$begingroup$
As indicated in the title I'm looking for the fastest way to transform a[b[c]]
into a[b][c]
, and the natural generalization to an arbitrary chaining of arguments. I'm sure there's got to be a convenient way that I've overlooked.
In my cases a
, b
, and c
can be any expression with any complicated internal structure they like.
function-construction
$endgroup$
1
$begingroup$
The solution likely would useOperate
.
$endgroup$
– QuantumDot
47 mins ago
$begingroup$
f = Curry[Replace][a_[b_[c_]] :> a[b][c]]
also works, so thata[b[c]] // f
gives the desired result.
$endgroup$
– Shredderroy
37 mins ago
$begingroup$
How about generalizing the question to takinga[b[c[d[...]]]]
toa[b][c][d]...
?
$endgroup$
– David G. Stork
26 mins ago
$begingroup$
@DavidG.Stork sorry I thought that was implicit
$endgroup$
– b3m2a1
20 mins ago
$begingroup$
@b3m2a1: Oh.... well I recommend you alter the question... and seem my new solution.
$endgroup$
– David G. Stork
18 mins ago
add a comment |
$begingroup$
As indicated in the title I'm looking for the fastest way to transform a[b[c]]
into a[b][c]
, and the natural generalization to an arbitrary chaining of arguments. I'm sure there's got to be a convenient way that I've overlooked.
In my cases a
, b
, and c
can be any expression with any complicated internal structure they like.
function-construction
$endgroup$
As indicated in the title I'm looking for the fastest way to transform a[b[c]]
into a[b][c]
, and the natural generalization to an arbitrary chaining of arguments. I'm sure there's got to be a convenient way that I've overlooked.
In my cases a
, b
, and c
can be any expression with any complicated internal structure they like.
function-construction
function-construction
edited 12 mins ago
David G. Stork
24.1k22153
24.1k22153
asked 1 hour ago
b3m2a1b3m2a1
27.2k257156
27.2k257156
1
$begingroup$
The solution likely would useOperate
.
$endgroup$
– QuantumDot
47 mins ago
$begingroup$
f = Curry[Replace][a_[b_[c_]] :> a[b][c]]
also works, so thata[b[c]] // f
gives the desired result.
$endgroup$
– Shredderroy
37 mins ago
$begingroup$
How about generalizing the question to takinga[b[c[d[...]]]]
toa[b][c][d]...
?
$endgroup$
– David G. Stork
26 mins ago
$begingroup$
@DavidG.Stork sorry I thought that was implicit
$endgroup$
– b3m2a1
20 mins ago
$begingroup$
@b3m2a1: Oh.... well I recommend you alter the question... and seem my new solution.
$endgroup$
– David G. Stork
18 mins ago
add a comment |
1
$begingroup$
The solution likely would useOperate
.
$endgroup$
– QuantumDot
47 mins ago
$begingroup$
f = Curry[Replace][a_[b_[c_]] :> a[b][c]]
also works, so thata[b[c]] // f
gives the desired result.
$endgroup$
– Shredderroy
37 mins ago
$begingroup$
How about generalizing the question to takinga[b[c[d[...]]]]
toa[b][c][d]...
?
$endgroup$
– David G. Stork
26 mins ago
$begingroup$
@DavidG.Stork sorry I thought that was implicit
$endgroup$
– b3m2a1
20 mins ago
$begingroup$
@b3m2a1: Oh.... well I recommend you alter the question... and seem my new solution.
$endgroup$
– David G. Stork
18 mins ago
1
1
$begingroup$
The solution likely would use
Operate
.$endgroup$
– QuantumDot
47 mins ago
$begingroup$
The solution likely would use
Operate
.$endgroup$
– QuantumDot
47 mins ago
$begingroup$
f = Curry[Replace][a_[b_[c_]] :> a[b][c]]
also works, so that a[b[c]] // f
gives the desired result.$endgroup$
– Shredderroy
37 mins ago
$begingroup$
f = Curry[Replace][a_[b_[c_]] :> a[b][c]]
also works, so that a[b[c]] // f
gives the desired result.$endgroup$
– Shredderroy
37 mins ago
$begingroup$
How about generalizing the question to taking
a[b[c[d[...]]]]
to a[b][c][d]...
?$endgroup$
– David G. Stork
26 mins ago
$begingroup$
How about generalizing the question to taking
a[b[c[d[...]]]]
to a[b][c][d]...
?$endgroup$
– David G. Stork
26 mins ago
$begingroup$
@DavidG.Stork sorry I thought that was implicit
$endgroup$
– b3m2a1
20 mins ago
$begingroup$
@DavidG.Stork sorry I thought that was implicit
$endgroup$
– b3m2a1
20 mins ago
$begingroup$
@b3m2a1: Oh.... well I recommend you alter the question... and seem my new solution.
$endgroup$
– David G. Stork
18 mins ago
$begingroup$
@b3m2a1: Oh.... well I recommend you alter the question... and seem my new solution.
$endgroup$
– David G. Stork
18 mins ago
add a comment |
2 Answers
2
active
oldest
votes
$begingroup$
Operate[#[[0]], First@#] &[a[b[c]]]
a[b][c]
ClearAll[deCompose]
deCompose = Nest[Operate[#[[0]], First@#] &, #, Depth[#] - 2] &;
deCompose@a[b[c]]
a[b][c]
exp = Compose[a, b, c, d, e, f, g]
a[b[c[d[e[f[g]]]]]]
deCompose @ exp
a[b][c][d][e][f][g]
$endgroup$
add a comment |
$begingroup$
Not particularly "clean," but it works:
Operate[Head[#], Level[#, 2][[2]]] & @ a[b[c]]
For the full generalization:
Nest[Operate[Head[#], Level[#, 2][[2]]] & , #, Depth[#] -2] & @
a[b[c[d[e]]]]
$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%2f190047%2fcleanest-way-to-take-abc-to-abc%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
Operate[#[[0]], First@#] &[a[b[c]]]
a[b][c]
ClearAll[deCompose]
deCompose = Nest[Operate[#[[0]], First@#] &, #, Depth[#] - 2] &;
deCompose@a[b[c]]
a[b][c]
exp = Compose[a, b, c, d, e, f, g]
a[b[c[d[e[f[g]]]]]]
deCompose @ exp
a[b][c][d][e][f][g]
$endgroup$
add a comment |
$begingroup$
Operate[#[[0]], First@#] &[a[b[c]]]
a[b][c]
ClearAll[deCompose]
deCompose = Nest[Operate[#[[0]], First@#] &, #, Depth[#] - 2] &;
deCompose@a[b[c]]
a[b][c]
exp = Compose[a, b, c, d, e, f, g]
a[b[c[d[e[f[g]]]]]]
deCompose @ exp
a[b][c][d][e][f][g]
$endgroup$
add a comment |
$begingroup$
Operate[#[[0]], First@#] &[a[b[c]]]
a[b][c]
ClearAll[deCompose]
deCompose = Nest[Operate[#[[0]], First@#] &, #, Depth[#] - 2] &;
deCompose@a[b[c]]
a[b][c]
exp = Compose[a, b, c, d, e, f, g]
a[b[c[d[e[f[g]]]]]]
deCompose @ exp
a[b][c][d][e][f][g]
$endgroup$
Operate[#[[0]], First@#] &[a[b[c]]]
a[b][c]
ClearAll[deCompose]
deCompose = Nest[Operate[#[[0]], First@#] &, #, Depth[#] - 2] &;
deCompose@a[b[c]]
a[b][c]
exp = Compose[a, b, c, d, e, f, g]
a[b[c[d[e[f[g]]]]]]
deCompose @ exp
a[b][c][d][e][f][g]
edited 14 mins ago
answered 21 mins ago
kglrkglr
180k9200413
180k9200413
add a comment |
add a comment |
$begingroup$
Not particularly "clean," but it works:
Operate[Head[#], Level[#, 2][[2]]] & @ a[b[c]]
For the full generalization:
Nest[Operate[Head[#], Level[#, 2][[2]]] & , #, Depth[#] -2] & @
a[b[c[d[e]]]]
$endgroup$
add a comment |
$begingroup$
Not particularly "clean," but it works:
Operate[Head[#], Level[#, 2][[2]]] & @ a[b[c]]
For the full generalization:
Nest[Operate[Head[#], Level[#, 2][[2]]] & , #, Depth[#] -2] & @
a[b[c[d[e]]]]
$endgroup$
add a comment |
$begingroup$
Not particularly "clean," but it works:
Operate[Head[#], Level[#, 2][[2]]] & @ a[b[c]]
For the full generalization:
Nest[Operate[Head[#], Level[#, 2][[2]]] & , #, Depth[#] -2] & @
a[b[c[d[e]]]]
$endgroup$
Not particularly "clean," but it works:
Operate[Head[#], Level[#, 2][[2]]] & @ a[b[c]]
For the full generalization:
Nest[Operate[Head[#], Level[#, 2][[2]]] & , #, Depth[#] -2] & @
a[b[c[d[e]]]]
edited 13 mins ago
answered 38 mins ago
David G. StorkDavid G. Stork
24.1k22153
24.1k22153
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%2f190047%2fcleanest-way-to-take-abc-to-abc%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$
The solution likely would use
Operate
.$endgroup$
– QuantumDot
47 mins ago
$begingroup$
f = Curry[Replace][a_[b_[c_]] :> a[b][c]]
also works, so thata[b[c]] // f
gives the desired result.$endgroup$
– Shredderroy
37 mins ago
$begingroup$
How about generalizing the question to taking
a[b[c[d[...]]]]
toa[b][c][d]...
?$endgroup$
– David G. Stork
26 mins ago
$begingroup$
@DavidG.Stork sorry I thought that was implicit
$endgroup$
– b3m2a1
20 mins ago
$begingroup$
@b3m2a1: Oh.... well I recommend you alter the question... and seem my new solution.
$endgroup$
– David G. Stork
18 mins ago