How to make FoldListIndexed ie FoldList with an index or iterator?












3












$begingroup$


Is there a functional or inbuilt way to add an index to FoldList?



i.e.:



FoldListIndex[f,x,{a,b,...}]


gives



{x,f[x,a,1],f[f[x,a,1],b,2],...}


My current method with a loop is quite unsatisfactory










share|improve this question







New contributor




user62657 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$

















    3












    $begingroup$


    Is there a functional or inbuilt way to add an index to FoldList?



    i.e.:



    FoldListIndex[f,x,{a,b,...}]


    gives



    {x,f[x,a,1],f[f[x,a,1],b,2],...}


    My current method with a loop is quite unsatisfactory










    share|improve this question







    New contributor




    user62657 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.







    $endgroup$















      3












      3








      3





      $begingroup$


      Is there a functional or inbuilt way to add an index to FoldList?



      i.e.:



      FoldListIndex[f,x,{a,b,...}]


      gives



      {x,f[x,a,1],f[f[x,a,1],b,2],...}


      My current method with a loop is quite unsatisfactory










      share|improve this question







      New contributor




      user62657 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.







      $endgroup$




      Is there a functional or inbuilt way to add an index to FoldList?



      i.e.:



      FoldListIndex[f,x,{a,b,...}]


      gives



      {x,f[x,a,1],f[f[x,a,1],b,2],...}


      My current method with a loop is quite unsatisfactory







      list-manipulation function-construction functional-style






      share|improve this question







      New contributor




      user62657 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question







      New contributor




      user62657 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question






      New contributor




      user62657 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 4 hours ago









      user62657user62657

      182




      182




      New contributor




      user62657 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      user62657 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      user62657 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






















          2 Answers
          2






          active

          oldest

          votes


















          0












          $begingroup$

          foldIndexedList = Module[{i = 1, f = #}, FoldList[f[##, i++] &, ##2]] &;
          foldIndexedList[f, x, {a, b, c, d}]



          {x, f[x, a, 1], f[f[x, a, 1], b, 2], f[f[f[x, a, 1], b, 2], c, 3],
          f[f[f[f[x, a, 1], b, 2], c, 3], d, 4]}




          foldIndexedList2 =  Module[{f = #}, 
          FoldList[f[#, ## & @@ #2] &, #2, MapIndexed[{#, #2[[1]]} &]@#3]] &;
          foldIndexedList2[f, x, {a, b, c, d}]



          {x, f[x, a, 1], f[f[x, a, 1], b, 2], f[f[f[x, a, 1], b, 2], c, 3],
          f[f[f[f[x, a, 1], b, 2], c, 3], d, 4]}







          share|improve this answer











          $endgroup$













          • $begingroup$
            Thanks rafalc and kglr for the answers. I selected kglr as it resulted in a faster function for my specific implementation and it worked with foldIndexedList[f,{a,b,c,d}] as well
            $endgroup$
            – user62657
            2 hours ago










          • $begingroup$
            @user62657, thank you for the accept; and welcome to mma.se.
            $endgroup$
            – kglr
            2 hours ago



















          2












          $begingroup$

          You can try



          FoldListIndexed[f_, x_, lst_] := 
          FoldList[
          Function[{a, b}, f[a, Sequence @@ b]],
          x,
          Transpose[{lst, Range @ Length @ lst}]
          ]


          and then



          In[4]:= FoldListIndexed[f, x, {a, b, c, d}]

          Out[4]= {x, f[x, a, 1], f[f[x, a, 1], b, 2],
          f[f[f[x, a, 1], b, 2], c, 3], f[f[f[f[x, a, 1], b, 2], c, 3], d, 4]}





          share|improve this answer











          $endgroup$













          • $begingroup$
            Nice solution, but I would rather avoid using Block, especially for functions living in Global` . Imagine that g has been defined globally, and is called by f - then this code will break it in a very non-obvious way. I would rather use a pure function for g, and With instead of Block.
            $endgroup$
            – Leonid Shifrin
            3 hours ago












          • $begingroup$
            @LeonidShifrin thank you, I have edited my answer following your suggestions (got rid of Block entirely - I think the code is still readable even without the helper variable)
            $endgroup$
            – rafalc
            3 hours ago











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


          }
          });






          user62657 is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f190503%2fhow-to-make-foldlistindexed-ie-foldlist-with-an-index-or-iterator%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









          0












          $begingroup$

          foldIndexedList = Module[{i = 1, f = #}, FoldList[f[##, i++] &, ##2]] &;
          foldIndexedList[f, x, {a, b, c, d}]



          {x, f[x, a, 1], f[f[x, a, 1], b, 2], f[f[f[x, a, 1], b, 2], c, 3],
          f[f[f[f[x, a, 1], b, 2], c, 3], d, 4]}




          foldIndexedList2 =  Module[{f = #}, 
          FoldList[f[#, ## & @@ #2] &, #2, MapIndexed[{#, #2[[1]]} &]@#3]] &;
          foldIndexedList2[f, x, {a, b, c, d}]



          {x, f[x, a, 1], f[f[x, a, 1], b, 2], f[f[f[x, a, 1], b, 2], c, 3],
          f[f[f[f[x, a, 1], b, 2], c, 3], d, 4]}







          share|improve this answer











          $endgroup$













          • $begingroup$
            Thanks rafalc and kglr for the answers. I selected kglr as it resulted in a faster function for my specific implementation and it worked with foldIndexedList[f,{a,b,c,d}] as well
            $endgroup$
            – user62657
            2 hours ago










          • $begingroup$
            @user62657, thank you for the accept; and welcome to mma.se.
            $endgroup$
            – kglr
            2 hours ago
















          0












          $begingroup$

          foldIndexedList = Module[{i = 1, f = #}, FoldList[f[##, i++] &, ##2]] &;
          foldIndexedList[f, x, {a, b, c, d}]



          {x, f[x, a, 1], f[f[x, a, 1], b, 2], f[f[f[x, a, 1], b, 2], c, 3],
          f[f[f[f[x, a, 1], b, 2], c, 3], d, 4]}




          foldIndexedList2 =  Module[{f = #}, 
          FoldList[f[#, ## & @@ #2] &, #2, MapIndexed[{#, #2[[1]]} &]@#3]] &;
          foldIndexedList2[f, x, {a, b, c, d}]



          {x, f[x, a, 1], f[f[x, a, 1], b, 2], f[f[f[x, a, 1], b, 2], c, 3],
          f[f[f[f[x, a, 1], b, 2], c, 3], d, 4]}







          share|improve this answer











          $endgroup$













          • $begingroup$
            Thanks rafalc and kglr for the answers. I selected kglr as it resulted in a faster function for my specific implementation and it worked with foldIndexedList[f,{a,b,c,d}] as well
            $endgroup$
            – user62657
            2 hours ago










          • $begingroup$
            @user62657, thank you for the accept; and welcome to mma.se.
            $endgroup$
            – kglr
            2 hours ago














          0












          0








          0





          $begingroup$

          foldIndexedList = Module[{i = 1, f = #}, FoldList[f[##, i++] &, ##2]] &;
          foldIndexedList[f, x, {a, b, c, d}]



          {x, f[x, a, 1], f[f[x, a, 1], b, 2], f[f[f[x, a, 1], b, 2], c, 3],
          f[f[f[f[x, a, 1], b, 2], c, 3], d, 4]}




          foldIndexedList2 =  Module[{f = #}, 
          FoldList[f[#, ## & @@ #2] &, #2, MapIndexed[{#, #2[[1]]} &]@#3]] &;
          foldIndexedList2[f, x, {a, b, c, d}]



          {x, f[x, a, 1], f[f[x, a, 1], b, 2], f[f[f[x, a, 1], b, 2], c, 3],
          f[f[f[f[x, a, 1], b, 2], c, 3], d, 4]}







          share|improve this answer











          $endgroup$



          foldIndexedList = Module[{i = 1, f = #}, FoldList[f[##, i++] &, ##2]] &;
          foldIndexedList[f, x, {a, b, c, d}]



          {x, f[x, a, 1], f[f[x, a, 1], b, 2], f[f[f[x, a, 1], b, 2], c, 3],
          f[f[f[f[x, a, 1], b, 2], c, 3], d, 4]}




          foldIndexedList2 =  Module[{f = #}, 
          FoldList[f[#, ## & @@ #2] &, #2, MapIndexed[{#, #2[[1]]} &]@#3]] &;
          foldIndexedList2[f, x, {a, b, c, d}]



          {x, f[x, a, 1], f[f[x, a, 1], b, 2], f[f[f[x, a, 1], b, 2], c, 3],
          f[f[f[f[x, a, 1], b, 2], c, 3], d, 4]}








          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 3 hours ago

























          answered 3 hours ago









          kglrkglr

          181k10200414




          181k10200414












          • $begingroup$
            Thanks rafalc and kglr for the answers. I selected kglr as it resulted in a faster function for my specific implementation and it worked with foldIndexedList[f,{a,b,c,d}] as well
            $endgroup$
            – user62657
            2 hours ago










          • $begingroup$
            @user62657, thank you for the accept; and welcome to mma.se.
            $endgroup$
            – kglr
            2 hours ago


















          • $begingroup$
            Thanks rafalc and kglr for the answers. I selected kglr as it resulted in a faster function for my specific implementation and it worked with foldIndexedList[f,{a,b,c,d}] as well
            $endgroup$
            – user62657
            2 hours ago










          • $begingroup$
            @user62657, thank you for the accept; and welcome to mma.se.
            $endgroup$
            – kglr
            2 hours ago
















          $begingroup$
          Thanks rafalc and kglr for the answers. I selected kglr as it resulted in a faster function for my specific implementation and it worked with foldIndexedList[f,{a,b,c,d}] as well
          $endgroup$
          – user62657
          2 hours ago




          $begingroup$
          Thanks rafalc and kglr for the answers. I selected kglr as it resulted in a faster function for my specific implementation and it worked with foldIndexedList[f,{a,b,c,d}] as well
          $endgroup$
          – user62657
          2 hours ago












          $begingroup$
          @user62657, thank you for the accept; and welcome to mma.se.
          $endgroup$
          – kglr
          2 hours ago




          $begingroup$
          @user62657, thank you for the accept; and welcome to mma.se.
          $endgroup$
          – kglr
          2 hours ago











          2












          $begingroup$

          You can try



          FoldListIndexed[f_, x_, lst_] := 
          FoldList[
          Function[{a, b}, f[a, Sequence @@ b]],
          x,
          Transpose[{lst, Range @ Length @ lst}]
          ]


          and then



          In[4]:= FoldListIndexed[f, x, {a, b, c, d}]

          Out[4]= {x, f[x, a, 1], f[f[x, a, 1], b, 2],
          f[f[f[x, a, 1], b, 2], c, 3], f[f[f[f[x, a, 1], b, 2], c, 3], d, 4]}





          share|improve this answer











          $endgroup$













          • $begingroup$
            Nice solution, but I would rather avoid using Block, especially for functions living in Global` . Imagine that g has been defined globally, and is called by f - then this code will break it in a very non-obvious way. I would rather use a pure function for g, and With instead of Block.
            $endgroup$
            – Leonid Shifrin
            3 hours ago












          • $begingroup$
            @LeonidShifrin thank you, I have edited my answer following your suggestions (got rid of Block entirely - I think the code is still readable even without the helper variable)
            $endgroup$
            – rafalc
            3 hours ago
















          2












          $begingroup$

          You can try



          FoldListIndexed[f_, x_, lst_] := 
          FoldList[
          Function[{a, b}, f[a, Sequence @@ b]],
          x,
          Transpose[{lst, Range @ Length @ lst}]
          ]


          and then



          In[4]:= FoldListIndexed[f, x, {a, b, c, d}]

          Out[4]= {x, f[x, a, 1], f[f[x, a, 1], b, 2],
          f[f[f[x, a, 1], b, 2], c, 3], f[f[f[f[x, a, 1], b, 2], c, 3], d, 4]}





          share|improve this answer











          $endgroup$













          • $begingroup$
            Nice solution, but I would rather avoid using Block, especially for functions living in Global` . Imagine that g has been defined globally, and is called by f - then this code will break it in a very non-obvious way. I would rather use a pure function for g, and With instead of Block.
            $endgroup$
            – Leonid Shifrin
            3 hours ago












          • $begingroup$
            @LeonidShifrin thank you, I have edited my answer following your suggestions (got rid of Block entirely - I think the code is still readable even without the helper variable)
            $endgroup$
            – rafalc
            3 hours ago














          2












          2








          2





          $begingroup$

          You can try



          FoldListIndexed[f_, x_, lst_] := 
          FoldList[
          Function[{a, b}, f[a, Sequence @@ b]],
          x,
          Transpose[{lst, Range @ Length @ lst}]
          ]


          and then



          In[4]:= FoldListIndexed[f, x, {a, b, c, d}]

          Out[4]= {x, f[x, a, 1], f[f[x, a, 1], b, 2],
          f[f[f[x, a, 1], b, 2], c, 3], f[f[f[f[x, a, 1], b, 2], c, 3], d, 4]}





          share|improve this answer











          $endgroup$



          You can try



          FoldListIndexed[f_, x_, lst_] := 
          FoldList[
          Function[{a, b}, f[a, Sequence @@ b]],
          x,
          Transpose[{lst, Range @ Length @ lst}]
          ]


          and then



          In[4]:= FoldListIndexed[f, x, {a, b, c, d}]

          Out[4]= {x, f[x, a, 1], f[f[x, a, 1], b, 2],
          f[f[f[x, a, 1], b, 2], c, 3], f[f[f[f[x, a, 1], b, 2], c, 3], d, 4]}






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 3 hours ago

























          answered 3 hours ago









          rafalcrafalc

          563211




          563211












          • $begingroup$
            Nice solution, but I would rather avoid using Block, especially for functions living in Global` . Imagine that g has been defined globally, and is called by f - then this code will break it in a very non-obvious way. I would rather use a pure function for g, and With instead of Block.
            $endgroup$
            – Leonid Shifrin
            3 hours ago












          • $begingroup$
            @LeonidShifrin thank you, I have edited my answer following your suggestions (got rid of Block entirely - I think the code is still readable even without the helper variable)
            $endgroup$
            – rafalc
            3 hours ago


















          • $begingroup$
            Nice solution, but I would rather avoid using Block, especially for functions living in Global` . Imagine that g has been defined globally, and is called by f - then this code will break it in a very non-obvious way. I would rather use a pure function for g, and With instead of Block.
            $endgroup$
            – Leonid Shifrin
            3 hours ago












          • $begingroup$
            @LeonidShifrin thank you, I have edited my answer following your suggestions (got rid of Block entirely - I think the code is still readable even without the helper variable)
            $endgroup$
            – rafalc
            3 hours ago
















          $begingroup$
          Nice solution, but I would rather avoid using Block, especially for functions living in Global` . Imagine that g has been defined globally, and is called by f - then this code will break it in a very non-obvious way. I would rather use a pure function for g, and With instead of Block.
          $endgroup$
          – Leonid Shifrin
          3 hours ago






          $begingroup$
          Nice solution, but I would rather avoid using Block, especially for functions living in Global` . Imagine that g has been defined globally, and is called by f - then this code will break it in a very non-obvious way. I would rather use a pure function for g, and With instead of Block.
          $endgroup$
          – Leonid Shifrin
          3 hours ago














          $begingroup$
          @LeonidShifrin thank you, I have edited my answer following your suggestions (got rid of Block entirely - I think the code is still readable even without the helper variable)
          $endgroup$
          – rafalc
          3 hours ago




          $begingroup$
          @LeonidShifrin thank you, I have edited my answer following your suggestions (got rid of Block entirely - I think the code is still readable even without the helper variable)
          $endgroup$
          – rafalc
          3 hours ago










          user62657 is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          user62657 is a new contributor. Be nice, and check out our Code of Conduct.













          user62657 is a new contributor. Be nice, and check out our Code of Conduct.












          user62657 is a new contributor. Be nice, and check out our Code of Conduct.
















          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%2f190503%2fhow-to-make-foldlistindexed-ie-foldlist-with-an-index-or-iterator%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

          Why is a white electrical wire connected to 2 black wires?

          Waikiki

          Steinsetfjorden