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












4












$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









$endgroup$

















    4












    $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









    $endgroup$















      4












      4








      4


      2



      $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









      $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













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 30 at 7:55









      user62657user62657

      232




      232






















          2 Answers
          2






          active

          oldest

          votes


















          3












          $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
            Jan 30 at 9:37










          • $begingroup$
            @user62657, thank you for the accept; and welcome to mma.se.
            $endgroup$
            – kglr
            Jan 30 at 9:50



















          3












          $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
            Jan 30 at 8:35












          • $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
            Jan 30 at 8:47











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


          }
          });














          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









          3












          $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
            Jan 30 at 9:37










          • $begingroup$
            @user62657, thank you for the accept; and welcome to mma.se.
            $endgroup$
            – kglr
            Jan 30 at 9:50
















          3












          $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
            Jan 30 at 9:37










          • $begingroup$
            @user62657, thank you for the accept; and welcome to mma.se.
            $endgroup$
            – kglr
            Jan 30 at 9:50














          3












          3








          3





          $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 Jan 30 at 9:08

























          answered Jan 30 at 9:00









          kglrkglr

          188k10204422




          188k10204422












          • $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
            Jan 30 at 9:37










          • $begingroup$
            @user62657, thank you for the accept; and welcome to mma.se.
            $endgroup$
            – kglr
            Jan 30 at 9:50


















          • $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
            Jan 30 at 9:37










          • $begingroup$
            @user62657, thank you for the accept; and welcome to mma.se.
            $endgroup$
            – kglr
            Jan 30 at 9:50
















          $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
          Jan 30 at 9:37




          $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
          Jan 30 at 9:37












          $begingroup$
          @user62657, thank you for the accept; and welcome to mma.se.
          $endgroup$
          – kglr
          Jan 30 at 9:50




          $begingroup$
          @user62657, thank you for the accept; and welcome to mma.se.
          $endgroup$
          – kglr
          Jan 30 at 9:50











          3












          $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
            Jan 30 at 8:35












          • $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
            Jan 30 at 8:47
















          3












          $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
            Jan 30 at 8:35












          • $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
            Jan 30 at 8:47














          3












          3








          3





          $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 Jan 30 at 8:43

























          answered Jan 30 at 8:22









          rafalcrafalc

          698212




          698212












          • $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
            Jan 30 at 8:35












          • $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
            Jan 30 at 8:47


















          • $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
            Jan 30 at 8:35












          • $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
            Jan 30 at 8:47
















          $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
          Jan 30 at 8:35






          $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
          Jan 30 at 8:35














          $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
          Jan 30 at 8:47




          $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
          Jan 30 at 8:47


















          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%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

          Human spaceflight

          Can not write log (Is /dev/pts mounted?) - openpty in Ubuntu-on-Windows?

          張江高科駅