How to make FoldListIndexed ie FoldList with an index or iterator?
$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
list-manipulation function-construction functional-style
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$
add a comment |
$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
list-manipulation function-construction functional-style
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$
add a comment |
$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
list-manipulation function-construction functional-style
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
list-manipulation function-construction functional-style
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.
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.
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
$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]}
$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
add a comment |
$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]}
$endgroup$
$begingroup$
Nice solution, but I would rather avoid usingBlock, especially for functions living inGlobal`. Imagine thatghas been defined globally, and is called byf- then this code will break it in a very non-obvious way. I would rather use a pure function forg, andWithinstead ofBlock.
$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
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
});
}
});
user62657 is a new contributor. Be nice, and check out our Code of Conduct.
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%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
$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]}
$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
add a comment |
$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]}
$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
add a comment |
$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]}
$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]}
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
add a comment |
$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
add a comment |
$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]}
$endgroup$
$begingroup$
Nice solution, but I would rather avoid usingBlock, especially for functions living inGlobal`. Imagine thatghas been defined globally, and is called byf- then this code will break it in a very non-obvious way. I would rather use a pure function forg, andWithinstead ofBlock.
$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
add a comment |
$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]}
$endgroup$
$begingroup$
Nice solution, but I would rather avoid usingBlock, especially for functions living inGlobal`. Imagine thatghas been defined globally, and is called byf- then this code will break it in a very non-obvious way. I would rather use a pure function forg, andWithinstead ofBlock.
$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
add a comment |
$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]}
$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]}
edited 3 hours ago
answered 3 hours ago
rafalcrafalc
563211
563211
$begingroup$
Nice solution, but I would rather avoid usingBlock, especially for functions living inGlobal`. Imagine thatghas been defined globally, and is called byf- then this code will break it in a very non-obvious way. I would rather use a pure function forg, andWithinstead ofBlock.
$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
add a comment |
$begingroup$
Nice solution, but I would rather avoid usingBlock, especially for functions living inGlobal`. Imagine thatghas been defined globally, and is called byf- then this code will break it in a very non-obvious way. I would rather use a pure function forg, andWithinstead ofBlock.
$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
add a comment |
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.
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.
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%2f190503%2fhow-to-make-foldlistindexed-ie-foldlist-with-an-index-or-iterator%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