Returning the outputs of a nested structure












1












$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}]}




?










share|improve this question









$endgroup$








  • 1




    $begingroup$
    Try with Sow+Reap
    $endgroup$
    – Kuba
    2 hours ago
















1












$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}]}




?










share|improve this question









$endgroup$








  • 1




    $begingroup$
    Try with Sow+Reap
    $endgroup$
    – Kuba
    2 hours ago














1












1








1





$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}]}




?










share|improve this question









$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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 2 hours ago









amator2357amator2357

4928




4928








  • 1




    $begingroup$
    Try with Sow+Reap
    $endgroup$
    – Kuba
    2 hours ago














  • 1




    $begingroup$
    Try with Sow+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










2 Answers
2






active

oldest

votes


















1












$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}]}







share|improve this answer











$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



















2












$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}]}}






share|improve this answer











$endgroup$














    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
    });


    }
    });














    draft saved

    draft discarded


















    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









    1












    $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}]}







    share|improve this answer











    $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
















    1












    $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}]}







    share|improve this answer











    $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














    1












    1








    1





    $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}]}







    share|improve this answer











    $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}]}








    share|improve this answer














    share|improve this answer



    share|improve this answer








    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


















    • $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











    2












    $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}]}}






    share|improve this answer











    $endgroup$


















      2












      $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}]}}






      share|improve this answer











      $endgroup$
















        2












        2








        2





        $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}]}}






        share|improve this answer











        $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}]}}







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited 39 mins ago

























        answered 2 hours ago









        amator2357amator2357

        4928




        4928






























            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            香粉寮

            GameSpot