Returning the outputs of a nested structure
$begingroup$
Say we have a list
l = {{{a, b}, c}, d}
To which we then apply a function F in the following manner
Replace[l, x_List :> F[x], All]
F[{F[{F[{a, b}], c}], d}]
Is there away of extracting the outputs of each application of F to l, so that we get a list
{F[{a,b}],F[{F[{a,b}],c}],F[{F[{F[{a, b}], c}], d}]}
?
list-manipulation
$endgroup$
add a comment |
$begingroup$
Say we have a list
l = {{{a, b}, c}, d}
To which we then apply a function F in the following manner
Replace[l, x_List :> F[x], All]
F[{F[{F[{a, b}], c}], d}]
Is there away of extracting the outputs of each application of F to l, so that we get a list
{F[{a,b}],F[{F[{a,b}],c}],F[{F[{F[{a, b}], c}], d}]}
?
list-manipulation
$endgroup$
1
$begingroup$
Try withSow+Reap
$endgroup$
– Kuba♦
2 hours ago
add a comment |
$begingroup$
Say we have a list
l = {{{a, b}, c}, d}
To which we then apply a function F in the following manner
Replace[l, x_List :> F[x], All]
F[{F[{F[{a, b}], c}], d}]
Is there away of extracting the outputs of each application of F to l, so that we get a list
{F[{a,b}],F[{F[{a,b}],c}],F[{F[{F[{a, b}], c}], d}]}
?
list-manipulation
$endgroup$
Say we have a list
l = {{{a, b}, c}, d}
To which we then apply a function F in the following manner
Replace[l, x_List :> F[x], All]
F[{F[{F[{a, b}], c}], d}]
Is there away of extracting the outputs of each application of F to l, so that we get a list
{F[{a,b}],F[{F[{a,b}],c}],F[{F[{F[{a, b}], c}], d}]}
?
list-manipulation
list-manipulation
asked 2 hours ago
amator2357amator2357
4928
4928
1
$begingroup$
Try withSow+Reap
$endgroup$
– Kuba♦
2 hours ago
add a comment |
1
$begingroup$
Try withSow+Reap
$endgroup$
– Kuba♦
2 hours ago
1
1
$begingroup$
Try with
Sow+Reap$endgroup$
– Kuba♦
2 hours ago
$begingroup$
Try with
Sow+Reap$endgroup$
– Kuba♦
2 hours ago
add a comment |
2 Answers
2
active
oldest
votes
$begingroup$
I think I'd do it with FoldList. Like so:
l = {{{a, b}, c}, d};
FoldList[F[{#1, #2}] &, Flatten @ l] // Rest
{F[{a, b}], F[{F[{a, b}], c}], F[{F[{F[{a, b}], c}], d}]}
$endgroup$
$begingroup$
That is very nice! Thank you.
$endgroup$
– amator2357
51 mins ago
$begingroup$
@amator2357. Thanks for accepting my answer. But I don't understand why you considered it worth an acceptance but not an up-vote. You are aware, aren't you, that you can do both.
$endgroup$
– m_goldberg
44 mins ago
$begingroup$
Yes, I am aware, it wasn't intentional tho, I thought that I did upvote it. Thanks for pointing it out :)
$endgroup$
– amator2357
40 mins ago
$begingroup$
@amator2357. Your welcome and thank you for the up-vote.
$endgroup$
– m_goldberg
39 mins ago
add a comment |
$begingroup$
Using @Kuba 's suggestion:
Reap[Replace[l, x_List :> Sow[F[x]], All]]
{F[{F[{F[{a, b}], c}], d}], {{F[{a, b}], F[{F[{a, b}], c}], F[{F[{F[{a, b}], c}], d}]}}}
So to get precisely what I want
Reap[Replace[l, x_List :> Sow[F[x]], All]][[2]]
{{F[{a, b}], F[{F[{a, b}], c}], F[{F[{F[{a, b}], c}], d}]}}
$endgroup$
add a comment |
Your Answer
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%2f197366%2freturning-the-outputs-of-a-nested-structure%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$
I think I'd do it with FoldList. Like so:
l = {{{a, b}, c}, d};
FoldList[F[{#1, #2}] &, Flatten @ l] // Rest
{F[{a, b}], F[{F[{a, b}], c}], F[{F[{F[{a, b}], c}], d}]}
$endgroup$
$begingroup$
That is very nice! Thank you.
$endgroup$
– amator2357
51 mins ago
$begingroup$
@amator2357. Thanks for accepting my answer. But I don't understand why you considered it worth an acceptance but not an up-vote. You are aware, aren't you, that you can do both.
$endgroup$
– m_goldberg
44 mins ago
$begingroup$
Yes, I am aware, it wasn't intentional tho, I thought that I did upvote it. Thanks for pointing it out :)
$endgroup$
– amator2357
40 mins ago
$begingroup$
@amator2357. Your welcome and thank you for the up-vote.
$endgroup$
– m_goldberg
39 mins ago
add a comment |
$begingroup$
I think I'd do it with FoldList. Like so:
l = {{{a, b}, c}, d};
FoldList[F[{#1, #2}] &, Flatten @ l] // Rest
{F[{a, b}], F[{F[{a, b}], c}], F[{F[{F[{a, b}], c}], d}]}
$endgroup$
$begingroup$
That is very nice! Thank you.
$endgroup$
– amator2357
51 mins ago
$begingroup$
@amator2357. Thanks for accepting my answer. But I don't understand why you considered it worth an acceptance but not an up-vote. You are aware, aren't you, that you can do both.
$endgroup$
– m_goldberg
44 mins ago
$begingroup$
Yes, I am aware, it wasn't intentional tho, I thought that I did upvote it. Thanks for pointing it out :)
$endgroup$
– amator2357
40 mins ago
$begingroup$
@amator2357. Your welcome and thank you for the up-vote.
$endgroup$
– m_goldberg
39 mins ago
add a comment |
$begingroup$
I think I'd do it with FoldList. Like so:
l = {{{a, b}, c}, d};
FoldList[F[{#1, #2}] &, Flatten @ l] // Rest
{F[{a, b}], F[{F[{a, b}], c}], F[{F[{F[{a, b}], c}], d}]}
$endgroup$
I think I'd do it with FoldList. Like so:
l = {{{a, b}, c}, d};
FoldList[F[{#1, #2}] &, Flatten @ l] // Rest
{F[{a, b}], F[{F[{a, b}], c}], F[{F[{F[{a, b}], c}], d}]}
edited 1 hour ago
answered 2 hours ago
m_goldbergm_goldberg
89.3k873201
89.3k873201
$begingroup$
That is very nice! Thank you.
$endgroup$
– amator2357
51 mins ago
$begingroup$
@amator2357. Thanks for accepting my answer. But I don't understand why you considered it worth an acceptance but not an up-vote. You are aware, aren't you, that you can do both.
$endgroup$
– m_goldberg
44 mins ago
$begingroup$
Yes, I am aware, it wasn't intentional tho, I thought that I did upvote it. Thanks for pointing it out :)
$endgroup$
– amator2357
40 mins ago
$begingroup$
@amator2357. Your welcome and thank you for the up-vote.
$endgroup$
– m_goldberg
39 mins ago
add a comment |
$begingroup$
That is very nice! Thank you.
$endgroup$
– amator2357
51 mins ago
$begingroup$
@amator2357. Thanks for accepting my answer. But I don't understand why you considered it worth an acceptance but not an up-vote. You are aware, aren't you, that you can do both.
$endgroup$
– m_goldberg
44 mins ago
$begingroup$
Yes, I am aware, it wasn't intentional tho, I thought that I did upvote it. Thanks for pointing it out :)
$endgroup$
– amator2357
40 mins ago
$begingroup$
@amator2357. Your welcome and thank you for the up-vote.
$endgroup$
– m_goldberg
39 mins ago
$begingroup$
That is very nice! Thank you.
$endgroup$
– amator2357
51 mins ago
$begingroup$
That is very nice! Thank you.
$endgroup$
– amator2357
51 mins ago
$begingroup$
@amator2357. Thanks for accepting my answer. But I don't understand why you considered it worth an acceptance but not an up-vote. You are aware, aren't you, that you can do both.
$endgroup$
– m_goldberg
44 mins ago
$begingroup$
@amator2357. Thanks for accepting my answer. But I don't understand why you considered it worth an acceptance but not an up-vote. You are aware, aren't you, that you can do both.
$endgroup$
– m_goldberg
44 mins ago
$begingroup$
Yes, I am aware, it wasn't intentional tho, I thought that I did upvote it. Thanks for pointing it out :)
$endgroup$
– amator2357
40 mins ago
$begingroup$
Yes, I am aware, it wasn't intentional tho, I thought that I did upvote it. Thanks for pointing it out :)
$endgroup$
– amator2357
40 mins ago
$begingroup$
@amator2357. Your welcome and thank you for the up-vote.
$endgroup$
– m_goldberg
39 mins ago
$begingroup$
@amator2357. Your welcome and thank you for the up-vote.
$endgroup$
– m_goldberg
39 mins ago
add a comment |
$begingroup$
Using @Kuba 's suggestion:
Reap[Replace[l, x_List :> Sow[F[x]], All]]
{F[{F[{F[{a, b}], c}], d}], {{F[{a, b}], F[{F[{a, b}], c}], F[{F[{F[{a, b}], c}], d}]}}}
So to get precisely what I want
Reap[Replace[l, x_List :> Sow[F[x]], All]][[2]]
{{F[{a, b}], F[{F[{a, b}], c}], F[{F[{F[{a, b}], c}], d}]}}
$endgroup$
add a comment |
$begingroup$
Using @Kuba 's suggestion:
Reap[Replace[l, x_List :> Sow[F[x]], All]]
{F[{F[{F[{a, b}], c}], d}], {{F[{a, b}], F[{F[{a, b}], c}], F[{F[{F[{a, b}], c}], d}]}}}
So to get precisely what I want
Reap[Replace[l, x_List :> Sow[F[x]], All]][[2]]
{{F[{a, b}], F[{F[{a, b}], c}], F[{F[{F[{a, b}], c}], d}]}}
$endgroup$
add a comment |
$begingroup$
Using @Kuba 's suggestion:
Reap[Replace[l, x_List :> Sow[F[x]], All]]
{F[{F[{F[{a, b}], c}], d}], {{F[{a, b}], F[{F[{a, b}], c}], F[{F[{F[{a, b}], c}], d}]}}}
So to get precisely what I want
Reap[Replace[l, x_List :> Sow[F[x]], All]][[2]]
{{F[{a, b}], F[{F[{a, b}], c}], F[{F[{F[{a, b}], c}], d}]}}
$endgroup$
Using @Kuba 's suggestion:
Reap[Replace[l, x_List :> Sow[F[x]], All]]
{F[{F[{F[{a, b}], c}], d}], {{F[{a, b}], F[{F[{a, b}], c}], F[{F[{F[{a, b}], c}], d}]}}}
So to get precisely what I want
Reap[Replace[l, x_List :> Sow[F[x]], All]][[2]]
{{F[{a, b}], F[{F[{a, b}], c}], F[{F[{F[{a, b}], c}], d}]}}
edited 39 mins ago
answered 2 hours ago
amator2357amator2357
4928
4928
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%2f197366%2freturning-the-outputs-of-a-nested-structure%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$
Try with
Sow+Reap$endgroup$
– Kuba♦
2 hours ago