0
$\begingroup$

I have a function say $f$ and some initial point $x$ and pairs of iterators $\{\{a_1,b_1\},\{a_2,b_2\},...\}$

I want to foldlist as follows: $\{x,f[x,a_1,b_1],f[f[x,a_1,b_1],a_2,b_2],...\}$

can somebody suggest a builtin function to do this or a fast algorithm?

$\endgroup$

1 Answer 1

4
$\begingroup$

Here we go using FoldList

list = Array[{Subscript[a, #], Subscript[b, #]} &, {3, 1}] // 
  Flatten /@ # &  (* or Flatten[#,1]& doing the same, but faster *) 

FoldList[f[#, Sequence @@ #2]&, x, list]

enter image description here

EDIT, according suggestion from Shutao Tang to create the initial list in a more elegant and efficient way:

list={Subscript[a, #], Subscript[b, #]} & /@ Range[3]
$\endgroup$
3
  • 1
    $\begingroup$ Why not list={Subscript[a, #], Subscript[b, #]} & /@ Range[3] directly? $\endgroup$
    – xyz
    Apr 12, 2015 at 4:48
  • $\begingroup$ @Shutao Tang, ....because penguins are not always as smart as you, thx for your comment. $\endgroup$
    – penguin77
    Apr 12, 2015 at 8:53
  • $\begingroup$ We can learn each other .:) $\endgroup$
    – xyz
    Apr 13, 2015 at 1:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.